Skip to content

Commit

Permalink
test: refactor editor testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Jan 14, 2025
1 parent 1355231 commit e995900
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/editors/containers/EditorContainer/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ jest.spyOn(editorCmsApi, 'fetchCourseImages').mockImplementation(async () => ( /
{ data: { assets: [], start: 0, end: 0, page: 0, pageSize: 50, totalCount: 0 } }
));
// Mock out the 'get ancestors' API:
jest.spyOn(editorCmsApi, 'fetchByUnitId').mockImplementation(async () => ({
status: 200,
data: {
ancestors: [{
id: 'block-v1:Org+TS100+24+type@vertical+block@parent',
display_name: 'You-Knit? The Test Unit',
category: 'vertical',
has_children: true,
}],
},
}));

const isDirtyMock = jest.fn();
jest.mock('../TextEditor/hooks', () => ({
Expand Down Expand Up @@ -60,6 +49,17 @@ describe('EditorContainer', () => {
jest.spyOn(window, 'removeEventListener');
jest.spyOn(mockEvent, 'preventDefault');
Object.defineProperty(mockEvent, 'returnValue', { writable: true });
jest.spyOn(editorCmsApi, 'fetchByUnitId').mockImplementation(async () => ({
status: 200,
data: {
ancestors: [{
id: 'block-v1:Org+TS100+24+type@vertical+block@parent',
display_name: 'You-Knit? The Test Unit',
category: 'vertical',
has_children: true,
}],
},
}));
});

afterEach(() => {
Expand Down
3 changes: 0 additions & 3 deletions src/editors/containers/EditorContainer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const EditorContainer: React.FC<Props> = ({
const onSave = () => {
setSaved(true);
handleSave();
dispatch({ type: 'resetEditor' });
};
// Stops user from navigating away if they have unsaved changes.
usePromptIfDirty(() => {
Expand All @@ -110,7 +109,6 @@ const EditorContainer: React.FC<Props> = ({
openCancelConfirmModal();
} else {
handleCancel();
dispatch({ type: 'resetEditor' });
}
};
return (
Expand All @@ -130,7 +128,6 @@ const EditorContainer: React.FC<Props> = ({
if (returnFunction) {
closeCancelConfirmModal();
}
dispatch({ type: 'resetEditor' });
}}
>
<FormattedMessage {...messages.okButtonLabel} />
Expand Down
1 change: 1 addition & 0 deletions src/editors/containers/TextEditor/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ jest.mock('../../data/redux', () => ({
selectors: {
app: {
blockValue: jest.fn(state => ({ blockValue: state })),
isCreateBlock: jest.fn(state => ({ isCreateBlock: state })),
lmsEndpointUrl: jest.fn(state => ({ lmsEndpointUrl: state })),
studioEndpointUrl: jest.fn(state => ({ studioEndpointUrl: state })),
showRawEditor: jest.fn(state => ({ showRawEditor: state })),
Expand Down
5 changes: 5 additions & 0 deletions src/editors/data/redux/thunkActions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export const fetchCourseDetails = () => (dispatch) => {
*/
export const initialize = (data) => (dispatch) => {
const editorType = data.blockType;
dispatch({ type: 'resetEditor' });
dispatch(actions.app.initialize(data));
if (data.blockId === '' && editorType) {
dispatch(actions.app.initializeEditor());
Expand Down Expand Up @@ -139,6 +140,10 @@ export const createBlock = (content, returnToUnit) => (dispatch) => {
dispatch(actions.app.setBlockId(response.id));
dispatch(saveBlock(content, returnToUnit));
},
onFailure: (error) => dispatch(actions.requests.failRequest({
requestKey: RequestKeys.createBlock,
error,
})),
}));
};

Expand Down
5 changes: 5 additions & 0 deletions src/editors/data/redux/thunkActions/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('app thunkActions', () => {
thunkActions.fetchCourseDetails = () => 'fetchCourseDetails';
thunkActions.initialize(testValue)(dispatch);
expect(dispatch.mock.calls).toEqual([
[{ type: 'resetEditor' }],
[actions.app.initialize(testValue)],
[thunkActions.fetchBlock()],
]);
Expand All @@ -207,6 +208,7 @@ describe('app thunkActions', () => {
};
thunkActions.initialize(data)(dispatch);
expect(dispatch.mock.calls).toEqual([
[{ type: 'resetEditor' }],
[actions.app.initialize(data)],
[actions.app.initializeEditor()],
]);
Expand Down Expand Up @@ -236,6 +238,7 @@ describe('app thunkActions', () => {
};
thunkActions.initialize(data)(dispatch);
expect(dispatch.mock.calls).toEqual([
[{ type: 'resetEditor' }],
[actions.app.initialize(data)],
[thunkActions.fetchBlock()],
[thunkActions.fetchUnit()],
Expand Down Expand Up @@ -273,6 +276,7 @@ describe('app thunkActions', () => {
};
thunkActions.initialize(data)(dispatch);
expect(dispatch.mock.calls).toEqual([
[{ type: 'resetEditor' }],
[actions.app.initialize(data)],
[thunkActions.fetchBlock()],
[thunkActions.fetchUnit()],
Expand Down Expand Up @@ -310,6 +314,7 @@ describe('app thunkActions', () => {
};
thunkActions.initialize(data)(dispatch);
expect(dispatch.mock.calls).toEqual([
[{ type: 'resetEditor' }],
[actions.app.initialize(data)],
[thunkActions.fetchBlock()],
[thunkActions.fetchUnit()],
Expand Down
1 change: 1 addition & 0 deletions src/editors/data/redux/thunkActions/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ export default StrictDict({
fetchBlock,
fetchStudioView,
fetchUnit,
createBlock,
saveBlock,
fetchImages,
fetchVideos,
Expand Down

0 comments on commit e995900

Please sign in to comment.