Skip to content

Commit d55330f

Browse files
committed
test: deprecate react-unit-test-utils 8/14
1 parent 0c1be3a commit d55330f

File tree

10 files changed

+225
-392
lines changed

10 files changed

+225
-392
lines changed

src/components/Assessment/__snapshots__/index.test.jsx.snap

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,59 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render, screen } from '@testing-library/react';
2+
import '@testing-library/jest-dom';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
24

35
import Assessment from './index';
4-
56
import { useAssessmentData } from './useAssessmentData';
67

8+
jest.unmock('@openedx/paragon');
9+
jest.unmock('react');
10+
jest.unmock('@edx/frontend-platform/i18n');
11+
712
jest.mock('./useAssessmentData', () => ({
813
useAssessmentData: jest.fn(),
914
}));
1015

11-
jest.mock('./EditableAssessment', () => 'EditableAssessment');
12-
jest.mock('./ReadonlyAssessment', () => 'ReadonlyAssessment');
16+
jest.mock('./EditableAssessment', () => () => (
17+
<div data-testid="editable-assessment" />
18+
));
19+
20+
jest.mock('./ReadonlyAssessment', () => () => (
21+
<div data-testid="readonly-assessment" />
22+
));
23+
24+
const renderWithIntl = (component) => render(<IntlProvider locale="en">{component}</IntlProvider>);
1325

1426
describe('<Assessment />', () => {
15-
it('renders the ReadonlyAssessment', () => {
16-
useAssessmentData.mockReturnValue({ initialized: true, hasSubmitted: true });
17-
const wrapper = shallow(<Assessment />);
18-
expect(wrapper.snapshot).toMatchSnapshot();
27+
beforeEach(() => {
28+
jest.clearAllMocks();
29+
});
30+
31+
it('renders the ReadonlyAssessment when assessment has been submitted', () => {
32+
useAssessmentData.mockReturnValue({
33+
initialized: true,
34+
hasSubmitted: true,
35+
});
36+
renderWithIntl(<Assessment />);
1937

20-
expect(wrapper.instance.findByType('ReadonlyAssessment')).toHaveLength(1);
21-
expect(wrapper.instance.findByType('EditableAssessment')).toHaveLength(0);
38+
expect(screen.getByTestId('readonly-assessment')).toBeInTheDocument();
39+
expect(screen.queryByTestId('editable-assessment')).not.toBeInTheDocument();
2240
});
23-
it('renders the EditableAssessment', () => {
24-
useAssessmentData.mockReturnValue({ initialized: true, hasSubmitted: false });
25-
const wrapper = shallow(<Assessment />);
26-
expect(wrapper.snapshot).toMatchSnapshot();
2741

28-
expect(wrapper.instance.findByType('ReadonlyAssessment')).toHaveLength(0);
29-
expect(wrapper.instance.findByType('EditableAssessment')).toHaveLength(1);
42+
it('renders the EditableAssessment when assessment has not been submitted', () => {
43+
useAssessmentData.mockReturnValue({
44+
initialized: true,
45+
hasSubmitted: false,
46+
});
47+
renderWithIntl(<Assessment />);
48+
49+
expect(screen.getByTestId('editable-assessment')).toBeInTheDocument();
50+
expect(screen.queryByTestId('readonly-assessment')).not.toBeInTheDocument();
3051
});
31-
it('renders nothing if not initialized', () => {
52+
53+
it('renders nothing when assessment data is not initialized', () => {
3254
useAssessmentData.mockReturnValue({ initialized: false });
33-
const wrapper = shallow(<Assessment />);
34-
expect(wrapper.snapshot).toMatchSnapshot();
55+
const { container } = renderWithIntl(<Assessment />);
3556

36-
expect(wrapper.isEmptyRender()).toBe(true);
57+
expect(container).toBeEmptyDOMElement();
3758
});
3859
});

src/components/ProgressBar/__snapshots__/index.test.jsx.snap

Lines changed: 0 additions & 111 deletions
This file was deleted.

src/components/ProgressBar/hooks.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ describe('useProgressStepData', () => {
5757
isXblockStep.mockReturnValueOnce(true);
5858
const result = useProgressStepData(props);
5959
result.onClick();
60-
expect(mockOpenModal).toHaveBeenCalledWith({ view: stepNames.self, title: stepNames.self });
60+
expect(mockOpenModal).toHaveBeenCalledWith({
61+
view: stepNames.self,
62+
title: stepNames.self,
63+
});
6164
});
6265

6366
it('should have href when is not xblock', () => {
6467
const result = useProgressStepData(props);
65-
expect(result.href).toBe(`/${stepRoutes[stepNames.self]}/courseId/xblockId`);
68+
expect(result.href).toBe(
69+
`/${stepRoutes[stepNames.self]}/courseId/xblockId`,
70+
);
6671
});
6772

6873
it('is complete when step state is done', () => {
@@ -99,12 +104,12 @@ describe('useProgressStepData', () => {
99104
expect(result.isEnabled).toBe(false);
100105
});
101106

102-
it('use effect grade from global state', () => {
107+
it('uses effective grade from global state', () => {
103108
const result = useProgressStepData(props);
104109
expect(result.myGrade).toBe(8);
105110
});
106111

107-
test('for peer step is not enabled when waiting for submissions', () => {
112+
it('disables peer step when waiting for submissions', () => {
108113
useStepInfo.mockReturnValue({
109114
peer: {
110115
numberOfReceivedAssessments: 0,
@@ -115,7 +120,7 @@ describe('useProgressStepData', () => {
115120
expect(result.isEnabled).toBe(false);
116121
});
117122

118-
test('for peer step is enabled iif peer is complete and no waiting for submission', () => {
123+
it('enables peer step when peer is complete and not waiting for submissions', () => {
119124
useStepInfo.mockReturnValue({
120125
peer: {
121126
numberOfReceivedAssessments: 1,
Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render, screen } from '@testing-library/react';
2+
import '@testing-library/jest-dom';
3+
import { IntlProvider } from '@edx/frontend-platform/i18n';
24

35
import {
46
useAssessmentStepOrder,
@@ -12,6 +14,12 @@ import { isXblockStep } from 'utils';
1214

1315
import ProgressBar from './index';
1416

17+
/* eslint-disable react/prop-types */
18+
19+
jest.unmock('@openedx/paragon');
20+
jest.unmock('react');
21+
jest.unmock('@edx/frontend-platform/i18n');
22+
1523
jest.mock('hooks/app', () => ({
1624
useAssessmentStepOrder: jest.fn(),
1725
useGlobalState: jest.fn(),
@@ -24,45 +32,54 @@ jest.mock('hooks/routing', () => ({
2432
jest.mock('utils', () => ({
2533
isXblockStep: jest.fn(),
2634
}));
27-
jest.mock('./ProgressStep', () => 'ProgressStep');
35+
36+
jest.mock('./ProgressStep', () => ({ step }) => (
37+
<div data-testid="progress-step" data-step={step} />
38+
));
39+
40+
const renderWithIntl = (component) => render(<IntlProvider locale="en">{component}</IntlProvider>);
2841

2942
describe('<ProgressBar />', () => {
3043
const props = {
3144
className: 'test-class',
3245
};
3346

3447
beforeEach(() => {
35-
useIsPageDataLoaded.mockReturnValue(true);
48+
jest.clearAllMocks();
49+
});
50+
51+
it('renders null when page data is not loaded', () => {
52+
useIsPageDataLoaded.mockReturnValue(false);
3653
useHasReceivedFinalGrade.mockReturnValue(false);
3754
useGlobalState.mockReturnValue({ activeStepName: stepNames.submission });
3855
useAssessmentStepOrder.mockReturnValue([]);
3956
useViewStep.mockReturnValue(stepNames.submission);
4057
isXblockStep.mockReturnValue(false);
41-
});
4258

43-
it('renders null when page data is not loaded', () => {
44-
useIsPageDataLoaded.mockReturnValueOnce(false);
45-
const wrapper = shallow(<ProgressBar {...props} />);
46-
expect(wrapper.snapshot).toMatchSnapshot();
47-
48-
expect(wrapper.isEmptyRender()).toBe(true);
49-
});
50-
51-
it('renders at least 2 steps: submission and done', () => {
52-
const wrapper = shallow(<ProgressBar {...props} />);
53-
expect(wrapper.snapshot).toMatchSnapshot();
54-
expect(wrapper.instance.findByType('ProgressStep')).toHaveLength(2);
59+
renderWithIntl(<ProgressBar {...props} />);
60+
expect(screen.queryByRole('navigation')).not.toBeInTheDocument();
5561
});
5662

57-
it('renders all steps', () => {
58-
isXblockStep.mockReturnValueOnce(true);
59-
useAssessmentStepOrder.mockReturnValueOnce([
63+
it('renders all steps when xblock step with assessment order', () => {
64+
useIsPageDataLoaded.mockReturnValue(true);
65+
useHasReceivedFinalGrade.mockReturnValue(false);
66+
useGlobalState.mockReturnValue({ activeStepName: stepNames.submission });
67+
useAssessmentStepOrder.mockReturnValue([
6068
stepNames.studentTraining,
6169
stepNames.self,
6270
stepNames.peer,
6371
]);
64-
const wrapper = shallow(<ProgressBar {...props} />);
65-
expect(wrapper.snapshot).toMatchSnapshot();
66-
expect(wrapper.instance.findByType('ProgressStep')).toHaveLength(5);
72+
useViewStep.mockReturnValue(stepNames.submission);
73+
isXblockStep.mockReturnValue(true);
74+
75+
renderWithIntl(<ProgressBar {...props} />);
76+
77+
const steps = screen.getAllByTestId('progress-step');
78+
expect(steps).toHaveLength(5);
79+
expect(steps[0]).toHaveAttribute('data-step', stepNames.submission);
80+
expect(steps[1]).toHaveAttribute('data-step', stepNames.studentTraining);
81+
expect(steps[2]).toHaveAttribute('data-step', stepNames.self);
82+
expect(steps[3]).toHaveAttribute('data-step', stepNames.peer);
83+
expect(steps[4]).toHaveAttribute('data-step', stepNames.done);
6784
});
6885
});

0 commit comments

Comments
 (0)