Skip to content

Commit a5c1745

Browse files
test: replacing snapshot tests with RTL tests part 8 (#2184)
1 parent 4b4ab92 commit a5c1745

File tree

16 files changed

+197
-393
lines changed

16 files changed

+197
-393
lines changed
Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
2+
import { useIntl } from '@edx/frontend-platform/i18n';
33
import {
44
ActionRow,
55
Container,
@@ -19,34 +19,35 @@ const HintRow = ({
1919
images,
2020
isLibrary,
2121
learningContextId,
22-
// injected
23-
intl,
24-
}) => (
25-
<ActionRow className="mb-4">
26-
<Container fluid className="p-0">
27-
<ExpandableTextArea
28-
value={value}
29-
setContent={handleChange}
30-
placeholder={intl.formatMessage(messages.hintInputLabel)}
31-
id={`hint-${id}`}
32-
{...{
33-
images,
34-
isLibrary,
35-
learningContextId,
36-
}}
37-
/>
38-
</Container>
39-
<div className="d-flex flex-row flex-nowrap">
40-
<IconButton
41-
src={DeleteOutline}
42-
iconAs={Icon}
43-
alt={intl.formatMessage(messages.settingsDeleteIconAltText)}
44-
onClick={handleDelete}
45-
variant="primary"
46-
/>
47-
</div>
48-
</ActionRow>
49-
);
22+
}) => {
23+
const intl = useIntl();
24+
return (
25+
<ActionRow className="mb-4">
26+
<Container fluid className="p-0">
27+
<ExpandableTextArea
28+
value={value}
29+
setContent={handleChange}
30+
placeholder={intl.formatMessage(messages.hintInputLabel)}
31+
id={`hint-${id}`}
32+
{...{
33+
images,
34+
isLibrary,
35+
learningContextId,
36+
}}
37+
/>
38+
</Container>
39+
<div className="d-flex flex-row flex-nowrap">
40+
<IconButton
41+
src={DeleteOutline}
42+
iconAs={Icon}
43+
alt={intl.formatMessage(messages.settingsDeleteIconAltText)}
44+
onClick={handleDelete}
45+
variant="primary"
46+
/>
47+
</div>
48+
</ActionRow>
49+
);
50+
};
5051

5152
HintRow.propTypes = {
5253
value: PropTypes.string.isRequired,
@@ -56,9 +57,6 @@ HintRow.propTypes = {
5657
images: PropTypes.shape({}).isRequired,
5758
learningContextId: PropTypes.string.isRequired,
5859
isLibrary: PropTypes.bool.isRequired,
59-
// injected
60-
intl: intlShape.isRequired,
6160
};
6261

63-
export const HintRowInternal = HintRow; // For testing only
64-
export default injectIntl(HintRow);
62+
export default HintRow;

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

Lines changed: 0 additions & 24 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import React from 'react';
2+
import {
3+
render, screen, initializeMocks, fireEvent,
4+
} from '@src/testUtils';
5+
import HintRow from './HintRow';
6+
7+
jest.mock('../../../../../../sharedComponents/ExpandableTextArea', () => 'ExpandableTextArea');
8+
9+
describe('HintRow', () => {
10+
const props = {
11+
value: 'hint_1',
12+
handleChange: jest.fn(),
13+
handleDelete: jest.fn(),
14+
id: '0',
15+
images: {},
16+
isLibrary: false,
17+
learningContextId: 'course+org+run',
18+
};
19+
20+
beforeEach(() => {
21+
initializeMocks();
22+
});
23+
24+
test('renders hints row', () => {
25+
render(<HintRow {...props} />);
26+
expect(screen.getByPlaceholderText('Hint')).toBeInTheDocument();
27+
expect(screen.getByRole('button', { name: 'Delete answer' })).toBeInTheDocument();
28+
});
29+
30+
test('calls handleDelete when button is clicked', () => {
31+
render(<HintRow {...props} />);
32+
expect(screen.getByRole('button', { name: 'Delete answer' })).toBeInTheDocument();
33+
fireEvent.click(screen.getByRole('button', { name: 'Delete answer' }));
34+
expect(props.handleDelete).toHaveBeenCalled();
35+
});
36+
});

src/editors/containers/ProblemEditor/components/EditProblemView/SettingsWidget/settingsComponents/__snapshots__/HintRow.test.jsx.snap

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ exports[`HintsCard snapshot snapshot: renders hints setting card multiple hints
99
summary=" {count, plural, =0 {} other {(+# more)}}"
1010
title="Hints"
1111
>
12-
<injectIntl(ShimmedIntlComponent)
12+
<HintRow
1313
handleChange={[MockFunction hintsRowHooks.handleChange]}
1414
handleDelete={[MockFunction hintsRowHooks.handleDelete]}
1515
id={1}
1616
key="1"
1717
value="hint1"
1818
/>
19-
<injectIntl(ShimmedIntlComponent)
19+
<HintRow
2020
handleChange={[MockFunction hintsRowHooks.handleChange]}
2121
handleDelete={[MockFunction hintsRowHooks.handleDelete]}
2222
id={2}
@@ -73,7 +73,7 @@ exports[`HintsCard snapshot snapshot: renders hints setting card one hint 1`] =
7373
summary="hint1 {count, plural, =0 {} other {(+# more)}}"
7474
title="Hints"
7575
>
76-
<injectIntl(ShimmedIntlComponent)
76+
<HintRow
7777
handleChange={[MockFunction hintsRowHooks.handleChange]}
7878
handleDelete={[MockFunction hintsRowHooks.handleDelete]}
7979
id={1}

src/editors/containers/VideoEditor/components/VideoSettingsModal/ErrorSummary.test.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import 'CourseAuthoring/editors/setupEditorTest';
21
import React from 'react';
3-
import { shallow } from '@edx/react-unit-test-utils';
2+
import {
3+
render, screen, initializeMocks,
4+
} from '@src/testUtils';
45

56
import * as mod from './ErrorSummary';
67

@@ -11,22 +12,29 @@ describe('ErrorSummary', () => {
1112
widgetWithError: [{ err1: 'mSg', err2: 'msG2' }, jest.fn()],
1213
widgetWithNoError: [{}, jest.fn()],
1314
};
15+
beforeEach(() => {
16+
initializeMocks();
17+
});
18+
1419
afterEach(() => {
1520
jest.restoreAllMocks();
1621
});
17-
describe('render', () => {
18-
beforeEach(() => {
19-
jest.spyOn(React, 'useContext').mockReturnValueOnce({});
20-
});
21-
test('snapshots: renders as expected when there are no errors', () => {
22+
23+
describe('renders', () => {
24+
jest.spyOn(React, 'useContext').mockReturnValueOnce({});
25+
test('renders as expected when there are no errors', () => {
2226
jest.spyOn(mod, 'showAlert').mockReturnValue(false);
23-
expect(shallow(<ErrorSummary />).snapshot).toMatchSnapshot();
27+
render(<ErrorSummary />);
28+
expect(screen.queryByRole('alert')).not.toBeInTheDocument();
2429
});
30+
2531
test('snapshots: renders as expected when there are errors', () => {
2632
jest.spyOn(mod, 'showAlert').mockReturnValue(true);
27-
expect(shallow(<ErrorSummary />).snapshot).toMatchSnapshot();
33+
render(<ErrorSummary />);
34+
expect(screen.getByRole('alert')).toBeInTheDocument();
2835
});
2936
});
37+
3038
describe('hasNoError', () => {
3139
it('returns true', () => {
3240
expect(mod.hasNoError(errors.widgetWithError)).toEqual(false);
@@ -35,6 +43,7 @@ describe('ErrorSummary', () => {
3543
expect(mod.hasNoError(errors.widgetWithNoError)).toEqual(true);
3644
});
3745
});
46+
3847
describe('showAlert', () => {
3948
it('returns true', () => {
4049
jest.spyOn(mod, 'hasNoError').mockReturnValue(false);

src/editors/containers/VideoEditor/components/VideoSettingsModal/__snapshots__/ErrorSummary.test.tsx.snap

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

src/editors/containers/VideoEditor/components/VideoSettingsModal/components/TranscriptWidget/LanguageSelector.jsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,11 @@ import {
1010

1111
import { Check } from '@openedx/paragon/icons';
1212
import { connect, useDispatch } from 'react-redux';
13-
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
13+
import { useIntl } from '@edx/frontend-platform/i18n';
1414
import { thunkActions, selectors } from '../../../../../../data/redux';
1515
import { videoTranscriptLanguages } from '../../../../../../data/constants/video';
1616
import { FileInput, fileInput } from '../../../../../../sharedComponents/FileInput';
1717
import messages from './messages';
18-
// This 'module' self-import hack enables mocking during tests.
19-
// See src/editors/decisions/0005-internal-editor-testability-decisions.md. The whole approach to how hooks are tested
20-
// should be re-thought and cleaned up to avoid this pattern.
21-
// eslint-disable-next-line import/no-self-import
22-
import * as module from './LanguageSelector';
2318

2419
export const hooks = {
2520
onSelectLanguage: ({
@@ -54,13 +49,11 @@ const LanguageSelector = ({
5449
language,
5550
// Redux
5651
openLanguages, // Only allow those languages not already associated with a transcript to be selected
57-
// intl
58-
intl,
59-
6052
}) => {
53+
const intl = useIntl();
6154
const [localLang, setLocalLang] = React.useState(language);
6255
const input = fileInput({ onAddFile: hooks.addFileCallback({ dispatch: useDispatch(), localLang }) });
63-
const onLanguageChange = module.hooks.onSelectLanguage({
56+
const onLanguageChange = hooks.onSelectLanguage({
6457
dispatch: useDispatch(), languageBeforeChange: localLang, setLocalLang, triggerupload: input.click,
6558
});
6659

@@ -124,7 +117,6 @@ LanguageSelector.propTypes = {
124117
openLanguages: PropTypes.arrayOf(PropTypes.string),
125118
index: PropTypes.number.isRequired,
126119
language: PropTypes.string.isRequired,
127-
intl: intlShape.isRequired,
128120
};
129121

130122
export const mapStateToProps = (state) => ({
@@ -134,4 +126,4 @@ export const mapStateToProps = (state) => ({
134126
export const mapDispatchToProps = {};
135127

136128
export const LanguageSelectorInternal = LanguageSelector; // For testing only
137-
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(LanguageSelector));
129+
export default connect(mapStateToProps, mapDispatchToProps)(LanguageSelector);

0 commit comments

Comments
 (0)