|
| 1 | +import { fireEvent, render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { IntlProvider } from 'react-intl'; |
| 4 | +import { Context as ResponsiveContext } from 'react-responsive'; |
| 5 | +import { MemoryRouter } from 'react-router'; |
| 6 | + |
| 7 | +import { initializeMockApp } from '@edx/frontend-platform'; |
| 8 | +import { AppProvider } from '@edx/frontend-platform/react'; |
| 9 | + |
| 10 | +import { initializeStore } from '../../store'; |
| 11 | +import EmptyPosts from '../empty-posts/EmptyPosts'; |
| 12 | +import messages from '../messages'; |
| 13 | +import { sendEmailForAccountActivation } from '../posts/data/api'; |
| 14 | + |
| 15 | +let store; |
| 16 | +const courseId = 'course-v1:edX+DemoX+Demo_Course'; |
| 17 | + |
| 18 | +jest.mock('../posts/data/api', () => ({ |
| 19 | + sendEmailForAccountActivation: jest.fn(), |
| 20 | +})); |
| 21 | + |
| 22 | +function renderComponent(location = `/${courseId}/`) { |
| 23 | + return render( |
| 24 | + <IntlProvider locale="en"> |
| 25 | + <ResponsiveContext.Provider value={{ width: 1280 }}> |
| 26 | + <AppProvider store={store} wrapWithRouter={false}> |
| 27 | + <MemoryRouter initialEntries={[location]}> |
| 28 | + <EmptyPosts subTitleMessage={messages.emptyMyPosts} /> |
| 29 | + </MemoryRouter> |
| 30 | + </AppProvider> |
| 31 | + </ResponsiveContext.Provider> |
| 32 | + </IntlProvider>, |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +describe('EmptyPage', () => { |
| 37 | + beforeEach(async () => { |
| 38 | + initializeMockApp({ |
| 39 | + authenticatedUser: { |
| 40 | + userId: 3, |
| 41 | + username: 'abc123', |
| 42 | + administrator: true, |
| 43 | + roles: [], |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + store = initializeStore(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should open the confirmation link dialogue box.', async () => { |
| 51 | + renderComponent(`/${courseId}/my-posts/`); |
| 52 | + |
| 53 | + const addPostButton = screen.getByRole('button', { name: 'Add a post' }); |
| 54 | + await userEvent.click(addPostButton); |
| 55 | + |
| 56 | + expect(screen.queryByText('Send confirmation link')).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('dispatches sendAccountActivationEmail on confirm', async () => { |
| 60 | + sendEmailForAccountActivation.mockResolvedValue({ success: true }); |
| 61 | + renderComponent(`/${courseId}/my-posts/`); |
| 62 | + |
| 63 | + const addPostButton = screen.getByRole('button', { name: 'Add a post' }); |
| 64 | + await userEvent.click(addPostButton); |
| 65 | + const confirmButton = screen.getByText('Send confirmation link'); |
| 66 | + fireEvent.click(confirmButton); |
| 67 | + expect(sendEmailForAccountActivation).toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should close the confirmation dialogue box.', async () => { |
| 71 | + renderComponent(`/${courseId}/my-posts/`); |
| 72 | + |
| 73 | + const addPostButton = screen.getByRole('button', { name: 'Add a post' }); |
| 74 | + await userEvent.click(addPostButton); |
| 75 | + const confirmButton = screen.getByText('Close'); |
| 76 | + fireEvent.click(confirmButton); |
| 77 | + |
| 78 | + expect(sendEmailForAccountActivation).toHaveBeenCalled(); |
| 79 | + |
| 80 | + expect(screen.queryByText('Close')).not.toBeInTheDocument(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments