Skip to content

Commit

Permalink
test: add testing for request
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoa committed Dec 26, 2024
1 parent 6af6ee7 commit f70d770
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/editors/containers/ProblemEditor/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jest.mock('../../data/redux', () => ({
selectors: {
app: {
blockValue: jest.fn(state => ({ blockValue: state })),
isCreateBlock: jest.fn(state => ({ isCreateBlock: state })),
},
problem: {
problemType: jest.fn(state => ({ problemType: state })),
Expand Down Expand Up @@ -104,12 +105,18 @@ describe('ProblemEditor', () => {
test('blockFinished from requests.isFinished', () => {
expect(
mapStateToProps(testState).blockFinished,
).toEqual(selectors.requests.isFinished(testState, { requestKey: RequestKeys.fetchBlock }));
).toEqual(
selectors.app.isCreateBlock(testState)
|| selectors.requests.isFinished(testState, { requestKey: RequestKeys.fetchBlock })

Check failure on line 110 in src/editors/containers/ProblemEditor/index.test.tsx

View workflow job for this annotation

GitHub Actions / tests

Missing trailing comma
);
});
test('advancedSettingsFinished from requests.isFinished', () => {
expect(
mapStateToProps(testState).advancedSettingsFinished,
).toEqual(selectors.requests.isFinished(testState, { requestKey: RequestKeys.fetchAdvancedSettings }));
).toEqual(
selectors.app.isCreateBlock(testState)
|| selectors.requests.isFinished(testState, { requestKey: RequestKeys.fetchAdvancedSettings })

Check failure on line 118 in src/editors/containers/ProblemEditor/index.test.tsx

View workflow job for this annotation

GitHub Actions / tests

Missing trailing comma
);
});
});
describe('mapDispatchToProps', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/editors/data/constants/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const RequestKeys = StrictDict({
fetchImages: 'fetchImages',
fetchUnit: 'fetchUnit',
fetchStudioView: 'fetchStudioView',
creaateBlock: 'createBlock',
createBlock: 'createBlock',
saveBlock: 'saveBlock',
uploadVideo: 'uploadVideo',
allowThumbnailUpload: 'allowThumbnailUpload',
Expand Down
16 changes: 16 additions & 0 deletions src/editors/data/redux/app/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('app selectors unit tests', () => {
simpleSelectors.unitUrl,
simpleSelectors.blockValue,
selectors.isLibrary,
selectors.isCreateBlock,
]);
});
describe('for library blocks', () => {
Expand Down Expand Up @@ -118,6 +119,16 @@ describe('app selectors unit tests', () => {
].map(([args, expected]) => expect(cb(...args)).toEqual(expected));
});
});
describe('component creation workflow', () => {

Check failure on line 122 in src/editors/data/redux/app/selectors.test.ts

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
it('returns true if is isCreateBlock is truthy', () => {
const { resultFunc: cb } = selectors.isInitialized;

[
[[null, null, true, true] as [any, any, any, any], true] as const,
[[null, null, true, true] as [any, any, any, any], true] as const,
].map(([args, expected]) => expect(cb(...args)).toEqual(expected));
});
});
});
describe('displayTitle', () => {
const title = 'tItLe';
Expand Down Expand Up @@ -184,4 +195,9 @@ describe('app selectors unit tests', () => {
});
});
});
describe('isCreateBlock', () => {
it('should return false if the editor is initialized with a blockId', () => {
expect(selectors.isCreateBlock.resultFunc('block-v1:', 'text')).toEqual(false);
});
});
});
3 changes: 1 addition & 2 deletions src/editors/data/redux/thunkActions/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export const fetchCourseDetails = () => (dispatch) => {
export const initialize = (data) => (dispatch) => {
const editorType = data.blockType;
dispatch(actions.app.initialize(data));

if (data.blockId === '' && data.blockType) {
if (data.blockId === '' && editorType) {
dispatch(actions.app.initializeEditor());
return;
}
Expand Down
36 changes: 36 additions & 0 deletions src/editors/data/redux/thunkActions/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as thunkActions from './app';
jest.mock('./requests', () => ({
fetchBlock: (args) => ({ fetchBlock: args }),
fetchUnit: (args) => ({ fetchUnit: args }),
createBlock: (args) => ({ createBlock: args }),
saveBlock: (args) => ({ saveBlock: args }),
uploadAsset: (args) => ({ uploadAsset: args }),
fetchStudioView: (args) => ({ fetchStudioView: args }),
Expand Down Expand Up @@ -196,6 +197,21 @@ describe('app thunkActions', () => {
thunkActions.fetchCourseDetails = fetchCourseDetails;
});
});
describe('initialize without block id but block type defined', () => {
it('dispatches actions.app.initialize, and then fetches both block and unit', () => {
const data = {
...testValue,
blockType: 'html',
blockId: '',
learningContextId: 'course-v1:UniversityX+PHYS+1',
};
thunkActions.initialize(data)(dispatch);
expect(dispatch.mock.calls).toEqual([
[actions.app.initialize(data)],
[actions.app.initializeEditor()],
]);
});
});
describe('initialize with block type html', () => {
it('dispatches actions.app.initialize, and then fetches both block and unit', () => {
const {
Expand Down Expand Up @@ -333,6 +349,26 @@ describe('app thunkActions', () => {
expect(returnToUnit).toHaveBeenCalled();
});
});
describe('createBlock', () => {
let returnToUnit;
beforeEach(() => {
returnToUnit = jest.fn();
thunkActions.createBlock(testValue, returnToUnit)(dispatch);
[[dispatchedAction]] = dispatch.mock.calls;
});
it('dispatches createBlock', () => {
expect(dispatchedAction.createBlock).not.toBe(undefined);
});
test('onSuccess: calls setBlockId and dispatches saveBlock', () => {
const {
saveBlock,
} = thunkActions;
thunkActions.saveBlock = saveBlock;

dispatchedAction.createBlock.onSuccess({ id: 'library' });
expect(dispatch).toHaveBeenCalledWith(actions.app.setBlockId('library'));
});
});
describe('uploadAsset', () => {
const setSelection = jest.fn();
beforeEach(() => {
Expand Down
5 changes: 2 additions & 3 deletions src/editors/data/redux/thunkActions/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ export const saveBlock = ({ content, ...rest }) => (dispatch, getState) => {
};

/**
* Tracked saveBlock api method. Tracked to the `saveBlock` request key.
* @param {string} content
* Tracked createBlock api method. Tracked to the `createBlock` request key.
* @param {[func]} onSuccess - onSuccess method ((response) => { ... })
* @param {[func]} onFailure - onFailure method ((error) => { ... })
*/
Expand All @@ -137,7 +136,7 @@ export const createBlock = ({ ...rest }) => (dispatch, getState) => {
: `${uuid4()}`;

dispatch(module.networkRequest({
requestKey: RequestKeys.creaateBlock,
requestKey: RequestKeys.createBlock,
promise: createLibraryBlock({
libraryId: selectors.app.learningContextId(getState()),
blockType: selectors.app.blockType(getState()),
Expand Down
22 changes: 21 additions & 1 deletion src/editors/data/redux/thunkActions/requests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RequestKeys } from '../../constants/requests';
import api from '../../services/cms/api';
import * as requests from './requests';
import { actions, selectors } from '../index';
import { createLibraryBlock } from '../../../../library-authoring/data/api';

const testState = {
some: 'data',
Expand All @@ -17,7 +18,7 @@ jest.mock('../app/selectors', () => ({
blockId: (state) => ({ blockId: state }),
blockType: (state) => ({ blockType: state }),
learningContextId: (state) => ({ learningContextId: state }),
blockTitle: (state) => ({ title: state }),
blockTitle: (state) => state.data,
}));

jest.mock('../../services/cms/api', () => ({
Expand All @@ -41,6 +42,10 @@ jest.mock('../../services/cms/api', () => ({
uploadVideo: (args) => args,
}));

jest.mock('../../../../library-authoring/data/api', () => ({
createLibraryBlock: ({ id, url }) => ({ id, url }),
}));

jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'),
parseLibraryImageData: jest.fn(),
Expand Down Expand Up @@ -349,6 +354,21 @@ describe('requests thunkActions module', () => {
},
});
});
describe('createBlock', () => {
testNetworkRequestAction({
action: requests.createBlock,
args: { ...fetchParams },
expectedString: 'with create promise',
expectedData: {
...fetchParams,
requestKey: RequestKeys.createBlock,
promise: createLibraryBlock({
libraryId: selectors.app.learningContextId(testState),
blockType: selectors.app.blockType(testState),
}),
},
});
});
describe('uploadAsset', () => {
const asset = 'SoME iMage CoNtent As String';
let uploadAsset;
Expand Down

0 comments on commit f70d770

Please sign in to comment.