test: cms api tests + extra lint (#25)

This commit is contained in:
Raymond Zhou
2022-03-01 11:32:03 -05:00
committed by GitHub
parent 9c9d3c8fdf
commit 6f0f6296e4
7 changed files with 91 additions and 73 deletions

View File

@@ -23,11 +23,11 @@ jest.mock('../../data/redux', () => ({
}));
jest.mock('.', () => ({
__esModule: true, // Use it when dealing with esModules
...jest.requireActual('./index'),
handleCancelClicked: jest.fn(args => ({ handleCancelClicked: args })),
handleSaveClicked: jest.fn(args => ({ handleSaveClicked: args })),
}
__esModule: true, // Use it when dealing with esModules
...jest.requireActual('./index'),
handleCancelClicked: jest.fn(args => ({ handleCancelClicked: args })),
handleSaveClicked: jest.fn(args => ({ handleSaveClicked: args })),
}
));
jest.mock('../../hooks', () => ({

View File

@@ -57,11 +57,11 @@ describe('HeaderTitle', () => {
expect(shallow(<module.HeaderTitle {...props} />)).toMatchSnapshot();
});
test('initialized', () => {
localTitleHooks.mockReturnValue(hookProps);
localTitleHooks.mockReturnValue(localTitleHooksProps);
expect(shallow(<module.HeaderTitle {...props} isInitialized />)).toMatchSnapshot();
});
test('editing', () => {
localTitleHooks.mockReturnValue({ ...hookProps, isEditing: true });
localTitleHooks.mockReturnValue({ ...localTitleHooksProps, isEditing: true });
expect(shallow(<module.HeaderTitle {...props} isInitialized />)).toMatchSnapshot();
});
});

View File

@@ -132,7 +132,7 @@ describe('EditorHeader hooks', () => {
expect(output.startEditing).toEqual(values.startEditing);
expect(output.stopEditing).toEqual(values.stopEditing);
});
it('returns localTitle, updateTitle, and handleChange, tied to the localTitle hook', () => {
it('returns localTitle, updateTitle, and handleChange, tied to the localTitle hook', () => {
expect(output.updateTitle).toEqual({
setBlockTitle,
stopEditing: values.stopEditing,

View File

@@ -2,7 +2,9 @@ import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { ActionRow, IconButton, Icon, ModalDialog } from '@edx/paragon';
import {
ActionRow, IconButton, Icon, ModalDialog,
} from '@edx/paragon';
import { Close } from '@edx/paragon/icons';
import { selectors } from '../../data/redux';

View File

@@ -31,9 +31,8 @@ describe('Editor Header index', () => {
returnUrl: 'TeST-ReTurNurL',
};
const { EditorHeader } = module;
let el;
el = shallow(<EditorHeader {...props} />);
const el = shallow(<EditorHeader {...props} />);
describe('behavior', () => {
test('IconButton onClick calls navigateCallback', () => {
const iconButtonControl = el.find(IconButton);

View File

@@ -1,66 +1,83 @@
import axios from 'axios'; // eslint-disable-line import/no-extraneous-dependencies
import { fetchBlockById, fetchUnitById, saveBlock } from './api';
import {
fetchBlockById, fetchByUnitId, normalizeContent, saveBlock,
} from './api';
import * as urls from './urls';
import { get, post } from './utils';
const get = jest.spyOn(axios, 'get');
const post = jest.spyOn(axios, 'post');
jest.mock('./urls', () => ({
block: jest.fn().mockName('urls.block'),
blockAncestor: jest.fn().mockName('urls.blockAncestor'),
}));
jest.mock('./utils', () => ({
get: jest.fn().mockName('get'),
post: jest.fn().mockName('post'),
}));
const saveFunctionsGet = {
setValue: jest.fn(),
setError: jest.fn(),
setLoading: jest.fn(),
};
const saveFunctionsSave = {
setResponse: jest.fn(),
setInProgress: jest.fn(),
};
const blockId = 'coursev1:2uX@4345432';
const content = 'Im baby palo santo ugh celiac fashion axe. La croix lo-fi venmo whatever. Beard man braid migas single-origin coffee forage ramps.';
const courseId = 'demo2uX';
const studioEndpointUrl = 'hortus.coa';
const title = 'remember this needs to go into metadata to save';
test('fetchBlockById 404', () => {
get.mockRejectedValue({ response: { status: 404 } });
fetchBlockById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('fetchBlockById 403', () => {
get.mockRejectedValue({ response: { status: 403 } });
fetchBlockById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('fetchBlockById 408', () => {
get.mockRejectedValue({ response: { status: 408 } });
fetchBlockById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('fetchBlockById 404', () => {
get.mockRejectedValue({ response: { status: 404 } });
fetchBlockById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('fetchUnitById 401', () => {
get.mockRejectedValue({ response: { status: 401 } });
fetchUnitById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('fetchUnitById 404', () => {
get.mockRejectedValue({ response: { status: 404 } });
fetchUnitById(saveFunctionsGet, blockId, studioEndpointUrl);
expect(saveFunctionsGet.setLoading).toHaveBeenCalled();
expect(saveFunctionsGet.setError).toHaveBeenCalled();
});
test('saveBlock 408', () => {
post.mockRejectedValue({ response: { status: 408 } });
saveBlock(blockId, 'html', 'demo2uX', studioEndpointUrl, 'Im baby palo santo ugh celiac fashion axe. La croix lo-fi venmo whatever. Beard man braid migas single-origin coffee forage ramps.', saveFunctionsSave);
expect(saveFunctionsSave.setInProgress).toHaveBeenCalled();
expect(saveFunctionsSave.setResponse).toHaveBeenCalled();
});
test('saveBlock 404', () => {
post.mockRejectedValue({ response: { status: 404 } });
saveBlock(blockId, 'html', 'demo2uX', studioEndpointUrl, 'Im baby palo santo ugh celiac fashion axe. La croix lo-fi venmo whatever. Beard man braid migas single-origin coffee forage ramps.', saveFunctionsSave);
expect(saveFunctionsSave.setInProgress).toHaveBeenCalled();
expect(saveFunctionsSave.setResponse).toHaveBeenCalled();
describe('cms api', () => {
describe('fetchBlockId', () => {
it('should call get with url.blocks', () => {
fetchBlockById({ blockId, studioEndpointUrl });
expect(get).toHaveBeenCalledWith(urls.block({ blockId, studioEndpointUrl }));
});
});
describe('fetchByUnitId', () => {
it('should call get with url.blockAncestor', () => {
fetchByUnitId({ blockId, studioEndpointUrl });
expect(get).toHaveBeenCalledWith(urls.blockAncestor({ studioEndpointUrl, blockId }));
});
});
describe('normalizeContent', () => {
test('return value for blockType: html', () => {
expect(normalizeContent({
blockId,
blockType: 'html',
content,
courseId,
title,
})).toEqual({
category: 'html',
couseKey: courseId,
data: content,
has_changes: true,
id: blockId,
metadata: { display_name: title },
});
});
test('throw error for invalid blockType', () => {
expect(() => { normalizeContent({ blockType: 'somethingINVALID' }); })
.toThrow(TypeError);
});
});
describe('saveBlock', () => {
it('should call post with urls.block and normalizeContent', () => {
saveBlock({
blockId,
blockType: 'html',
content,
courseId,
studioEndpointUrl,
title,
});
expect(post).toHaveBeenCalledWith(
urls.block({ studioEndpointUrl }),
normalizeContent({
blockType: 'html',
content,
blockId,
courseId,
title,
}),
);
});
});
});

View File

@@ -34,4 +34,4 @@ describe('cms url methods', () => {
.toEqual(`${block({ studioEndpointUrl, blockId })}?fields=ancestorInfo`);
});
});
});
});