|
| 1 | +import { screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import CohortsPage from './CohortsPage'; |
| 4 | +import { useCohorts, useCohortStatus, useToggleCohorts } from './data/apiHook'; |
| 5 | +import { renderWithIntl } from '../testUtils'; |
| 6 | +import messages from './messages'; |
| 7 | + |
| 8 | +jest.mock('react-router-dom', () => ({ |
| 9 | + ...jest.requireActual('react-router-dom'), |
| 10 | + useParams: () => ({ courseId: 'course-v1:edX+Test+2024' }), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('./data/apiHook', () => ({ |
| 14 | + useCohorts: jest.fn(), |
| 15 | + useCohortStatus: jest.fn(), |
| 16 | + useToggleCohorts: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +describe('CohortsPage', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('renders cohorts list and add button when cohorts exist', () => { |
| 25 | + (useCohorts as jest.Mock).mockReturnValue({ data: [{ id: '1', name: 'Cohort 1' }] }); |
| 26 | + (useCohortStatus as jest.Mock).mockReturnValue({ data: { isCohorted: true } }); |
| 27 | + (useToggleCohorts as jest.Mock).mockReturnValue({ mutate: jest.fn() }); |
| 28 | + |
| 29 | + renderWithIntl(<CohortsPage />); |
| 30 | + expect(screen.getByText(messages.cohortsTitle.defaultMessage)).toBeInTheDocument(); |
| 31 | + expect(screen.getByRole('option', { name: 'Cohort 1' })).toBeInTheDocument(); |
| 32 | + expect(screen.getByText(`+ ${messages.addCohort.defaultMessage}`)).toBeInTheDocument(); |
| 33 | + }); |
| 34 | + |
| 35 | + it('renders no cohorts message and enable button when no cohorts', () => { |
| 36 | + (useCohorts as jest.Mock).mockReturnValue({ data: [] }); |
| 37 | + (useCohortStatus as jest.Mock).mockReturnValue({ data: { isCohorted: false } }); |
| 38 | + (useToggleCohorts as jest.Mock).mockReturnValue({ mutate: jest.fn() }); |
| 39 | + |
| 40 | + renderWithIntl(<CohortsPage />); |
| 41 | + expect(screen.getByText(messages.noCohortsMessage.defaultMessage)).toBeInTheDocument(); |
| 42 | + expect(screen.getByRole('button', { name: messages.enableCohorts.defaultMessage })).toBeInTheDocument(); |
| 43 | + expect(screen.getByRole('link', { name: messages.learnMore.defaultMessage })).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('calls enableCohortsMutate when enable button is clicked', async () => { |
| 47 | + const enableMock = jest.fn(); |
| 48 | + (useCohorts as jest.Mock).mockReturnValue({ data: [] }); |
| 49 | + (useCohortStatus as jest.Mock).mockReturnValue({ data: { isCohorted: false } }); |
| 50 | + (useToggleCohorts as jest.Mock).mockReturnValue({ mutate: enableMock }); |
| 51 | + |
| 52 | + renderWithIntl(<CohortsPage />); |
| 53 | + const user = userEvent.setup(); |
| 54 | + await user.click(screen.getByRole('button', { name: messages.enableCohorts.defaultMessage })); |
| 55 | + expect(enableMock).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('opens and closes the disable cohorts modal', async () => { |
| 59 | + (useCohorts as jest.Mock).mockReturnValue({ data: [{ id: '1', name: 'Cohort 1' }] }); |
| 60 | + (useCohortStatus as jest.Mock).mockReturnValue({ data: { isCohorted: true } }); |
| 61 | + (useToggleCohorts as jest.Mock).mockReturnValue({ mutate: jest.fn() }); |
| 62 | + |
| 63 | + renderWithIntl(<CohortsPage />); |
| 64 | + const user = userEvent.setup(); |
| 65 | + expect(screen.queryByRole('dialog')).not.toBeInTheDocument(); |
| 66 | + await user.click(screen.getByRole('button', { name: messages.disableCohorts.defaultMessage })); |
| 67 | + expect(screen.getByRole('dialog', { name: messages.disableCohorts.defaultMessage })).toBeInTheDocument(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('calls disableCohortsMutate and closes modal on confirm', async () => { |
| 71 | + const disableMock = jest.fn(); |
| 72 | + (useCohorts as jest.Mock).mockReturnValue({ data: [{ id: '1', name: 'Cohort 1' }] }); |
| 73 | + (useCohortStatus as jest.Mock).mockReturnValue({ data: { isCohorted: true } }); |
| 74 | + (useToggleCohorts as jest.Mock).mockReturnValue({ mutate: disableMock }); |
| 75 | + |
| 76 | + renderWithIntl(<CohortsPage />); |
| 77 | + const user = userEvent.setup(); |
| 78 | + await user.click(screen.getByRole('button', { name: messages.disableCohorts.defaultMessage })); |
| 79 | + await user.click(screen.getByRole('button', { name: messages.disableLabel.defaultMessage })); |
| 80 | + expect(disableMock).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments