mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-02-28 20:56:42 +00:00
Created tagDelete reducer test
This commit is contained in:
91
test/tags/reducers/tagDelete.test.js
Normal file
91
test/tags/reducers/tagDelete.test.js
Normal 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 }]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user