From f94b5b7c68d6898ef65c5ce26744cdfff9f3d810 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 1 Nov 2018 14:44:55 +0100 Subject: [PATCH] Created tagDelete reducer test --- src/tags/reducers/tagDelete.js | 6 +- test/tags/reducers/tagDelete.test.js | 91 ++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 test/tags/reducers/tagDelete.test.js diff --git a/src/tags/reducers/tagDelete.js b/src/tags/reducers/tagDelete.js index 3b70f460..dfd2bf4a 100644 --- a/src/tags/reducers/tagDelete.js +++ b/src/tags/reducers/tagDelete.js @@ -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 */ diff --git a/test/tags/reducers/tagDelete.test.js b/test/tags/reducers/tagDelete.test.js new file mode 100644 index 00000000..ece3c92d --- /dev/null +++ b/test/tags/reducers/tagDelete.test.js @@ -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 }]); + }); + }); +});