From 07b1d5be2e6085c640fa17736c03328cd7d64d6a Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Thu, 1 Nov 2018 14:55:30 +0100 Subject: [PATCH] Created shortUrlCreation reducer test --- src/short-urls/reducers/shortUrlCreation.js | 9 +- .../reducers/shortUrlCreation.test.js | 94 +++++++++++++++++++ 2 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 test/short-urls/reducers/shortUrlCreation.test.js diff --git a/src/short-urls/reducers/shortUrlCreation.js b/src/short-urls/reducers/shortUrlCreation.js index ccb4f01c..9f01a8ee 100644 --- a/src/short-urls/reducers/shortUrlCreation.js +++ b/src/short-urls/reducers/shortUrlCreation.js @@ -3,10 +3,10 @@ import PropTypes from 'prop-types'; import shlinkApiClient from '../../api/ShlinkApiClient'; /* eslint-disable padding-line-between-statements, newline-after-var */ -const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START'; -const CREATE_SHORT_URL_ERROR = 'shlink/createShortUrl/CREATE_SHORT_URL_ERROR'; -const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL'; -const RESET_CREATE_SHORT_URL = 'shlink/createShortUrl/RESET_CREATE_SHORT_URL'; +export const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START'; +export const CREATE_SHORT_URL_ERROR = 'shlink/createShortUrl/CREATE_SHORT_URL_ERROR'; +export const CREATE_SHORT_URL = 'shlink/createShortUrl/CREATE_SHORT_URL'; +export const RESET_CREATE_SHORT_URL = 'shlink/createShortUrl/RESET_CREATE_SHORT_URL'; /* eslint-enable padding-line-between-statements, newline-after-var */ export const createShortUrlResultType = PropTypes.shape({ @@ -29,6 +29,7 @@ export default function reducer(state = defaultState, action) { return { ...state, saving: true, + error: false, }; case CREATE_SHORT_URL_ERROR: return { diff --git a/test/short-urls/reducers/shortUrlCreation.test.js b/test/short-urls/reducers/shortUrlCreation.test.js new file mode 100644 index 00000000..ebf1dc04 --- /dev/null +++ b/test/short-urls/reducers/shortUrlCreation.test.js @@ -0,0 +1,94 @@ +import * as sinon from 'sinon'; +import reducer, { + CREATE_SHORT_URL_START, + CREATE_SHORT_URL_ERROR, + CREATE_SHORT_URL, + RESET_CREATE_SHORT_URL, + _createShortUrl, + resetCreateShortUrl, +} from '../../../src/short-urls/reducers/shortUrlCreation'; + +describe('shortUrlCreationReducer', () => { + describe('reducer', () => { + it('returns loading on CREATE_SHORT_URL_START', () => { + expect(reducer({}, { type: CREATE_SHORT_URL_START })).toEqual({ + saving: true, + error: false, + }); + }); + + it('returns error on CREATE_SHORT_URL_ERROR', () => { + expect(reducer({}, { type: CREATE_SHORT_URL_ERROR })).toEqual({ + saving: false, + error: true, + }); + }); + + it('returns result on CREATE_SHORT_URL', () => { + expect(reducer({}, { type: CREATE_SHORT_URL, result: 'foo' })).toEqual({ + saving: false, + error: false, + result: 'foo', + }); + }); + + it('returns default state on RESET_CREATE_SHORT_URL', () => { + expect(reducer({}, { type: RESET_CREATE_SHORT_URL })).toEqual({ + result: null, + saving: false, + error: false, + }); + }); + + it('returns provided state on unknown action', () => + expect(reducer({}, { type: 'unknown' })).toEqual({})); + }); + + describe('resetCreateShortUrl', () => { + it('returns proper action', () => + expect(resetCreateShortUrl()).toEqual({ type: RESET_CREATE_SHORT_URL })); + }); + + describe('createShortUrl', () => { + const createApiClientMock = (result) => ({ + createShortUrl: sinon.fake.returns(result), + }); + const dispatch = sinon.spy(); + + afterEach(() => dispatch.resetHistory()); + + it('calls API on success', async () => { + const expectedDispatchCalls = 2; + const result = 'foo'; + const apiClientMock = createApiClientMock(Promise.resolve(result)); + const dispatchable = _createShortUrl(apiClientMock, {}); + + await dispatchable(dispatch); + + expect(apiClientMock.createShortUrl.callCount).toEqual(1); + + expect(dispatch.callCount).toEqual(expectedDispatchCalls); + expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]); + expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL, result }]); + }); + + it('throws on error', async () => { + const expectedDispatchCalls = 2; + const error = 'Error'; + const apiClientMock = createApiClientMock(Promise.reject(error)); + const dispatchable = _createShortUrl(apiClientMock, {}); + + try { + await dispatchable(dispatch); + } catch (e) { + expect(e).toEqual(error); + } + + expect(apiClientMock.createShortUrl.callCount).toEqual(1); + + expect(dispatch.callCount).toEqual(expectedDispatchCalls); + expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]); + expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL_ERROR }]); + }); + }); +});