From b8d3291cfff14cdb5e9cc1a1112971df0abf8335 Mon Sep 17 00:00:00 2001 From: Diana Olarte Date: Tue, 5 Nov 2024 07:37:01 +1100 Subject: [PATCH] test: fix current test --- src/editors/data/redux/thunkActions/app.js | 6 +-- .../data/redux/thunkActions/requests.test.js | 39 ++++++++++++------- .../SelectionModal/GalleryCard.jsx | 2 + .../__snapshots__/index.test.jsx.snap | 4 +- .../TinyMceWidget/index.test.jsx | 2 +- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/editors/data/redux/thunkActions/app.js b/src/editors/data/redux/thunkActions/app.js index c67896a28a..15b696d6f3 100644 --- a/src/editors/data/redux/thunkActions/app.js +++ b/src/editors/data/redux/thunkActions/app.js @@ -1,14 +1,14 @@ import { StrictDict, camelizeKeys } from '../../../utils'; +import { isLibraryKey } from '../../../../generic/key-utils'; import * as requests from './requests'; // This 'module' self-import hack enables mocking during tests. // See src/editors/decisions/0005-internal-editor-testability-decisions.md. The whole approach to how hooks are tested // should be re-thought and cleaned up to avoid this pattern. // eslint-disable-next-line import/no-self-import import * as module from './app'; -import { actions as appActions } from '../app'; +import { actions as appActions, selectors } from '../app'; import { actions as requestsActions } from '../requests'; import { RequestKeys } from '../../constants/requests'; -import { isLibrary } from '../app/selectors'; // Similar to `import { actions } from '..';` but avoid circular imports: const actions = { @@ -103,7 +103,7 @@ export const initialize = (data) => (dispatch) => { dispatch(module.fetchCourseDetails()); break; case 'html': - if (isLibrary) { dispatch(actions.app.resetImages()); } + if (isLibraryKey(data.learningContextId)) { dispatch(actions.app.resetImages()); } dispatch(module.fetchImages({ pageNumber: 0 })); break; default: diff --git a/src/editors/data/redux/thunkActions/requests.test.js b/src/editors/data/redux/thunkActions/requests.test.js index 4b5961b9eb..a71f9f52b3 100644 --- a/src/editors/data/redux/thunkActions/requests.test.js +++ b/src/editors/data/redux/thunkActions/requests.test.js @@ -242,6 +242,7 @@ describe('requests thunkActions module', () => { let loadImages; let dispatchedAction; const expectedArgs = { + blockId: selectors.app.blockId(testState), studioEndpointUrl: selectors.app.studioEndpointUrl(testState), learningContextId: selectors.app.learningContextId(testState), }; @@ -316,19 +317,31 @@ describe('requests thunkActions module', () => { }); describe('uploadAsset', () => { const asset = 'SoME iMage CoNtent As String'; - testNetworkRequestAction({ - action: requests.uploadAsset, - args: { asset, ...fetchParams }, - expectedString: 'with uploadAsset promise', - expectedData: { - ...fetchParams, - requestKey: RequestKeys.uploadAsset, - promise: api.uploadAsset({ - learningContextId: selectors.app.learningContextId(testState), - asset, - studioEndpointUrl: selectors.app.studioEndpointUrl(testState), - }), - }, + let uploadAsset; + let dispatchedAction; + const expectedArgs = { + learningContextId: selectors.app.learningContextId(testState), + studioEndpointUrl: selectors.app.studioEndpointUrl(testState), + blockId: selectors.app.blockId(testState), + asset, + }; + beforeEach(() => { + uploadAsset = jest.fn((args) => new Promise((resolve) => { + resolve({ data: { asset: args } }); + })); + jest.spyOn(api, apiKeys.uploadAsset).mockImplementationOnce(uploadAsset); + requests.uploadAsset({ asset, ...fetchParams, onSuccess, onFailure })(dispatch, () => testState); + [[dispatchedAction]] = dispatch.mock.calls; + }); + it('dispatches networkRequest', () => { + expect(dispatchedAction.networkRequest).not.toEqual(undefined); + }); + test('forwards onSuccess and onFailure', () => { + expect(dispatchedAction.networkRequest.onSuccess).toEqual(onSuccess); + expect(dispatchedAction.networkRequest.onFailure).toEqual(onFailure); + }); + test('api.fetchImages promise called with studioEndpointUrl and learningContextId', () => { + expect(uploadAsset).toHaveBeenCalledWith(expectedArgs); }); }); describe('uploadThumbnail', () => { diff --git a/src/editors/sharedComponents/SelectionModal/GalleryCard.jsx b/src/editors/sharedComponents/SelectionModal/GalleryCard.jsx index e1864b3991..4ffcb63af2 100644 --- a/src/editors/sharedComponents/SelectionModal/GalleryCard.jsx +++ b/src/editors/sharedComponents/SelectionModal/GalleryCard.jsx @@ -75,6 +75,7 @@ const GalleryCard = ({ /> )} + {asset.dateAdded && (

+ )} diff --git a/src/editors/sharedComponents/TinyMceWidget/__snapshots__/index.test.jsx.snap b/src/editors/sharedComponents/TinyMceWidget/__snapshots__/index.test.jsx.snap index 01e328f8cc..feb3d25635 100644 --- a/src/editors/sharedComponents/TinyMceWidget/__snapshots__/index.test.jsx.snap +++ b/src/editors/sharedComponents/TinyMceWidget/__snapshots__/index.test.jsx.snap @@ -34,8 +34,8 @@ exports[`TinyMceWidget snapshots ImageUploadModal is not rendered 1`] = ` ], }, "initializeEditor": undefined, - "isLibrary": true, - "learningContextId": "course+org+run", + "isLibrary": false, + "learningContextId": "library-v1:org+t01", "lmsEndpointUrl": "sOmEvaLue.cOm", "minHeight": undefined, "openImgModal": [MockFunction modal.openModal], diff --git a/src/editors/sharedComponents/TinyMceWidget/index.test.jsx b/src/editors/sharedComponents/TinyMceWidget/index.test.jsx index 741e196ad3..bce15315de 100644 --- a/src/editors/sharedComponents/TinyMceWidget/index.test.jsx +++ b/src/editors/sharedComponents/TinyMceWidget/index.test.jsx @@ -74,7 +74,7 @@ describe('TinyMceWidget', () => { expect(wrapper.instance.findByType(SourceCodeModal).length).toBe(0); }); test('ImageUploadModal is not rendered', () => { - const wrapper = shallow(); + const wrapper = shallow(); expect(wrapper.snapshot).toMatchSnapshot(); expect(wrapper.instance.findByType(ImageUploadModal).length).toBe(0); });