|
| 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 | + const banner = screen.queryByRole('alert'); |
| 56 | + expect(banner).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('when email is unverified', () => { |
| 60 | + let resendEmailUrl; |
| 61 | + beforeEach(async () => { |
| 62 | + resendEmailUrl = `${getConfig().LMS_BASE_URL}/api/send_account_activation_email`; |
| 63 | + axiosMock.onGet(getDiscussionsConfigUrl(courseId)).reply(200, { isEmailVerified: false }); |
| 64 | + axiosMock.onPost(resendEmailUrl).reply(200); |
| 65 | + await executeThunk(fetchCourseConfig(courseId), store.dispatch, store.getState); |
| 66 | + renderComponent(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('shows banner', async () => { |
| 70 | + const banner = await screen.findByRole('alert'); |
| 71 | + expect(banner.textContent).toContain('Remember to confirm'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('shows confirm now button', async () => { |
| 75 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 76 | + expect(confirmButton).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('shows modal when confirm now button is clicked', async () => { |
| 80 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 81 | + await act(async () => { |
| 82 | + fireEvent.click(confirmButton); |
| 83 | + }); |
| 84 | + await waitFor(() => { |
| 85 | + const modal = screen.getByRole('dialog'); |
| 86 | + expect(modal).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + it('shows modal header and body', async () => { |
| 91 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 92 | + await act(async () => { |
| 93 | + fireEvent.click(confirmButton); |
| 94 | + }); |
| 95 | + await waitFor(() => { |
| 96 | + const modalHeader = screen.getByText(messages.confirmEmailModalHeader.defaultMessage); |
| 97 | + expect(modalHeader).toBeInTheDocument(); |
| 98 | + const modalBody = screen.getByText(messages.confirmEmailModalBody.defaultMessage); |
| 99 | + expect(modalBody).toBeInTheDocument(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('shows confirm image', async () => { |
| 104 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 105 | + await act(async () => { |
| 106 | + fireEvent.click(confirmButton); |
| 107 | + }); |
| 108 | + await waitFor(() => { |
| 109 | + const confirmImage = screen.getByRole('img', { name: messages.confirmEmailImageAlt.defaultMessage }); |
| 110 | + expect(confirmImage).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('shows confirm email button', async () => { |
| 115 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 116 | + await act(async () => { |
| 117 | + fireEvent.click(confirmButton); |
| 118 | + }); |
| 119 | + await waitFor(() => { |
| 120 | + const verifyButton = screen.getByRole('button', { name: messages.verifiedConfirmEmailButton.defaultMessage }); |
| 121 | + expect(verifyButton).toBeInTheDocument(); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it('calls resend email API when confirm now button is clicked', async () => { |
| 126 | + const confirmButton = await screen.findByRole('button', { name: messages.confirmNowButton.defaultMessage }); |
| 127 | + await act(async () => { |
| 128 | + fireEvent.click(confirmButton); |
| 129 | + }); |
| 130 | + await waitFor(() => expect(axiosMock.history.post.length).toBe(1)); |
| 131 | + expect(axiosMock.history.post[0].url).toBe(resendEmailUrl); |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | +}); |
0 commit comments