|
| 1 | +import { screen, fireEvent } from '@testing-library/react'; |
| 2 | +import ManageLearners from './ManageLearners'; |
| 3 | +import { useParams } from 'react-router-dom'; |
| 4 | +import { useAddLearnersToCohort } from '../data/apiHook'; |
| 5 | +import { useCohortContext } from './CohortContext'; |
| 6 | +import messages from '../messages'; |
| 7 | +import { renderWithIntl } from '../../testUtils'; |
| 8 | + |
| 9 | +jest.mock('react-router-dom', () => ({ |
| 10 | + useParams: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('../data/apiHook', () => ({ |
| 14 | + useAddLearnersToCohort: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +jest.mock('./CohortContext', () => ({ |
| 18 | + useCohortContext: jest.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +describe('ManageLearners', () => { |
| 22 | + const mutateMock = jest.fn(); |
| 23 | + |
| 24 | + beforeEach(() => { |
| 25 | + (useParams as jest.Mock).mockReturnValue({ courseId: 'course-v1:edX+Test+2024' }); |
| 26 | + (useCohortContext as jest.Mock).mockReturnValue({ selectedCohort: { id: 123 } }); |
| 27 | + (useAddLearnersToCohort as jest.Mock).mockReturnValue({ mutate: mutateMock }); |
| 28 | + mutateMock.mockReset(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('render all static texts', () => { |
| 32 | + renderWithIntl(<ManageLearners />); |
| 33 | + expect(screen.getByRole('heading', { name: messages.addLearnersTitle.defaultMessage })).toBeInTheDocument(); |
| 34 | + expect(screen.getByText(messages.addLearnersSubtitle.defaultMessage)).toBeInTheDocument(); |
| 35 | + expect(screen.getByText(messages.addLearnersInstructions.defaultMessage)).toBeInTheDocument(); |
| 36 | + expect(screen.getByPlaceholderText(messages.learnersExample.defaultMessage)).toBeInTheDocument(); |
| 37 | + expect(screen.getByText(messages.addLearnersFootnote.defaultMessage)).toBeInTheDocument(); |
| 38 | + expect(screen.getByRole('button', { name: /\+ Add Learners/i })).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('updates textarea value and calls mutate on button click', () => { |
| 42 | + renderWithIntl(<ManageLearners />); |
| 43 | + const textarea = screen.getByPlaceholderText(messages.learnersExample.defaultMessage); |
| 44 | + fireEvent.change(textarea, { target: { value: '[email protected],[email protected]' } }); |
| 45 | + fireEvent.click(screen.getByRole('button', { name: /\+ Add Learners/i })); |
| 46 | + expect(mutateMock).toHaveBeenCalledWith( |
| 47 | + |
| 48 | + expect.objectContaining({ |
| 49 | + onSuccess: expect.any(Function), |
| 50 | + onError: expect.any(Function), |
| 51 | + }) |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('handles empty input gracefully', () => { |
| 56 | + renderWithIntl(<ManageLearners />); |
| 57 | + fireEvent.click(screen.getByRole('button', { name: /\+ Add Learners/i })); |
| 58 | + expect(mutateMock).toHaveBeenCalledWith( |
| 59 | + [''], |
| 60 | + expect.objectContaining({ |
| 61 | + onSuccess: expect.any(Function), |
| 62 | + onError: expect.any(Function), |
| 63 | + }) |
| 64 | + ); |
| 65 | + }); |
| 66 | + |
| 67 | + it('calls onError if mutate fails', () => { |
| 68 | + renderWithIntl(<ManageLearners />); |
| 69 | + const textarea = screen.getByPlaceholderText(messages.learnersExample.defaultMessage); |
| 70 | + fireEvent.change(textarea, { target: { value: '[email protected]' } }); |
| 71 | + fireEvent.click(screen.getByRole('button', { name: /\+ Add Learners/i })); |
| 72 | + |
| 73 | + const callArgs = mutateMock.mock.calls[0][1]; |
| 74 | + const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); |
| 75 | + callArgs.onError('error!'); |
| 76 | + expect(consoleErrorSpy).toHaveBeenCalledWith('error!'); |
| 77 | + consoleErrorSpy.mockRestore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('uses default cohort id 0 if selectedCohort is missing', () => { |
| 81 | + (useCohortContext as jest.Mock).mockReturnValue({ selectedCohort: undefined }); |
| 82 | + renderWithIntl(<ManageLearners />); |
| 83 | + fireEvent.click(screen.getByRole('button', { name: /\+ Add Learners/i })); |
| 84 | + expect(mutateMock).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | +}); |
0 commit comments