Skip to content

Commit 2d27ce0

Browse files
committed
test: deprecate react-unit-test-utils 12/14
1 parent baaf4e6 commit 2d27ce0

File tree

10 files changed

+434
-499
lines changed

10 files changed

+434
-499
lines changed
Lines changed: 86 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
24

35
import ReadOnlyAssessment from './ReadOnlyAssessment';
46

5-
jest.mock('./CollapsibleAssessment', () => 'CollapsibleAssessment');
6-
jest.mock('./AssessmentCriteria', () => 'AssessmentCriteria');
7+
jest.unmock('@openedx/paragon');
8+
jest.unmock('react');
9+
jest.unmock('@edx/frontend-platform/i18n');
10+
11+
/* eslint-disable react/prop-types */
12+
jest.mock(
13+
'./CollapsibleAssessment',
14+
() => ({
15+
children, stepLabel, stepScore, defaultOpen,
16+
}) => (
17+
<section aria-label="Collapsible Assessment">
18+
<div aria-label="Assessment Properties">
19+
stepLabel: {stepLabel || 'none'}, stepScore:{' '}
20+
{stepScore ? `${stepScore.earned}/${stepScore.total}` : 'none'},
21+
defaultOpen: {defaultOpen?.toString() || 'false'}
22+
</div>
23+
{children}
24+
</section>
25+
),
26+
);
27+
28+
jest.mock('./AssessmentCriteria', () => ({ stepLabel, ...props }) => (
29+
<article aria-label={`Assessment Criteria for ${stepLabel || 'unknown'}`}>
30+
AssessmentCriteria - stepLabel: {stepLabel || 'none'}
31+
<div aria-label="Criteria Properties">{JSON.stringify(props)}</div>
32+
</article>
33+
));
734

835
describe('<ReadOnlyAssessment />', () => {
936
const props = {
@@ -15,16 +42,33 @@ describe('<ReadOnlyAssessment />', () => {
1542
earned: 5,
1643
total: 10,
1744
},
45+
stepLabel: 'Test Step',
46+
defaultOpen: false,
1847
};
1948

20-
it('render with assessment', () => {
21-
const wrapper = shallow(<ReadOnlyAssessment {...props} />);
22-
expect(wrapper.snapshot).toMatchSnapshot();
49+
it('renders with single assessment', () => {
50+
render(<ReadOnlyAssessment {...props} />);
51+
52+
expect(screen.getByLabelText('Collapsible Assessment')).toBeInTheDocument();
2353

24-
expect(wrapper.instance.findByType('AssessmentCriteria').length).toBe(1);
54+
const assessmentProps = screen.getByLabelText('Assessment Properties');
55+
expect(assessmentProps).toHaveTextContent('stepLabel: Test Step');
56+
expect(assessmentProps).toHaveTextContent('stepScore: 5/10');
57+
expect(assessmentProps).toHaveTextContent('defaultOpen: false');
58+
59+
const assessmentCriteria = screen.getAllByLabelText(
60+
/Assessment Criteria for/,
61+
);
62+
expect(assessmentCriteria).toHaveLength(1);
63+
expect(assessmentCriteria[0]).toHaveTextContent(
64+
'AssessmentCriteria - stepLabel: Test Step',
65+
);
66+
67+
const criteriaProps = screen.getByLabelText('Criteria Properties');
68+
expect(criteriaProps).toHaveTextContent('"abc":"def"');
2569
});
2670

27-
it('render with assessments', () => {
71+
it('renders with multiple assessments', () => {
2872
const assessments = [
2973
{
3074
abc: 'def',
@@ -33,14 +77,43 @@ describe('<ReadOnlyAssessment />', () => {
3377
ghi: 'jkl',
3478
},
3579
];
36-
const wrapper = shallow(<ReadOnlyAssessment {...props} assessments={assessments} />);
37-
expect(wrapper.snapshot).toMatchSnapshot();
80+
render(<ReadOnlyAssessment {...props} assessments={assessments} />);
81+
82+
expect(screen.getByLabelText('Collapsible Assessment')).toBeInTheDocument();
3883

39-
expect(wrapper.instance.findByType('AssessmentCriteria').length).toBe(2);
84+
const assessmentProps = screen.getByLabelText('Assessment Properties');
85+
expect(assessmentProps).toHaveTextContent('stepLabel: Test Step');
86+
expect(assessmentProps).toHaveTextContent('stepScore: 5/10');
87+
88+
const assessmentCriteria = screen.getAllByLabelText(
89+
/Assessment Criteria for/,
90+
);
91+
expect(assessmentCriteria).toHaveLength(2);
92+
93+
expect(screen.getByText('Test Step 1:')).toBeInTheDocument();
94+
expect(screen.getByText('Test Step 2:')).toBeInTheDocument();
95+
96+
const criteriaProps = screen.getAllByLabelText('Criteria Properties');
97+
expect(criteriaProps[0]).toHaveTextContent('"abc":"def"');
98+
expect(criteriaProps[1]).toHaveTextContent('"ghi":"jkl"');
4099
});
41100

42101
it('renders without props', () => {
43-
const wrapper = shallow(<ReadOnlyAssessment />);
44-
expect(wrapper.snapshot).toMatchSnapshot();
102+
render(<ReadOnlyAssessment />);
103+
104+
expect(screen.getByLabelText('Collapsible Assessment')).toBeInTheDocument();
105+
106+
const assessmentProps = screen.getByLabelText('Assessment Properties');
107+
expect(assessmentProps).toHaveTextContent('stepLabel: none');
108+
expect(assessmentProps).toHaveTextContent('stepScore: none');
109+
expect(assessmentProps).toHaveTextContent('defaultOpen: false');
110+
111+
const assessmentCriteria = screen.getAllByLabelText(
112+
/Assessment Criteria for/,
113+
);
114+
expect(assessmentCriteria).toHaveLength(1);
115+
expect(assessmentCriteria[0]).toHaveTextContent(
116+
'AssessmentCriteria - stepLabel: none',
117+
);
45118
});
46119
});

src/components/Assessment/ReadonlyAssessment/__snapshots__/ReadOnlyAssessment.test.jsx.snap

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

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

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

src/components/Assessment/ReadonlyAssessment/index.test.jsx

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,68 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
24

5+
import { useHasSubmitted, useRefreshPageData } from 'hooks/app';
6+
import { useSubmittedAssessment } from 'hooks/assessment';
37
import ReadOnlyAssessmentContainer from '.';
48

9+
jest.unmock('@openedx/paragon');
10+
jest.unmock('react');
11+
jest.unmock('@edx/frontend-platform/i18n');
12+
13+
const mockRefreshPageData = jest.fn();
14+
515
jest.mock('hooks/app', () => ({
616
useHasSubmitted: jest.fn(),
7-
useRefreshPageData: jest.fn(),
17+
useRefreshPageData: jest.fn(() => mockRefreshPageData),
818
}));
919
jest.mock('hooks/assessment', () => ({
1020
useSubmittedAssessment: jest.fn(),
1121
}));
12-
jest.mock('./ReadOnlyAssessment', () => 'ReadOnlyAssessment');
22+
23+
/* eslint-disable react/prop-types */
24+
jest.mock(
25+
'./ReadOnlyAssessment',
26+
() => ({
27+
assessment, step, stepScore, stepLabel, defaultOpen, ...rest
28+
}) => (
29+
<div>
30+
<h2>ReadOnly Assessment</h2>
31+
{assessment && <div data-assessment="true">Assessment Data</div>}
32+
{step && <div data-step="true">Step: {step}</div>}
33+
{stepScore && (
34+
<div data-stepscore="true">
35+
Score: {stepScore.earned}/{stepScore.total}
36+
</div>
37+
)}
38+
{stepLabel && <div data-steplabel="true">Label: {stepLabel}</div>}
39+
{defaultOpen !== undefined && (
40+
<div data-defaultopen="true">
41+
Default Open: {defaultOpen.toString()}
42+
</div>
43+
)}
44+
<div aria-label="Props Data">
45+
{JSON.stringify({
46+
assessment,
47+
step,
48+
stepScore,
49+
stepLabel,
50+
defaultOpen,
51+
...rest,
52+
})}
53+
</div>
54+
</div>
55+
),
56+
);
1357

1458
describe('<ReadOnlyAssessmentContainer />', () => {
59+
beforeEach(() => {
60+
useHasSubmitted.mockReturnValue(false);
61+
useRefreshPageData.mockReturnValue(mockRefreshPageData);
62+
useSubmittedAssessment.mockReturnValue(null);
63+
mockRefreshPageData.mockClear();
64+
});
65+
1566
const props = {
1667
assessment: {
1768
abc: 'def',
@@ -29,15 +80,60 @@ describe('<ReadOnlyAssessmentContainer />', () => {
2980
earned: 5,
3081
total: 10,
3182
},
83+
stepLabel: 'Test Label',
3284
defaultOpen: true,
3385
};
34-
it('renders the component', () => {
35-
const wrapper = shallow(<ReadOnlyAssessmentContainer {...props} />);
36-
expect(wrapper.snapshot).toMatchSnapshot();
86+
87+
it('renders the component with props', () => {
88+
render(<ReadOnlyAssessmentContainer {...props} />);
89+
90+
expect(
91+
screen.getByRole('heading', { name: 'ReadOnly Assessment' }),
92+
).toBeInTheDocument();
93+
expect(screen.getByText('Assessment Data')).toBeInTheDocument();
94+
expect(screen.getByText('Step: Step')).toBeInTheDocument();
95+
expect(screen.getByText('Score: 5/10')).toBeInTheDocument();
96+
expect(screen.getByText('Label: Test Label')).toBeInTheDocument();
97+
expect(screen.getByText('Default Open: true')).toBeInTheDocument();
3798
});
3899

39100
it('renders without props', () => {
40-
const wrapper = shallow(<ReadOnlyAssessmentContainer />);
41-
expect(wrapper.snapshot).toMatchSnapshot();
101+
render(<ReadOnlyAssessmentContainer />);
102+
103+
expect(
104+
screen.getByRole('heading', { name: 'ReadOnly Assessment' }),
105+
).toBeInTheDocument();
106+
expect(screen.queryByText('Assessment Data')).not.toBeInTheDocument();
107+
expect(screen.queryByText(/Step:/)).not.toBeInTheDocument();
108+
expect(screen.queryByText(/Score:/)).not.toBeInTheDocument();
109+
expect(screen.queryByText(/Label:/)).not.toBeInTheDocument();
110+
});
111+
112+
it('passes submitted assessment when user has submitted', () => {
113+
const submittedAssessment = { submitted: 'data' };
114+
useHasSubmitted.mockReturnValue(true);
115+
useSubmittedAssessment.mockReturnValue(submittedAssessment);
116+
117+
render(<ReadOnlyAssessmentContainer {...props} />);
118+
119+
expect(screen.getByLabelText('Props Data')).toHaveTextContent(
120+
'"submitted":"data"',
121+
);
122+
});
123+
124+
it('calls refreshPageData when hasSubmitted is true', () => {
125+
useHasSubmitted.mockReturnValue(true);
126+
127+
render(<ReadOnlyAssessmentContainer {...props} />);
128+
129+
expect(mockRefreshPageData).toHaveBeenCalled();
130+
});
131+
132+
it('does not call refreshPageData when hasSubmitted is false', () => {
133+
useHasSubmitted.mockReturnValue(false);
134+
135+
render(<ReadOnlyAssessmentContainer {...props} />);
136+
137+
expect(mockRefreshPageData).not.toHaveBeenCalled();
42138
});
43139
});

0 commit comments

Comments
 (0)