Skip to content

Commit

Permalink
test: update testing
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Jan 24, 2025
1 parent a8f286c commit fc1b4cf
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/editors/data/redux/thunkActions/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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' };
Expand All @@ -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 }));
});
});
});
50 changes: 50 additions & 0 deletions src/editors/hooks.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jest.mock('./data/redux', () => ({
app: {
initialize: (args) => ({ initializeApp: args }),
saveBlock: (args) => ({ saveBlock: args }),
createBlock: (args) => ({ createBlock: args }),
},
},
}));
Expand Down Expand Up @@ -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();
Expand All @@ -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,
}));
});
});
});

0 comments on commit fc1b4cf

Please sign in to comment.