Skip to content

Commit 2bd48e6

Browse files
committed
test: deprecate react-unit-test-utils 9/14
1 parent 0c1be3a commit 2bd48e6

File tree

10 files changed

+343
-844
lines changed

10 files changed

+343
-844
lines changed
Lines changed: 90 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,137 @@
1-
import { shallow } from '@edx/react-unit-test-utils';
1+
import { render, screen, fireEvent } from '@testing-library/react';
2+
import '@testing-library/jest-dom';
23

3-
import { useExitWithoutSavingAction, useSubmitAssessmentAction } from 'hooks/actions';
4+
import {
5+
useExitWithoutSavingAction,
6+
useSubmitAssessmentAction,
7+
} from 'hooks/actions';
48
import AssessmentActions from './AssessmentActions';
59

10+
/* eslint-disable react/prop-types */
11+
12+
jest.unmock('@openedx/paragon');
13+
jest.unmock('react');
14+
jest.unmock('@edx/frontend-platform/i18n');
15+
616
jest.mock('hooks/actions', () => ({
717
useExitWithoutSavingAction: jest.fn(),
818
useSubmitAssessmentAction: jest.fn(),
919
}));
1020

11-
jest.mock('components/ActionButton', () => 'ActionButton');
12-
jest.mock('components/ConfirmDialog', () => 'ConfirmDialog');
21+
jest.mock(
22+
'components/ActionButton',
23+
() => ({
24+
children, variant, onClick, ...props
25+
}) => (
26+
<button
27+
type="button"
28+
data-testid={`action-button-${variant}`}
29+
onClick={onClick}
30+
{...props}
31+
>
32+
{children}
33+
</button>
34+
),
35+
);
36+
37+
jest.mock(
38+
'components/ConfirmDialog',
39+
() => ({ title, onConfirm, ...props }) => (
40+
<button
41+
type="button"
42+
data-testid={`confirm-dialog-${title}`}
43+
onClick={onConfirm}
44+
onKeyDown={(e) => e.key === 'Enter' && onConfirm()}
45+
{...props}
46+
/>
47+
),
48+
);
1349

14-
describe('<AssessmentActions />', () => {
50+
describe('AssessmentActions', () => {
1551
const mockExitWithoutSavingAction = {
1652
action: {
1753
onClick: jest.fn().mockName('useExitWithoutSavingAction.onClick'),
54+
children: 'Exit without saving',
1855
},
1956
confirmProps: {
2057
onConfirm: jest.fn().mockName('useExitWithoutSavingAction.onConfirm'),
58+
title: 'Exit without saving',
2159
},
2260
};
2361
const mockSubmitAssessmentAction = {
2462
action: {
2563
onClick: jest.fn().mockName('useSubmitAssessmentAction.onClick'),
64+
children: 'Submit assessment',
2665
},
2766
confirmProps: {
2867
onConfirm: jest.fn().mockName('useSubmitAssessmentAction.onConfirm'),
68+
title: 'Submit assessment',
2969
},
3070
};
3171

3272
beforeEach(() => {
73+
jest.clearAllMocks();
3374
useExitWithoutSavingAction.mockReturnValue(mockExitWithoutSavingAction);
3475
useSubmitAssessmentAction.mockReturnValue(mockSubmitAssessmentAction);
3576
});
3677

37-
it('render default', () => {
38-
const wrapper = shallow(<AssessmentActions />);
39-
expect(wrapper.snapshot).toMatchSnapshot();
78+
it('renders both action buttons and confirm dialogs', () => {
79+
render(<AssessmentActions />);
4080

41-
expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2);
42-
expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(2);
81+
expect(
82+
screen.getByTestId('action-button-outline-primary'),
83+
).toBeInTheDocument();
84+
expect(screen.getByTestId('action-button-primary')).toBeInTheDocument();
85+
expect(
86+
screen.getByTestId('confirm-dialog-Exit without saving'),
87+
).toBeInTheDocument();
88+
expect(
89+
screen.getByTestId('confirm-dialog-Submit assessment'),
90+
).toBeInTheDocument();
4391
});
4492

45-
it('render without submitConfirmDialog', () => {
93+
it('renders without submit confirm dialog when confirmProps is null', () => {
4694
useSubmitAssessmentAction.mockReturnValueOnce({
4795
action: mockSubmitAssessmentAction.action,
4896
confirmProps: null,
4997
});
50-
const wrapper = shallow(<AssessmentActions />);
51-
expect(wrapper.snapshot).toMatchSnapshot();
98+
render(<AssessmentActions />);
5299

53-
expect(wrapper.instance.findByType('ActionButton')).toHaveLength(2);
54-
expect(wrapper.instance.findByType('ConfirmDialog')).toHaveLength(1);
100+
expect(
101+
screen.getByTestId('action-button-outline-primary'),
102+
).toBeInTheDocument();
103+
expect(screen.getByTestId('action-button-primary')).toBeInTheDocument();
104+
expect(
105+
screen.getByTestId('confirm-dialog-Exit without saving'),
106+
).toBeInTheDocument();
107+
expect(
108+
screen.queryByTestId('confirm-dialog-Submit assessment'),
109+
).not.toBeInTheDocument();
55110
});
56111

57-
it('has correct mock value', () => {
58-
const wrapper = shallow(<AssessmentActions />);
112+
it('calls the correct handlers when buttons and dialogs are clicked', () => {
113+
render(<AssessmentActions />);
59114

60-
const exitButton = wrapper.instance.findByType('ActionButton')[0];
61-
expect(exitButton.props).toMatchObject(mockExitWithoutSavingAction.action);
115+
const exitButton = screen.getByTestId('action-button-outline-primary');
116+
expect(exitButton).toHaveTextContent('Exit without saving');
117+
fireEvent.click(exitButton);
118+
expect(mockExitWithoutSavingAction.action.onClick).toHaveBeenCalled();
62119

63-
const exitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[0];
64-
expect(exitConfirmDialog.props).toMatchObject(mockExitWithoutSavingAction.confirmProps);
120+
const submitButton = screen.getByTestId('action-button-primary');
121+
expect(submitButton).toHaveTextContent('Submit assessment');
122+
fireEvent.click(submitButton);
123+
expect(mockSubmitAssessmentAction.action.onClick).toHaveBeenCalled();
65124

66-
const submitButton = wrapper.instance.findByType('ActionButton')[1];
67-
expect(submitButton.props).toMatchObject(mockSubmitAssessmentAction.action);
125+
const exitDialog = screen.getByTestId('confirm-dialog-Exit without saving');
126+
fireEvent.click(exitDialog);
127+
expect(
128+
mockExitWithoutSavingAction.confirmProps.onConfirm,
129+
).toHaveBeenCalled();
68130

69-
const submitConfirmDialog = wrapper.instance.findByType('ConfirmDialog')[1];
70-
expect(submitConfirmDialog.props).toMatchObject(mockSubmitAssessmentAction.confirmProps);
131+
const submitDialog = screen.getByTestId('confirm-dialog-Submit assessment');
132+
fireEvent.click(submitDialog);
133+
expect(
134+
mockSubmitAssessmentAction.confirmProps.onConfirm,
135+
).toHaveBeenCalled();
71136
});
72137
});

src/components/Assessment/EditableAssessment/__snapshots__/AssessmentActions.test.jsx.snap

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,64 @@
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 GradedCriterion from './GradedCriterion';
46

5-
describe('<GradedCriterion />', () => {
6-
const props = {
7+
jest.unmock('@openedx/paragon');
8+
jest.unmock('react');
9+
jest.unmock('@edx/frontend-platform/i18n');
10+
11+
const renderComponent = (props = {}) => render(
12+
<IntlProvider
13+
messages={{
14+
'frontend-app-ora.RadioCriterion.optionPoints': '{points} points',
15+
}}
16+
locale="en"
17+
>
18+
<GradedCriterion {...props} />
19+
</IntlProvider>,
20+
);
21+
22+
describe('GradedCriterion', () => {
23+
const defaultProps = {
724
selectedOption: {
825
name: 'option1',
926
label: 'Option 1',
10-
points: 1,
27+
points: 5,
1128
},
12-
feedbackValue: 'Feedback',
29+
feedbackValue: 'Feedback text',
1330
};
1431

15-
it('renders correctly', () => {
16-
const wrapper = shallow(<GradedCriterion {...props} />);
17-
expect(wrapper.snapshot).toMatchSnapshot();
32+
it('displays the option label and points', () => {
33+
renderComponent(defaultProps);
34+
35+
expect(screen.getByText('Option 1')).toBeInTheDocument();
36+
expect(screen.getByText('5 points')).toBeInTheDocument();
37+
});
38+
39+
it('displays the option name if label is not provided', () => {
40+
const props = {
41+
selectedOption: {
42+
name: 'option1',
43+
points: 5,
44+
},
45+
};
46+
renderComponent(props);
47+
48+
expect(screen.getByText('option1')).toBeInTheDocument();
49+
expect(screen.getByText('5 points')).toBeInTheDocument();
50+
});
51+
52+
it('displays feedback when provided', () => {
53+
renderComponent(defaultProps);
54+
expect(screen.getByText('Feedback text')).toBeInTheDocument();
1855
});
1956

20-
it('renders correctly with no feedback', () => {
21-
const wrapper = shallow(<GradedCriterion selectedOption={props.selectedOption} />);
22-
expect(wrapper.snapshot).toMatchSnapshot();
57+
it('does not display feedback when not provided', () => {
58+
const props = {
59+
selectedOption: defaultProps.selectedOption,
60+
};
61+
renderComponent(props);
62+
expect(screen.queryByText('Feedback text')).not.toBeInTheDocument();
2363
});
2464
});

src/components/CriterionContainer/__snapshots__/GradedCriterion.test.jsx.snap

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

0 commit comments

Comments
 (0)