Skip to content

Commit 19f81cc

Browse files
test: replacing snapshot tests with RTL tests part 6 (#2173)
* test: replacing snapshot tests with rtl tests part 6 * fix: removing testing purposed text * test: fixing test mocking issues
1 parent fa9d66c commit 19f81cc

File tree

17 files changed

+287
-825
lines changed

17 files changed

+287
-825
lines changed

src/course-unit/xblock-container-iframe/hooks/tests/hooks.test.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import { useMessageHandlers } from '..';
1111

1212
jest.useFakeTimers();
1313

14-
jest.mock('@edx/react-unit-test-utils', () => ({
15-
useKeyedState: jest.fn(),
16-
}));
17-
1814
jest.mock('@edx/frontend-platform/logging', () => ({
1915
logError: jest.fn(),
2016
}));

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
Form,
99
} from '@openedx/paragon';
1010
import { FeedbackOutline, DeleteOutline } from '@openedx/paragon/icons';
11-
import { FormattedMessage, injectIntl, intlShape } from '@edx/frontend-platform/i18n';
11+
import { FormattedMessage, useIntl } from '@edx/frontend-platform/i18n';
1212
import { getConfig } from '@edx/frontend-platform';
1313
import messages from './messages';
1414
import { selectors } from '../../../../../data/redux';
@@ -22,15 +22,14 @@ import ExpandableTextArea from '../../../../../sharedComponents/ExpandableTextAr
2222
const AnswerOption = ({
2323
answer,
2424
hasSingleAnswer,
25-
// injected
26-
intl,
2725
// redux
2826
problemType,
2927
images,
3028
isLibrary,
3129
blockId,
3230
learningContextId,
3331
}) => {
32+
const intl = useIntl();
3433
const dispatch = useDispatch();
3534
const removeAnswer = hooks.removeAnswer({ answer, dispatch });
3635
const setAnswer = hooks.setAnswer({ answer, hasSingleAnswer, dispatch });
@@ -151,8 +150,6 @@ const AnswerOption = ({
151150
AnswerOption.propTypes = {
152151
answer: answerOptionProps.isRequired,
153152
hasSingleAnswer: PropTypes.bool.isRequired,
154-
// injected
155-
intl: intlShape.isRequired,
156153
// redux
157154
problemType: PropTypes.string.isRequired,
158155
images: PropTypes.shape({}).isRequired,
@@ -171,4 +168,4 @@ export const mapStateToProps = (state) => ({
171168

172169
export const mapDispatchToProps = {};
173170
export const AnswerOptionInternal = AnswerOption; // For testing only
174-
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(memo(AnswerOption)));
171+
export default connect(mapStateToProps, mapDispatchToProps)(memo(AnswerOption));

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.test.jsx

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import React from 'react';
2+
import { render, screen, initializeMocks } from '@src/testUtils';
3+
import AnswerOption from './AnswerOption';
4+
import * as hooks from './hooks';
5+
import { selectors } from '../../../../../data/redux';
6+
7+
const { problem } = selectors;
8+
9+
jest.mock('../../../../../data/redux', () => ({
10+
__esModule: true,
11+
default: jest.fn(),
12+
selectors: {
13+
problem: {
14+
problemType: jest.fn().mockReturnValue(''),
15+
},
16+
app: {
17+
images: jest.fn(state => ({ images: state })),
18+
isLibrary: jest.fn().mockReturnValue(true),
19+
learningContextId: jest.fn(state => ({ learningContextId: state })),
20+
blockId: jest.fn(state => ({ blockId: state })),
21+
},
22+
},
23+
thunkActions: {
24+
video: {
25+
importTranscripts: jest.fn(),
26+
},
27+
},
28+
}));
29+
30+
jest.mock('../../../../../sharedComponents/ExpandableTextArea', () => 'ExpandableTextArea');
31+
32+
describe('AnswerOption', () => {
33+
const answerWithOnlyFeedback = {
34+
id: 'A',
35+
title: 'Answer 1',
36+
correct: true,
37+
selectedFeedback: 'some feedback',
38+
isAnswerRange: true,
39+
};
40+
const answerWithSelectedUnselectedFeedback = {
41+
id: 'B',
42+
title: 'Answer 2',
43+
correct: true,
44+
selectedFeedback: 'selected feedback',
45+
unselectedFeedback: 'unselected feedback',
46+
isAnswerRange: false,
47+
};
48+
const answerRange = {
49+
id: 'A',
50+
title: 'Answer Range 1',
51+
correct: true,
52+
selectedFeedback: 'selected feedback',
53+
unselectedFeedback: 'unselected feedback',
54+
isAnswerRange: true,
55+
};
56+
57+
const props = {
58+
hasSingleAnswer: false,
59+
answer: answerWithOnlyFeedback,
60+
// redux
61+
problemType: 'multiplechoiceresponse',
62+
images: {},
63+
isLibrary: false,
64+
learningContextId: 'course+org+run',
65+
blockId: 'block-id',
66+
};
67+
68+
beforeEach(() => {
69+
jest.spyOn(hooks, 'removeAnswer').mockReturnValue(jest.fn());
70+
jest.spyOn(hooks, 'setAnswer').mockReturnValue(jest.fn());
71+
jest.spyOn(hooks, 'setAnswerTitle').mockReturnValue(jest.fn());
72+
jest.spyOn(hooks, 'setSelectedFeedback').mockReturnValue(jest.fn());
73+
jest.spyOn(hooks, 'setUnselectedFeedback').mockReturnValue(jest.fn());
74+
jest.spyOn(hooks, 'useFeedback').mockReturnValue({
75+
isFeedbackVisible: false,
76+
toggleFeedback: jest.fn(),
77+
});
78+
initializeMocks();
79+
});
80+
81+
test('renders correct option with feedback', () => {
82+
jest.spyOn(problem, 'problemType').mockReturnValue('multiplechoiceresponse');
83+
render(<AnswerOption {...props} />);
84+
expect(screen.getByPlaceholderText('Enter an answer')).toBeInTheDocument();
85+
expect(screen.getByRole('button', { name: 'Delete answer' })).toBeInTheDocument();
86+
});
87+
88+
test('renders correct option with selected unselected feedback', () => {
89+
jest.spyOn(problem, 'problemType').mockReturnValue('choiceresponse');
90+
const myProps = { ...props, answer: answerWithSelectedUnselectedFeedback };
91+
render(<AnswerOption {...myProps} />);
92+
expect(screen.getByText(answerWithSelectedUnselectedFeedback.id)).toBeInTheDocument();
93+
});
94+
95+
test('renders correct option with optionresponse input problem', () => {
96+
jest.spyOn(problem, 'problemType').mockReturnValue('optionresponse');
97+
const myProps = { ...props };
98+
render(<AnswerOption {...myProps} />);
99+
expect(screen.getByRole('textbox')).toBeInTheDocument();
100+
expect(screen.getByText(answerWithOnlyFeedback.title)).toBeInTheDocument();
101+
});
102+
103+
test('renders correct option with numeric input problem and answer range', () => {
104+
jest.spyOn(problem, 'problemType').mockReturnValue('numericalresponse');
105+
const myProps = { ...props };
106+
render(<AnswerOption {...myProps} answer={answerRange} />);
107+
expect(screen.getByText(answerRange.title)).toBeInTheDocument();
108+
expect(screen.getByRole('textbox')).toBeInTheDocument();
109+
});
110+
});

0 commit comments

Comments
 (0)