From fc1b4cf003711f077af17a04f3230bff7342c78e Mon Sep 17 00:00:00 2001 From: Diana Olarte Date: Fri, 24 Jan 2025 14:41:43 +1100 Subject: [PATCH] test: update testing --- .../data/redux/thunkActions/app.test.js | 28 +++++++++-- src/editors/hooks.test.jsx | 50 +++++++++++++++++++ 2 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/editors/data/redux/thunkActions/app.test.js b/src/editors/data/redux/thunkActions/app.test.js index 099a7419b..769e8a024 100644 --- a/src/editors/data/redux/thunkActions/app.test.js +++ b/src/editors/data/redux/thunkActions/app.test.js @@ -36,7 +36,7 @@ describe('app thunkActions', () => { let getState; beforeEach(() => { dispatch = jest.fn((action) => ({ dispatch: action })); - getState = jest.fn(() => ({ app: { blockId: 'blockId', images: {} } })); + getState = jest.fn().mockImplementation(() => ({ app: { blockId: 'blockId', images: {} } })); }); describe('fetchBlock', () => { beforeEach(() => { @@ -374,6 +374,14 @@ describe('app thunkActions', () => { expect(dispatch).toHaveBeenCalledWith(actions.app.setBlockId(data.id)); expect(dispatch.mock.calls.length).toBe(3); }); + test('onFailure: dispatches failRequest', () => { + const error = new Error('fail create a new component'); + dispatchedAction.createBlock.onFailure(error); + expect(dispatch).toHaveBeenCalledWith(actions.requests.failRequest({ + requestKey: RequestKeys.createBlock, + error, + })); + }); test('should call batchUploadAssets if the block has images', () => { getState.mockReturnValueOnce({ app: { blockId: '', images: mockImageData } }); const data = { id: 'block-id' }; @@ -384,19 +392,31 @@ describe('app thunkActions', () => { }); describe('uploadAsset', () => { const setSelection = jest.fn(); - beforeEach(() => { + + it('dispatches uploadAsset action', () => { thunkActions.uploadAsset({ file: testValue, setSelection })(dispatch, getState); [[dispatchedAction]] = dispatch.mock.calls; - }); - it('dispatches uploadAsset action', () => { expect(dispatchedAction.uploadAsset).not.toBe(undefined); }); test('passes file as image prop', () => { + thunkActions.uploadAsset({ file: testValue, setSelection })(dispatch, getState); + [[dispatchedAction]] = dispatch.mock.calls; expect(dispatchedAction.uploadAsset.asset).toEqual(testValue); }); test('onSuccess: calls setSelection with camelized response.data.asset', () => { + thunkActions.uploadAsset({ file: testValue, setSelection })(dispatch, getState); + [[dispatchedAction]] = dispatch.mock.calls; dispatchedAction.uploadAsset.onSuccess({ data: { asset: testValue } }); expect(setSelection).toHaveBeenCalledWith(camelizeKeys(testValue)); }); + test('should save a new image temporary when is create workflow', () => { + getState.mockImplementation(() => ({ app: { blockId: '', blockType: 'html' } })); + global.URL.createObjectURL = jest.fn(); + thunkActions.uploadAsset({ file: testValue, setSelection })(dispatch, getState); + [[dispatchedAction]] = dispatch.mock.calls; + expect(URL.createObjectURL).toHaveBeenCalledWith(testValue); + expect(setSelection).toHaveBeenCalled(); + expect(dispatch).toHaveBeenCalledWith(actions.app.setImages({ images: expect.any(Object), imageCount: 1 })); + }); }); }); diff --git a/src/editors/hooks.test.jsx b/src/editors/hooks.test.jsx index 103577a98..d1a2f1120 100644 --- a/src/editors/hooks.test.jsx +++ b/src/editors/hooks.test.jsx @@ -25,6 +25,7 @@ jest.mock('./data/redux', () => ({ app: { initialize: (args) => ({ initializeApp: args }), saveBlock: (args) => ({ saveBlock: args }), + createBlock: (args) => ({ createBlock: args }), }, }, })); @@ -165,6 +166,45 @@ describe('hooks', () => { }); }); + describe('createBlock', () => { + const navigateCallback = (args) => ({ navigateCallback: args }); + const dispatch = jest.fn(); + const destination = 'uRLwhENsAved'; + const analytics = 'dATAonEveNT'; + + beforeEach(() => { + jest.clearAllMocks(); + jest.spyOn(hooks, hookKeys.navigateCallback).mockImplementationOnce(navigateCallback); + }); + it('returns null when content is null', () => { + const content = null; + const expected = hooks.createBlock({ + content, + destination, + analytics, + dispatch, + }); + expect(expected).toEqual(undefined); + }); + it('dispatches thunkActions.app.createBlock with navigateCallback, and passed content', () => { + const content = 'myContent'; + hooks.createBlock({ + content, + destination, + analytics, + dispatch, + }); + expect(dispatch).toHaveBeenCalledWith(thunkActions.app.createBlock( + content, + navigateCallback({ + destination, + analyticsEvent: analyticsEvt.editorSaveClick, + analytics, + }), + )); + }); + }); + describe('clearSaveError', () => { it('dispatches actions.requests.clearRequest with saveBlock requestKey', () => { const dispatch = jest.fn(); @@ -174,4 +214,14 @@ describe('hooks', () => { })); }); }); + + describe('clearCreateError', () => { + it('dispatches actions.requests.clearRequest with createBlock requestKey', () => { + const dispatch = jest.fn(); + hooks.clearCreateError({ dispatch })(); + expect(dispatch).toHaveBeenCalledWith(actions.requests.clearRequest({ + requestKey: RequestKeys.createBlock, + })); + }); + }); });