|
| 1 | +import { |
| 2 | + fireEvent, render, screen, waitFor, |
| 3 | +} from '@testing-library/react'; |
| 4 | +import MockAdapter from 'axios-mock-adapter'; |
| 5 | +import { act } from 'react-dom/test-utils'; |
| 6 | +import { IntlProvider } from 'react-intl'; |
| 7 | +import { Context as ResponsiveContext } from 'react-responsive'; |
| 8 | + |
| 9 | +import { getConfig, initializeMockApp } from '@edx/frontend-platform'; |
| 10 | +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
| 11 | +import { AppProvider } from '@edx/frontend-platform/react'; |
| 12 | + |
| 13 | +import { initializeStore } from '../../store'; |
| 14 | +import executeThunk from '../../test-utils'; |
| 15 | +import { getDiscussionsConfigUrl } from '../data/api'; |
| 16 | +import fetchCourseConfig from '../data/thunks'; |
| 17 | +import DiscussionsConfirmEmailBanner from './DiscussionsConfirmEmailBanner'; |
| 18 | +import messages from './messages'; |
| 19 | + |
| 20 | +const courseId = 'course-v1:edX+DemoX+Demo_Course'; |
| 21 | +let axiosMock; |
| 22 | +let store; |
| 23 | + |
| 24 | +function renderComponent() { |
| 25 | + render( |
| 26 | + <IntlProvider locale="en"> |
| 27 | + <ResponsiveContext.Provider value={{ width: 1280 }}> |
| 28 | + <AppProvider store={store}> |
| 29 | + <DiscussionsConfirmEmailBanner /> |
| 30 | + </AppProvider> |
| 31 | + </ResponsiveContext.Provider> |
| 32 | + </IntlProvider>, |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +describe('DiscussionsConfirmEmailBanner', () => { |
| 37 | + beforeEach(async () => { |
| 38 | + initializeMockApp({ |
| 39 | + authenticatedUser: { |
| 40 | + userId: 3, |
| 41 | + username: 'abc123', |
| 42 | + administrator: true, |
| 43 | + roles: [], |
| 44 | + }, |
| 45 | + }); |
| 46 | + axiosMock = new MockAdapter(getAuthenticatedHttpClient()); |
| 47 | + store = initializeStore(); |
| 48 | + }); |
| 49 | + |
| 50 | + describe('render', () => { |
| 51 | + it('does not show when email is verified', async () => { |
| 52 | + axiosMock.onGet(getDiscussionsConfigUrl(courseId)).reply(200, { isEmailVerified: true }); |
| 53 | + await executeThunk(fetchCourseConfig(courseId), store.dispatch, store.getState); |
| 54 | + renderComponent(); |
| 55 | + expect(screen.queryByRole('alert')).toBeNull(); |
| 56 | + }); |
| 57 | + |
| 58 | + describe('when email is unverified', () => { |
| 59 | + let resendEmailUrl; |
| 60 | + beforeEach(async () => { |
| 61 | + resendEmailUrl = `${getConfig().LMS_BASE_URL}/api/send_account_activation_email`; |
| 62 | + axiosMock.onGet(getDiscussionsConfigUrl(courseId)).reply(200, { isEmailVerified: false }); |
| 63 | + axiosMock.onPost(resendEmailUrl).reply(200); |
| 64 | + await executeThunk(fetchCourseConfig(courseId), store.dispatch, store.getState); |
| 65 | + renderComponent(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('shows banner and confirm now button', async () => { |
| 69 | + const banner = await screen.findByRole('alert'); |
| 70 | + expect(banner.textContent).toContain('Remember to confirm'); |
| 71 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 72 | + expect(confirmButton).toBeInTheDocument(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('opens modal, closes banner, and calls resend email API when confirm now button is clicked', async () => { |
| 76 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 77 | + await act(async () => { |
| 78 | + fireEvent.click(confirmButton); |
| 79 | + }); |
| 80 | + await waitFor(() => { |
| 81 | + expect(screen.getByRole('dialog')).toBeInTheDocument(); |
| 82 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 83 | + expect(axiosMock.history.post).toHaveLength(1); |
| 84 | + expect(axiosMock.history.post[0].url).toBe(resendEmailUrl); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('shows modal header, body, image, and confirm email button and closes modal and banner on click', async () => { |
| 89 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 90 | + await act(async () => { |
| 91 | + fireEvent.click(confirmButton); |
| 92 | + }); |
| 93 | + await waitFor(() => { |
| 94 | + expect(screen.getByText(messages.confirmEmailModalHeader.defaultMessage)).toBeInTheDocument(); |
| 95 | + expect(screen.getByText(messages.confirmEmailModalBody.defaultMessage)).toBeInTheDocument(); |
| 96 | + expect(screen.getByRole('img', { name: messages.confirmEmailImageAlt.defaultMessage })).toBeInTheDocument(); |
| 97 | + |
| 98 | + const verifyButton = screen.getByRole('button', { name: messages.verifiedConfirmEmailButton.defaultMessage }); |
| 99 | + expect(verifyButton).toBeInTheDocument(); |
| 100 | + act(() => { |
| 101 | + fireEvent.click(verifyButton); |
| 102 | + }); |
| 103 | + }); |
| 104 | + await waitFor(() => { |
| 105 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 106 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments