Skip to content

Commit ca85ca8

Browse files
test: replacing snapshot tests with RTL tests part 4 (#2135)
* test: replacing snapshot tests with rtl tests part 4 * test: removing not needed icon mocks, and changing name to render function for editors
1 parent 4a1f454 commit ca85ca8

File tree

21 files changed

+385
-1152
lines changed

21 files changed

+385
-1152
lines changed

src/editors/VideoSelector.test.tsx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
11
import React from 'react';
22
import * as reactRedux from 'react-redux';
3-
import { Provider } from 'react-redux';
43
import * as hooks from './hooks';
54
import VideoSelector from './VideoSelector';
6-
import { render as baseRender, initializeMocks, screen } from '../testUtils';
7-
8-
import { EditorContextProvider } from './EditorContext';
9-
import editorStore from './data/store';
10-
11-
const render = (ui) => baseRender(ui, {
12-
extraWrapper: ({ children }) => (
13-
<EditorContextProvider learningContextId="course-v1:Org+COURSE+RUN">
14-
<Provider store={editorStore}>
15-
{children}
16-
</Provider>
17-
</EditorContextProvider>
18-
),
19-
});
5+
import editorRender from './editorTestRender';
6+
import { initializeMocks, screen } from '../testUtils';
207

218
const defaultProps = {
229
blockId: 'block-v1:edX+DemoX+Demo_Course+type@html+block@030e35c4756a4ddc8d40b95fbbfff4d4',
@@ -32,15 +19,15 @@ describe('VideoSelector', () => {
3219

3320
test('renders VideoGallery when loading is false', () => {
3421
jest.spyOn(hooks, 'useInitializeApp').mockReturnValue(false);
35-
render(<VideoSelector {...defaultProps} />);
22+
editorRender(<VideoSelector {...defaultProps} />);
3623
expect(screen.getByText('Add video to your course')).toBeInTheDocument();
3724
});
3825

3926
// TODO: transform into user-centric test to follow the best practices:
4027
// "testing the application components in the way the user would use it"
4128
test('renders nothing when loading is true', () => {
4229
jest.spyOn(hooks, 'useInitializeApp').mockReturnValue(true);
43-
render(<VideoSelector {...defaultProps} />);
30+
editorRender(<VideoSelector {...defaultProps} />);
4431
expect(screen.queryByText('Add video to your course')).not.toBeInTheDocument();
4532
});
4633

@@ -54,7 +41,7 @@ describe('VideoSelector', () => {
5441
const mockDispatch = jest.fn();
5542
jest.spyOn(reactRedux, 'useDispatch').mockReturnValue(mockDispatch);
5643
jest.spyOn(hooks, 'useInitializeApp');
57-
render(<VideoSelector {...defaultProps} />);
44+
editorRender(<VideoSelector {...defaultProps} />);
5845
expect(hooks.useInitializeApp).toHaveBeenCalledWith({
5946
dispatch: mockDispatch,
6047
data: initData,

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

Lines changed: 0 additions & 165 deletions
This file was deleted.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react';
2+
import {
3+
render, screen, fireEvent, initializeMocks,
4+
} from '../../../../../../testUtils';
5+
import { AnswersContainerInternal as AnswersContainer } from './AnswersContainer';
6+
import { ProblemTypeKeys } from '../../../../../data/constants/problem';
7+
8+
const { useAnswerContainer } = require('./hooks');
9+
10+
jest.mock('./AnswerOption', () => jest.fn(({ answer }) => <div>AnswerOption-{answer.id}</div>));
11+
jest.mock(
12+
'../../../../../sharedComponents/Button',
13+
() => jest.fn(({ children, ...props }) => <button type="button" {...props}>{children}</button>),
14+
);
15+
16+
jest.mock('./hooks', () => ({
17+
useAnswerContainer: jest.fn(),
18+
isSingleAnswerProblem: jest.fn(() => false),
19+
}));
20+
21+
describe('AnswersContainer', () => {
22+
const defaultProps = {
23+
problemType: 'multiple_choice',
24+
answers: [
25+
{ id: 'a1', isAnswerRange: false },
26+
{ id: 'a2', isAnswerRange: false },
27+
],
28+
addAnswer: jest.fn(),
29+
addAnswerRange: jest.fn(),
30+
updateField: jest.fn(),
31+
};
32+
33+
beforeEach(() => {
34+
initializeMocks();
35+
});
36+
37+
it('renders AnswerOption for each answer', () => {
38+
render(<AnswersContainer {...defaultProps} />);
39+
expect(screen.getByText('AnswerOption-a1')).toBeInTheDocument();
40+
expect(screen.getByText('AnswerOption-a2')).toBeInTheDocument();
41+
});
42+
43+
it('renders add answer Button for non-NUMERIC problemType and calls addAnswer on click', () => {
44+
render(<AnswersContainer {...defaultProps} />);
45+
const button = screen.getByRole('button', { name: 'Add answer' });
46+
expect(button).toBeInTheDocument();
47+
fireEvent.click(button);
48+
expect(defaultProps.addAnswer).toHaveBeenCalled();
49+
});
50+
51+
it('renders Dropdown for NUMERIC problemType', () => {
52+
render(<AnswersContainer {...defaultProps} problemType={ProblemTypeKeys.NUMERIC} />);
53+
expect(screen.getByRole('button', { name: 'Add answer' })).toBeInTheDocument();
54+
expect(screen.getByText('Add answer').closest('.dropdown')).toBeInTheDocument();
55+
});
56+
57+
it('calls useAnswerContainer with correct args', () => {
58+
render(<AnswersContainer {...defaultProps} />);
59+
expect(useAnswerContainer).toHaveBeenCalledWith({
60+
answers: defaultProps.answers,
61+
problemType: defaultProps.problemType,
62+
updateField: defaultProps.updateField,
63+
});
64+
});
65+
});

0 commit comments

Comments
 (0)