diff --git a/src/pages/ExamsPage/__snapshots__/index.test.jsx.snap b/src/pages/ExamsPage/__snapshots__/index.test.jsx.snap index 3495e2a..c507186 100644 --- a/src/pages/ExamsPage/__snapshots__/index.test.jsx.snap +++ b/src/pages/ExamsPage/__snapshots__/index.test.jsx.snap @@ -392,7 +392,7 @@ Object { class="pgn__data-table-cell-wrap" role="cell" > - username + username1 4/5/2023 + + Second Review Required + + +
+ +
+ + +
+ +
+ + + + + username2 + + + 60 + + + Proctored + + + 4/5/2023 + + + 4/5/2023 + - username + username1 4/5/2023 + + Second Review Required + + +
+ +
+ + +
+ +
+ + + + + username2 + + + 60 + + + Proctored + + + 4/5/2023 + + + 4/5/2023 + ({ useDeleteExamAttempt: jest.fn(), useModifyExamAttempt: jest.fn(), + useExamsData: jest.fn(), })); describe('AttemptList', () => { beforeEach(() => { hooks.useDeleteExamAttempt.mockReturnValue(jest.fn()); hooks.useModifyExamAttempt.mockReturnValue(jest.fn()); + hooks.useExamsData.mockReturnValue(testUtils.defaultExamsData); }); - const defaultAttemptsData = [ - { - exam_name: 'Exam 1', - username: 'username1', - time_limit: 60, - exam_type: 'timed', - started_at: '2023-04-05T19:27:16.000000Z', - completed_at: '2023-04-05T19:27:17.000000Z', - status: 'second_review_required', - attempt_id: 0, - severity: 1.0, - submission_reason: 'Submitted by user', - }, - { - exam_name: 'Exam 2', - username: 'username2', - time_limit: 60, - exam_type: 'proctored', - started_at: '2023-04-05T19:37:16.000000Z', - completed_at: '2023-04-05T19:37:17.000000Z', - status: 'submitted', - attempt_id: 1, - }, - ]; it('Test that the AttemptList matches snapshot', () => { - expect(render()).toMatchSnapshot(); + expect(render()).toMatchSnapshot(); }); it('Data appears in data table as expected', () => { - render(); - defaultAttemptsData.forEach((attempt, index) => { + render(); + testUtils.defaultAttemptsData.attemptsList.forEach((attempt, index) => { // Expect a row to be in the table for each attempt in the data (with respect to key/values present) if (attempt.severity && attempt.submission_reason) { expect(screen.getAllByRole('row', { @@ -78,7 +56,7 @@ describe('AttemptList', () => { }); }); it('filtering by status displays the correct entry', () => { - render(); + render(); // Get the 2nd row of data which has the values of defaultAttemptsData[1] const secondRow = screen.getAllByRole('row')[2]; screen.getByText('Filters').click(); @@ -89,7 +67,7 @@ describe('AttemptList', () => { expect(secondRow).not.toBeInTheDocument(); }); it('filtering by username displays the correct entry', () => { - render(); + render(); // Get the 2nd row of data which has the values of defaultAttemptsData[1] const secondRow = screen.getAllByRole('row')[2]; const searchInput = screen.getByLabelText('Search username'); diff --git a/src/pages/ExamsPage/components/ReviewExamAttemptButton.jsx b/src/pages/ExamsPage/components/ReviewExamAttemptButton.jsx index fa4a488..09a4f8a 100644 --- a/src/pages/ExamsPage/components/ReviewExamAttemptButton.jsx +++ b/src/pages/ExamsPage/components/ReviewExamAttemptButton.jsx @@ -6,7 +6,7 @@ import { import { useIntl } from '@edx/frontend-platform/i18n'; import { Info, Warning } from '@edx/paragon/icons'; import * as constants from 'data/constants'; -import { useModifyExamAttempt } from '../hooks'; +import { useExamsData, useModifyExamAttempt } from '../hooks'; import messages from '../messages'; import { getLaunchUrlByExamId, getMessageLabelForStatus } from '../utils'; @@ -27,6 +27,7 @@ const ReviewExamAttemptButton = ({ }) => { const [isOpen, open, close] = useToggle(false); const modifyExamAttempt = useModifyExamAttempt(); + const { currentExam } = useExamsData(); const { formatMessage } = useIntl(); const getButton = (status) => { @@ -96,7 +97,7 @@ const ReviewExamAttemptButton = ({

{formatMessage(messages.ReviewExamAttemptModalBodySessionInfo)}

- diff --git a/src/pages/ExamsPage/components/ReviewExamAttemptButton.test.jsx b/src/pages/ExamsPage/components/ReviewExamAttemptButton.test.jsx index 79e0ecb..16af672 100644 --- a/src/pages/ExamsPage/components/ReviewExamAttemptButton.test.jsx +++ b/src/pages/ExamsPage/components/ReviewExamAttemptButton.test.jsx @@ -2,16 +2,18 @@ import { render, screen } from '@testing-library/react'; import * as constants from 'data/constants'; import ReviewExamAttemptButton from './ReviewExamAttemptButton'; +import * as testUtils from '../../../testUtils'; import * as hooks from '../hooks'; jest.mock('../hooks', () => ({ useModifyExamAttempt: jest.fn(), + useExamsData: jest.fn(), })); const mockMakeNetworkRequest = jest.fn(); -// nomally mocked for unit tests but required for rendering/snapshots +// normally mocked for unit tests but required for rendering/snapshots jest.unmock('react'); const reviewButton = (status = constants.ExamAttemptStatus.second_review_required) => ( @@ -29,6 +31,7 @@ describe('ReviewExamAttemptButton', () => { beforeEach(() => { jest.restoreAllMocks(); hooks.useModifyExamAttempt.mockReturnValue(mockMakeNetworkRequest); + hooks.useExamsData.mockReturnValue(testUtils.defaultExamsData); }); it('Test that the ReviewExamAttemptButton matches snapshot', () => { expect(render(reviewButton())).toMatchSnapshot(); diff --git a/src/pages/ExamsPage/index.test.jsx b/src/pages/ExamsPage/index.test.jsx index cadde96..475cebd 100644 --- a/src/pages/ExamsPage/index.test.jsx +++ b/src/pages/ExamsPage/index.test.jsx @@ -2,8 +2,9 @@ import { render, screen } from '@testing-library/react'; import ExamsPage from '.'; import * as hooks from './hooks'; +import * as testUtils from '../../testUtils'; -// nomally mocked for unit tests but required for rendering/snapshots +// normally mocked for unit tests but required for rendering/snapshots jest.unmock('react'); jest.mock('./hooks', () => ({ @@ -16,34 +17,15 @@ jest.mock('./hooks', () => ({ })); describe('ExamsPage', () => { - const defaultExamsData = { - examsList: [ - { id: 1, name: 'exam1' }, - ], - currentExam: { id: 1, name: 'exam1' }, - setCurrentExam: jest.fn(), - }; - const defaultAttemptsData = { - attemptsList: [{ - exam_name: 'Exam 1', - username: 'username', - time_limit: 60, - exam_type: 'timed', - started_at: '2023-04-05T19:27:16.000000Z', - completed_at: '2023-04-05T19:27:17.000000Z', - status: 'submitted', - attempt_id: 0, - }], - }; beforeAll(() => { - hooks.useExamAttemptsData.mockReturnValue(defaultAttemptsData); + hooks.useExamAttemptsData.mockReturnValue(testUtils.defaultAttemptsData); }); describe('snapshots', () => { test('exams and attempts loaded', () => { // temporary, this won't fire on useEffect once we have an exam selection handler hooks.useFetchExamAttempts.mockReturnValue(jest.fn()); - hooks.useExamsData.mockReturnValue(defaultExamsData); + hooks.useExamsData.mockReturnValue(testUtils.defaultExamsData); expect(render()).toMatchSnapshot(); }); }); diff --git a/src/testUtils.js b/src/testUtils.js index 8e82f99..71264f4 100644 --- a/src/testUtils.js +++ b/src/testUtils.js @@ -18,3 +18,37 @@ export const formatMessage = (msg, values) => { }); return message; }; + +export const defaultExamsData = { + examsList: [ + { id: 1, name: 'exam1' }, + ], + currentExam: { id: 1, name: 'exam1' }, + setCurrentExam: jest.fn(), +}; + +export const defaultAttemptsData = { + attemptsList: + [{ + exam_name: 'Exam 1', + username: 'username1', + time_limit: 60, + exam_type: 'timed', + started_at: '2023-04-05T19:27:16.000000Z', + completed_at: '2023-04-05T19:27:17.000000Z', + status: 'second_review_required', + attempt_id: 0, + severity: 1.0, + submission_reason: 'Submitted by user', + }, + { + exam_name: 'Exam 2', + username: 'username2', + time_limit: 60, + exam_type: 'proctored', + started_at: '2023-04-05T19:37:16.000000Z', + completed_at: '2023-04-05T19:37:17.000000Z', + status: 'submitted', + attempt_id: 1, + }], +};