Created tagDelete reducer test

This commit is contained in:
Alejandro Celaya 2018-11-01 14:44:55 +01:00
parent 824a2facac
commit f94b5b7c68
2 changed files with 94 additions and 3 deletions
src/tags/reducers
test/tags/reducers

View file

@ -3,9 +3,9 @@ import PropTypes from 'prop-types';
import shlinkApiClient from '../../api/ShlinkApiClient';
/* eslint-disable padding-line-between-statements, newline-after-var */
const DELETE_TAG_START = 'shlink/deleteTag/DELETE_TAG_START';
const DELETE_TAG_ERROR = 'shlink/deleteTag/DELETE_TAG_ERROR';
const DELETE_TAG = 'shlink/deleteTag/DELETE_TAG';
export const DELETE_TAG_START = 'shlink/deleteTag/DELETE_TAG_START';
export const DELETE_TAG_ERROR = 'shlink/deleteTag/DELETE_TAG_ERROR';
export const DELETE_TAG = 'shlink/deleteTag/DELETE_TAG';
export const TAG_DELETED = 'shlink/deleteTag/TAG_DELETED';
/* eslint-enable padding-line-between-statements, newline-after-var */

View file

@ -0,0 +1,91 @@
import * as sinon from 'sinon';
import reducer, {
DELETE_TAG_START,
DELETE_TAG_ERROR,
DELETE_TAG,
TAG_DELETED,
tagDeleted,
_deleteTag,
} from '../../../src/tags/reducers/tagDelete';
describe('tagDeleteReducer', () => {
describe('reducer', () => {
it('returns loading on DELETE_TAG_START', () => {
expect(reducer({}, { type: DELETE_TAG_START })).toEqual({
deleting: true,
error: false,
});
});
it('returns error on DELETE_TAG_ERROR', () => {
expect(reducer({}, { type: DELETE_TAG_ERROR })).toEqual({
deleting: false,
error: true,
});
});
it('returns tag names on DELETE_TAG', () => {
expect(reducer({}, { type: DELETE_TAG })).toEqual({
deleting: false,
error: false,
});
});
it('returns provided state on unknown action', () =>
expect(reducer({}, { type: 'unknown' })).toEqual({}));
});
describe('tagDeleted', () => {
it('returns action based on provided params', () =>
expect(tagDeleted('foo')).toEqual({
type: TAG_DELETED,
tag: 'foo',
}));
});
describe('deleteTag', () => {
const createApiClientMock = (result) => ({
deleteTags: sinon.fake.returns(result),
});
const dispatch = sinon.spy();
afterEach(() => dispatch.resetHistory());
it('calls API on success', async () => {
const expectedDispatchCalls = 2;
const tag = 'foo';
const apiClientMock = createApiClientMock(Promise.resolve());
const dispatchable = _deleteTag(apiClientMock, tag);
await dispatchable(dispatch);
expect(apiClientMock.deleteTags.callCount).toEqual(1);
expect(apiClientMock.deleteTags.getCall(0).args).toEqual([[ tag ]]);
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_TAG_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_TAG }]);
});
it('throws on error', async () => {
const expectedDispatchCalls = 2;
const error = 'Error';
const tag = 'foo';
const apiClientMock = createApiClientMock(Promise.reject(error));
const dispatchable = _deleteTag(apiClientMock, tag);
try {
await dispatchable(dispatch);
} catch (e) {
expect(e).toEqual(error);
}
expect(apiClientMock.deleteTags.callCount).toEqual(1);
expect(apiClientMock.deleteTags.getCall(0).args).toEqual([[ tag ]]);
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_TAG_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_TAG_ERROR }]);
});
});
});