Skip to content

Commit 08c3d12

Browse files
test: replacing snapshot tests with RTL tests part 7 (#2181)
1 parent 920f4a5 commit 08c3d12

File tree

19 files changed

+418
-980
lines changed

19 files changed

+418
-980
lines changed

src/editors/containers/EditorContainer/components/TitleHeader/EditableHeader.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,4 @@ EditableHeader.propTypes = {
4747
cancelEdit: PropTypes.func.isRequired,
4848
};
4949

50-
export const EditableHeaderInternal = EditableHeader; // For testing only
5150
export default EditableHeader;

src/editors/containers/EditorContainer/components/TitleHeader/EditableHeader.test.jsx

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import React from 'react';
2+
import {
3+
render, screen, initializeMocks, fireEvent,
4+
} from '@src/testUtils';
5+
import EditableHeader from './EditableHeader';
6+
7+
describe('EditableHeader', () => {
8+
const props = {
9+
handleChange: jest.fn().mockName('args.handleChange'),
10+
updateTitle: jest.fn().mockName('args.updateTitle'),
11+
handleKeyDown: jest.fn().mockName('args.handleKeyDown'),
12+
inputRef: jest.fn().mockName('args.inputRef'),
13+
localTitle: 'test-title-text',
14+
cancelEdit: jest.fn().mockName('args.cancelEdit'),
15+
};
16+
17+
beforeEach(() => {
18+
initializeMocks();
19+
});
20+
21+
test('renders input with correct value and placeholder', () => {
22+
render(<EditableHeader {...props} />);
23+
const input = screen.getByPlaceholderText('Title');
24+
expect(input).toBeInTheDocument();
25+
expect((input as HTMLInputElement).value).toBe(props.localTitle);
26+
});
27+
28+
test('calls handleChange when input changes', () => {
29+
render(<EditableHeader {...props} />);
30+
const input = screen.getByPlaceholderText('Title');
31+
fireEvent.change(input, { target: { value: 'New title' } });
32+
expect(props.handleChange).toHaveBeenCalled();
33+
});
34+
35+
test('calls handleKeyDown on keydown', () => {
36+
render(<EditableHeader {...props} />);
37+
const input = screen.getByPlaceholderText('Title');
38+
fireEvent.keyDown(input, { target: { value: 'New title' } });
39+
expect(props.handleKeyDown).toHaveBeenCalled();
40+
});
41+
42+
test('calls updateTitle on blur', () => {
43+
render(<EditableHeader {...props} />);
44+
const input = screen.getByPlaceholderText('Title');
45+
fireEvent.blur(input);
46+
expect(props.updateTitle).toHaveBeenCalled();
47+
});
48+
49+
test('calls inputRef if provided', () => {
50+
const inputRef = jest.fn();
51+
render(<EditableHeader {...props} inputRef={inputRef} />);
52+
expect(inputRef).toHaveBeenCalled();
53+
});
54+
55+
test('renders buttons from trailing element EditConfirmationButtons', () => {
56+
render(<EditableHeader {...props} />);
57+
const cancelButton = screen.getByRole('button', { name: /cancel/i });
58+
expect(cancelButton).toBeInTheDocument();
59+
const saveButton = screen.getByRole('button', { name: /save/i });
60+
expect(saveButton).toBeInTheDocument();
61+
});
62+
});

src/editors/containers/EditorContainer/components/TitleHeader/__snapshots__/EditableHeader.test.jsx.snap

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

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

Lines changed: 3 additions & 6 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, FormattedMessage, intlShape } from '@edx/frontend-platform/i18n';
3+
import { useIntl, FormattedMessage } from '@edx/frontend-platform/i18n';
44
import SettingsOption from '../SettingsOption';
55
import { ProblemTypeKeys } from '../../../../../../data/constants/problem';
66
import messages from '../messages';
@@ -15,9 +15,8 @@ const HintsCard = ({
1515
images,
1616
isLibrary,
1717
learningContextId,
18-
// inject
19-
intl,
2018
}) => {
19+
const intl = useIntl();
2120
const { summary, handleAdd } = hintsCardHooks(hints, updateSettings);
2221

2322
if (problemType === ProblemTypeKeys.ADVANCED) { return null; }
@@ -55,7 +54,6 @@ const HintsCard = ({
5554
};
5655

5756
HintsCard.propTypes = {
58-
intl: intlShape.isRequired,
5957
hints: PropTypes.arrayOf(PropTypes.shape({
6058
id: PropTypes.string.isRequired,
6159
value: PropTypes.string.isRequired,
@@ -67,5 +65,4 @@ HintsCard.propTypes = {
6765
isLibrary: PropTypes.bool.isRequired,
6866
};
6967

70-
export const HintsCardInternal = HintsCard; // For testing only
71-
export default injectIntl(HintsCard);
68+
export default HintsCard;

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

Lines changed: 0 additions & 83 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React from 'react';
2+
import { render, screen, initializeMocks } from '@src/testUtils';
3+
import HintsCard from './HintsCard';
4+
5+
jest.mock('./HintRow', () => 'HintRow');
6+
7+
describe('HintsCard', () => {
8+
const hint1 = { id: '1', value: 'hint-1' };
9+
const hint2 = { id: '2', value: 'hint-2' };
10+
const hints0 = [];
11+
const hints1 = [hint1];
12+
const hints2 = [hint1, hint2];
13+
14+
const props = {
15+
hints: hints0,
16+
updateSettings: jest.fn().mockName('args.updateSettings'),
17+
problemType: 'multiplechoiceresponse',
18+
images: {},
19+
isLibrary: false,
20+
learningContextId: 'ID+',
21+
};
22+
23+
beforeEach(() => {
24+
initializeMocks();
25+
});
26+
27+
describe('HintsCard', () => {
28+
test('renders component', () => {
29+
render(<HintsCard {...props} />);
30+
expect(screen.getByText('Hints')).toBeInTheDocument();
31+
expect(screen.getByRole('button', { name: 'Add hint' })).toBeInTheDocument();
32+
});
33+
34+
test('does not render component when problemType is advanced', () => {
35+
render(<HintsCard {...props} problemType="advanced" />);
36+
expect(screen.queryByText('Hints')).not.toBeInTheDocument();
37+
expect(screen.queryByText('button')).not.toBeInTheDocument();
38+
});
39+
40+
test('renders hints setting card one hint', () => {
41+
render(<HintsCard {...props} hints={hints1} />);
42+
expect(document.querySelector('hintrow[value="hint-1"]')).toBeInTheDocument();
43+
});
44+
45+
test('snapshot: renders hints setting card multiple hints', () => {
46+
render(<HintsCard {...props} hints={hints2} />);
47+
expect(document.querySelector('hintrow[value="hint-1"]')).toBeInTheDocument();
48+
expect(document.querySelector('hintrow[value="hint-2"]')).toBeInTheDocument();
49+
});
50+
});
51+
});

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import isNil from 'lodash/isNil';
33
import PropTypes from 'prop-types';
44
import { connect } from 'react-redux';
5-
import { FormattedMessage, injectIntl, intlShape } from '@edx/frontend-platform/i18n';
5+
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
66
import { Form, Hyperlink } from '@openedx/paragon';
77
import { selectors } from '../../../../../../data/redux';
88
import SettingsOption from '../SettingsOption';
@@ -13,13 +13,12 @@ const ScoringCard = ({
1313
scoring,
1414
defaultValue,
1515
updateSettings,
16-
// inject
17-
intl,
1816
// redux
1917
studioEndpointUrl,
2018
learningContextId,
2119
isLibrary,
2220
}) => {
21+
const intl = useIntl();
2322
const {
2423
handleUnlimitedChange,
2524
handleMaxAttemptChange,
@@ -93,7 +92,6 @@ const ScoringCard = ({
9392
};
9493

9594
ScoringCard.propTypes = {
96-
intl: intlShape.isRequired,
9795
// eslint-disable-next-line
9896
scoring: PropTypes.any.isRequired,
9997
updateSettings: PropTypes.func.isRequired,
@@ -117,5 +115,4 @@ export const mapStateToProps = (state) => ({
117115

118116
export const mapDispatchToProps = {};
119117

120-
export const ScoringCardInternal = ScoringCard; // For testing only
121-
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(ScoringCard));
118+
export default connect(mapStateToProps, mapDispatchToProps)(ScoringCard);

0 commit comments

Comments
 (0)