Skip to content

Commit 962b30b

Browse files
test: replacing snapshot tests with RTL tests part 9 (#2206)
1 parent 75ea750 commit 962b30b

File tree

13 files changed

+159
-824
lines changed

13 files changed

+159
-824
lines changed

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/components/Checker/__snapshots__/index.test.jsx.snap

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

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/components/Checker/index.test.jsx

Lines changed: 0 additions & 27 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {
2+
render, screen, initializeMocks, fireEvent,
3+
} from '@src/testUtils';
4+
import Checker from '.';
5+
6+
const props = {
7+
hasSingleAnswer: true,
8+
answer: {
9+
id: 1,
10+
correct: true,
11+
},
12+
setAnswer: jest.fn(),
13+
};
14+
describe('Checker component', () => {
15+
beforeEach(() => {
16+
initializeMocks();
17+
});
18+
19+
test('renders with single answer', () => {
20+
render(<Checker {...props} />);
21+
expect(screen.getByText('1')).toBeInTheDocument();
22+
expect(screen.getByRole('radio')).toBeInTheDocument();
23+
expect(screen.queryByRole('checkbox')).not.toBeInTheDocument();
24+
});
25+
26+
test('renders with multiple answers', () => {
27+
render(<Checker {...props} hasSingleAnswer={false} />);
28+
expect(screen.getByRole('checkbox')).toBeInTheDocument();
29+
expect(screen.queryByRole('radio')).not.toBeInTheDocument();
30+
});
31+
32+
test('renders with disabled radio', () => {
33+
render(<Checker {...props} disabled />);
34+
expect(screen.getByRole('radio')).toBeInTheDocument();
35+
});
36+
37+
test('calls setAnswer when radio button is clicked', () => {
38+
render(<Checker {...props} />);
39+
expect(screen.getByRole('radio')).toBeInTheDocument();
40+
fireEvent.click(screen.getByRole('radio'), { target: { checked: !props.answer.correct } });
41+
expect(props.setAnswer).toHaveBeenCalledWith({ correct: true });
42+
});
43+
});
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import 'CourseAuthoring/editors/setupEditorTest';
21
import React from 'react';
3-
import { shallow } from '@edx/react-unit-test-utils';
4-
import { formatMessage } from '../../../../../../testUtils';
2+
import {
3+
render, screen, initializeMocks,
4+
} from '@src/testUtils';
5+
import { formatMessage } from '@src/editors/testUtils';
56
import { selectors } from '../../../../../../data/redux';
67
import { ShowAnswerCardInternal as ShowAnswerCard, mapStateToProps, mapDispatchToProps } from './ShowAnswerCard';
7-
import { useAnswerSettings } from '../hooks';
8-
9-
jest.mock('../hooks', () => ({
10-
useAnswerSettings: jest.fn(),
11-
}));
8+
import * as hooks from '../hooks';
129

1310
jest.mock('../../../../../../data/redux', () => ({
1411
selectors: {
@@ -30,45 +27,48 @@ describe('ShowAnswerCard', () => {
3027
updateSettings: jest.fn().mockName('args.updateSettings'),
3128
intl: { formatMessage },
3229
};
30+
3331
const props = {
3432
showAnswer,
3533
defaultValue: 'finished',
34+
updateSettings: jest.fn(),
3635
// injected
3736
intl: { formatMessage },
3837
// redux
3938
studioEndpointUrl: 'SoMEeNDpOinT',
4039
learningContextId: 'sOMEcouRseId',
40+
isLibrary: false,
4141
};
4242

43-
const useAnswerSettingsProps = {
44-
handleShowAnswerChange: jest.fn().mockName('useAnswerSettings.handleShowAnswerChange'),
45-
handleAttemptsChange: jest.fn().mockName('useAnswerSettings.handleAttemptsChange'),
46-
};
47-
48-
useAnswerSettings.mockReturnValue(useAnswerSettingsProps);
43+
describe('renders', () => {
44+
beforeEach(() => {
45+
initializeMocks();
46+
});
4947

50-
describe('behavior', () => {
51-
it(' calls useAnswerSettings when initialized', () => {
52-
shallow(<ShowAnswerCard {...props} />);
53-
expect(useAnswerSettings).toHaveBeenCalledWith(showAnswer, props.updateSettings);
48+
test('show answer setting card', () => {
49+
render(<ShowAnswerCard {...props} />);
50+
expect(screen.getByText('Show answer')).toBeInTheDocument();
5451
});
55-
});
5652

57-
describe('snapshot', () => {
58-
test('snapshot: show answer setting card', () => {
59-
expect(shallow(<ShowAnswerCard {...props} />).snapshot).toMatchSnapshot();
53+
test('calls useAnswerSettings when initialized', () => {
54+
jest.spyOn(hooks, 'useAnswerSettings');
55+
render(<ShowAnswerCard {...props} />);
56+
expect(screen.getByText('Show answer')).toBeInTheDocument();
57+
expect(hooks.useAnswerSettings).toHaveBeenCalledWith(showAnswer, props.updateSettings);
6058
});
6159
});
6260
describe('mapStateToProps', () => {
6361
const testState = { A: 'pple', B: 'anana', C: 'ucumber' };
6462
test('studioEndpointUrl from app.studioEndpointUrl', () => {
6563
expect(
6664
mapStateToProps(testState).studioEndpointUrl,
65+
// @ts-ignore
6766
).toEqual(selectors.app.studioEndpointUrl(testState));
6867
});
6968
test('learningContextId from app.learningContextId', () => {
7069
expect(
7170
mapStateToProps(testState).learningContextId,
71+
// @ts-ignore
7272
).toEqual(selectors.app.learningContextId(testState));
7373
});
7474
});

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/TypeCard.jsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
3+
import { useIntl } from '@edx/frontend-platform/i18n';
44
import SettingsOption from '../SettingsOption';
55
import { ProblemTypeKeys, ProblemTypes } from '../../../../../../data/constants/problem';
66
import messages from '../messages';
@@ -14,9 +14,8 @@ const TypeCard = ({
1414
setBlockTitle,
1515
updateField,
1616
updateAnswer,
17-
// inject
18-
intl,
1917
}) => {
18+
const intl = useIntl();
2019
const problemTypeKeysArray = Object.values(ProblemTypeKeys).filter(key => key !== ProblemTypeKeys.ADVANCED);
2120

2221
if (problemType === ProblemTypeKeys.ADVANCED) { return null; }
@@ -60,9 +59,6 @@ TypeCard.propTypes = {
6059
setBlockTitle: PropTypes.func.isRequired,
6160
updateField: PropTypes.func.isRequired,
6261
updateAnswer: PropTypes.func.isRequired,
63-
// injected
64-
intl: intlShape.isRequired,
6562
};
6663

67-
export const TypeCardInternal = TypeCard; // For testing only
68-
export default injectIntl(TypeCard);
64+
export default TypeCard;

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/TypeCard.test.jsx

Lines changed: 0 additions & 26 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from 'react';
2+
import { render, screen, initializeMocks } from '@src/testUtils';
3+
import TypeCard from './TypeCard';
4+
import { ProblemTypeKeys } from '../../../../../../data/constants/problem';
5+
6+
describe('TypeCard', () => {
7+
const props = {
8+
answers: [],
9+
blockTitle: 'BLocktiTLE',
10+
correctAnswerCount: 0,
11+
problemType: ProblemTypeKeys.TEXTINPUT,
12+
setBlockTitle: jest.fn().mockName('args.setBlockTitle'),
13+
updateField: jest.fn().mockName('args.updateField'),
14+
updateAnswer: jest.fn().mockName('args.updateAnswer'),
15+
};
16+
17+
beforeEach(() => {
18+
initializeMocks();
19+
});
20+
21+
test('renders type setting card', () => {
22+
render(<TypeCard {...props} />);
23+
expect(screen.getByText('Type')).toBeInTheDocument();
24+
expect(screen.getByText('Text input')).toBeInTheDocument();
25+
});
26+
27+
test('renders nothing if problemType is advanced', () => {
28+
const { container } = render(<TypeCard {...props} problemType={ProblemTypeKeys.ADVANCED} />);
29+
expect(screen.queryByText('Type')).not.toBeInTheDocument();
30+
expect(container.firstChild?.textContent).toBe('');
31+
});
32+
});

0 commit comments

Comments
 (0)