Skip to content

Commit 3fb1f86

Browse files
committed
test: add tests for dispatching updateCourseDetailsOverview on mceFocus in TinyMCE editor
1 parent d0c988f commit 3fb1f86

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

src/editors/sharedComponents/TinyMceWidget/hooks.test.js

+92
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import pluginConfig from './pluginConfig';
1010
// should be re-thought and cleaned up to avoid this pattern.
1111
// eslint-disable-next-line import/no-self-import
1212
import * as module from './hooks';
13+
import { updateCourseDetailsOverview } from '../../../schedule-and-details/data/slice';
1314

1415
jest.mock('react', () => ({
1516
...jest.requireActual('react'),
@@ -167,6 +168,97 @@ describe('TinyMceEditor hooks', () => {
167168
expect(openImgModal).not.toHaveBeenCalled();
168169
expect(editor.on).toHaveBeenCalled();
169170
});
171+
test('dispatches updateCourseDetailsOverview if newContent exists on mceFocus', () => {
172+
const dispatch = jest.fn();
173+
const editorType = 'text';
174+
175+
const editor = {
176+
ui: {
177+
registry: {
178+
addButton: jest.fn(),
179+
addToggleButton: jest.fn(),
180+
addIcon: jest.fn(),
181+
},
182+
},
183+
on: jest.fn((event, callback) => {
184+
if (event === 'ExecCommand') {
185+
callback({ command: 'mceFocus' });
186+
}
187+
}),
188+
getContent: jest.fn(() => '<p>Some content</p>'),
189+
formatter: { remove: jest.fn(), formatChanged: jest.fn() },
190+
undoManager: { add: jest.fn() },
191+
focus: jest.fn(),
192+
};
193+
194+
const replaceStaticWithAssetSpy = jest
195+
.spyOn(module, 'replaceStaticWithAsset')
196+
.mockReturnValue('<p>Updated content</p>');
197+
198+
const setup = module.setupCustomBehavior({
199+
editorType,
200+
updateContent: jest.fn(),
201+
openImgModal: jest.fn(),
202+
openSourceCodeModal: jest.fn(),
203+
setImage: jest.fn(),
204+
lmsEndpointUrl: 'http://example.com',
205+
learningContextId: 'course-v1:test',
206+
images: { current: [] },
207+
dispatch,
208+
});
209+
210+
setup(editor);
211+
212+
expect(dispatch).toHaveBeenCalledWith(updateCourseDetailsOverview('<p>Updated content</p>'));
213+
214+
replaceStaticWithAssetSpy.mockRestore();
215+
});
216+
217+
test('does not dispatch updateCourseDetailsOverview if newContent is falsy on mceFocus', () => {
218+
const dispatch = jest.fn();
219+
const editorType = 'text';
220+
221+
const editor = {
222+
ui: {
223+
registry: {
224+
addButton: jest.fn(),
225+
addToggleButton: jest.fn(),
226+
addIcon: jest.fn(),
227+
},
228+
},
229+
on: jest.fn((event, callback) => {
230+
if (event === 'ExecCommand') {
231+
callback({ command: 'mceFocus' });
232+
}
233+
}),
234+
getContent: jest.fn(() => '<p>Unchanged content</p>'),
235+
formatter: { remove: jest.fn(), formatChanged: jest.fn() },
236+
undoManager: { add: jest.fn() },
237+
focus: jest.fn(),
238+
};
239+
240+
const replaceStaticWithAssetSpy = jest
241+
.spyOn(module, 'replaceStaticWithAsset')
242+
.mockReturnValue(false);
243+
244+
const setup = module.setupCustomBehavior({
245+
editorType,
246+
updateContent: jest.fn(),
247+
openImgModal: jest.fn(),
248+
openSourceCodeModal: jest.fn(),
249+
setImage: jest.fn(),
250+
lmsEndpointUrl: 'http://example.com',
251+
learningContextId: 'course-v1:test',
252+
images: { current: [] },
253+
dispatch,
254+
});
255+
256+
setup(editor);
257+
258+
expect(dispatch).not.toHaveBeenCalled();
259+
260+
replaceStaticWithAssetSpy.mockRestore();
261+
});
170262
});
171263

172264
describe('parseContentForLabels', () => {

0 commit comments

Comments
 (0)