Skip to content

Commit 3673c5f

Browse files
test: replacing snapshot tests with RTL tests part 14 (#2236)
1 parent 2fe24f3 commit 3673c5f

File tree

11 files changed

+201
-500
lines changed

11 files changed

+201
-500
lines changed

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

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

src/editors/containers/ProblemEditor/components/EditProblemView/QuestionWidget/index.test.jsx

Lines changed: 0 additions & 76 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 { render, screen, initializeMocks } from '@src/testUtils';
3+
import { formatMessage } from '@src/editors/testUtils';
4+
import { QuestionWidgetInternal as QuestionWidget } from '.';
5+
6+
jest.mock('@src/editors/data/redux', () => ({
7+
__esModule: true,
8+
default: jest.fn(),
9+
actions: {
10+
problem: {
11+
updateQuestion: jest.fn().mockName('actions.problem.updateQuestion'),
12+
},
13+
},
14+
selectors: {
15+
app: {
16+
learningContextId: jest.fn(state => ({ learningContextId: state })),
17+
images: jest.fn(state => ({ images: state })),
18+
isLibrary: jest.fn(state => ({ isLibrary: state })),
19+
blockId: jest.fn(state => ({ blockId: state })),
20+
},
21+
problem: {
22+
question: jest.fn(state => ({ question: state })),
23+
},
24+
},
25+
thunkActions: {
26+
video: {
27+
importTranscript: jest.fn(),
28+
},
29+
},
30+
}));
31+
32+
jest.mock('@src/editors/sharedComponents/TinyMceWidget/hooks', () => ({
33+
...jest.requireActual('../../../../../sharedComponents/TinyMceWidget/hooks'),
34+
prepareEditorRef: jest.fn(() => ({
35+
refReady: true,
36+
setEditorRef: jest.fn().mockName('prepareEditorRef.setEditorRef'),
37+
})),
38+
}));
39+
40+
jest.mock('@src/editors/sharedComponents/TinyMceWidget', () => ('TinyMceWidget'));
41+
42+
describe('QuestionWidget', () => {
43+
const props = {
44+
question: 'This is my question',
45+
updateQuestion: jest.fn(),
46+
learningContextId: 'course+org+run',
47+
images: {},
48+
isLibrary: false,
49+
blockId: '',
50+
// injected
51+
intl: { formatMessage },
52+
};
53+
describe('render', () => {
54+
beforeEach(() => {
55+
initializeMocks();
56+
});
57+
test('renders correct default', () => {
58+
render(<QuestionWidget {...props} />);
59+
expect(screen.getByText('Question')).toBeInTheDocument();
60+
});
61+
});
62+
});

src/editors/containers/VideoEditor/__snapshots__/index.test.tsx.snap

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 73 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,85 @@
1-
import 'CourseAuthoring/editors/setupEditorTest';
2-
import { shallow } from '@edx/react-unit-test-utils';
3-
4-
import { formatMessage } from '../../testUtils';
1+
import { render, initializeMocks } from '@src/testUtils';
52
import VideoEditor from '.';
3+
import * as hooks from './hooks';
4+
5+
const reduxSelectors = require('../../data/redux').selectors;
66

77
jest.mock('../EditorContainer', () => 'EditorContainer');
88
jest.mock('./components/VideoEditorModal', () => 'VideoEditorModal');
99

10-
jest.mock('./hooks', () => ({
11-
ErrorContext: {
12-
Provider: 'ErrorContext.Provider',
13-
},
14-
errorsHook: jest.fn(() => ({
15-
error: 'hooks.errorsHook.error',
16-
validateEntry: jest.fn().mockName('validateEntry'),
17-
})),
18-
fetchVideoContent: jest.fn().mockName('fetchVideoContent'),
19-
}));
20-
21-
jest.mock('../../data/redux', () => ({
22-
selectors: {
23-
requests: {
24-
isFinished: jest.fn((state, params) => ({ isFailed: { state, params } })),
25-
},
26-
app: {
27-
isLibrary: jest.fn(state => ({ isLibrary: state })),
28-
},
29-
},
30-
}));
31-
32-
jest.mock('@openedx/paragon', () => ({
33-
...jest.requireActual('@openedx/paragon'),
34-
Spinner: 'Spinner',
35-
}));
36-
3710
describe('VideoEditor', () => {
3811
const props = {
3912
onClose: jest.fn().mockName('props.onClose'),
40-
intl: { formatMessage },
41-
studioViewFinished: false,
42-
isLibrary: false,
13+
returnFunction: jest.fn().mockName('props.returnFunction'),
4314
};
44-
describe('snapshots', () => {
45-
test('renders as expected with default behavior', () => {
46-
expect(shallow(<VideoEditor {...props} />).snapshot).toMatchSnapshot();
15+
16+
describe('conditional rendering', () => {
17+
let isFinishedSpy: jest.SpyInstance;
18+
let isLibrarySpy: jest.SpyInstance;
19+
let shouldCreateBlockSpy: jest.SpyInstance;
20+
let errorsHookSpy: jest.SpyInstance;
21+
let fetchVideoContentSpy: jest.SpyInstance;
22+
23+
beforeEach(() => {
24+
isFinishedSpy = jest.spyOn(reduxSelectors.requests, 'isFinished');
25+
isLibrarySpy = jest.spyOn(reduxSelectors.app, 'isLibrary');
26+
shouldCreateBlockSpy = jest.spyOn(reduxSelectors.app, 'shouldCreateBlock');
27+
errorsHookSpy = jest.spyOn(hooks, 'errorsHook');
28+
fetchVideoContentSpy = jest.spyOn(hooks, 'fetchVideoContent');
29+
errorsHookSpy.mockReturnValue({ error: null, validateEntry: jest.fn() });
30+
fetchVideoContentSpy.mockReturnValue(jest.fn());
31+
isLibrarySpy.mockReturnValue(false);
32+
initializeMocks();
33+
});
34+
35+
afterEach(() => {
36+
jest.clearAllMocks();
37+
});
38+
39+
test('shows spinner when neither create workflow nor studio view finished', () => {
40+
isFinishedSpy.mockReturnValue(false);
41+
shouldCreateBlockSpy.mockReturnValue(false);
42+
const { container } = render(<VideoEditor {...props} />);
43+
expect(container.querySelector('.pgn__spinner')).toBeInTheDocument();
44+
expect(container.querySelector('EditorContainer')).toBeInTheDocument();
45+
expect(container.querySelector('VideoEditorModal')).not.toBeInTheDocument();
46+
});
47+
48+
test('shows VideoEditorModal when isCreateWorkflow is true', () => {
49+
isFinishedSpy.mockReturnValue(false);
50+
shouldCreateBlockSpy.mockReturnValue(true);
51+
const { container } = render(<VideoEditor {...props} />);
52+
expect(container.querySelector('.pgn__spinner')).not.toBeInTheDocument();
53+
expect(container.querySelector('EditorContainer')).toBeInTheDocument();
54+
expect(container.querySelector('VideoEditorModal')).toBeInTheDocument();
55+
});
56+
57+
test('shows VideoEditorModal when studioViewFinished is true', () => {
58+
isFinishedSpy.mockReturnValue(true);
59+
shouldCreateBlockSpy.mockReturnValue(false);
60+
61+
const { container } = render(<VideoEditor {...props} />);
62+
expect(container.querySelector('.pgn__spinner')).not.toBeInTheDocument();
63+
expect(container.querySelector('EditorContainer')).toBeInTheDocument();
64+
expect(container.querySelector('VideoEditorModal')).toBeInTheDocument();
65+
});
66+
67+
test('passes isLibrary, onClose, and returnFunction to VideoEditorModal', () => {
68+
isFinishedSpy.mockReturnValue(true);
69+
shouldCreateBlockSpy.mockReturnValue(false);
70+
isLibrarySpy.mockReturnValue(true);
71+
72+
const { container } = render(<VideoEditor {...props} />);
73+
expect(container.querySelector('.video-editor')).toBeInTheDocument();
74+
});
75+
76+
test('calls fetchVideoContent and passes validateEntry to EditorContainer', () => {
77+
isFinishedSpy.mockReturnValue(true);
78+
shouldCreateBlockSpy.mockReturnValue(false);
79+
80+
render(<VideoEditor {...props} />);
81+
expect(fetchVideoContentSpy).toHaveBeenCalled();
82+
expect(errorsHookSpy).toHaveBeenCalled();
4783
});
4884
});
4985
});

0 commit comments

Comments
 (0)