From caa6f7bcd8af4b64226eb0edb7ab959c79474227 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 19 Jan 2020 20:21:59 +0100 Subject: [PATCH] Created shortUrlMetaReducer test --- test/short-urls/reducers/shortUrlMeta.test.js | 78 +++++++++++++++++++ test/short-urls/reducers/shortUrlTags.test.js | 3 +- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 test/short-urls/reducers/shortUrlMeta.test.js diff --git a/test/short-urls/reducers/shortUrlMeta.test.js b/test/short-urls/reducers/shortUrlMeta.test.js new file mode 100644 index 00000000..1fa74036 --- /dev/null +++ b/test/short-urls/reducers/shortUrlMeta.test.js @@ -0,0 +1,78 @@ +import moment from 'moment'; +import reducer, { + EDIT_SHORT_URL_META_START, + EDIT_SHORT_URL_META_ERROR, + SHORT_URL_META_EDITED, + editShortUrlMeta, +} from '../../../src/short-urls/reducers/shortUrlMeta'; + +describe('shortUrlMetaReducer', () => { + const meta = { + maxVisits: 50, + startDate: moment('2020-01-01').format(), + }; + const shortCode = 'abc123'; + + describe('reducer', () => { + it('returns loading on EDIT_SHORT_URL_META_START', () => { + expect(reducer({}, { type: EDIT_SHORT_URL_META_START })).toEqual({ + saving: true, + error: false, + }); + }); + + it('returns error on EDIT_SHORT_URL_META_ERROR', () => { + expect(reducer({}, { type: EDIT_SHORT_URL_META_ERROR })).toEqual({ + saving: false, + error: true, + }); + }); + + it('returns provided tags and shortCode on SHORT_URL_META_EDITED', () => { + expect(reducer({}, { type: SHORT_URL_META_EDITED, meta, shortCode })).toEqual({ + meta, + shortCode, + saving: false, + error: false, + }); + }); + }); + + describe('editShortUrlMeta', () => { + const updateShortUrlMeta = jest.fn().mockResolvedValue({}); + const buildShlinkApiClient = jest.fn().mockResolvedValue({ updateShortUrlMeta }); + const dispatch = jest.fn(); + + afterEach(jest.clearAllMocks); + + it('dispatches metadata on success', async () => { + await editShortUrlMeta(buildShlinkApiClient)(shortCode, meta)(dispatch); + + expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); + expect(updateShortUrlMeta).toHaveBeenCalledTimes(1); + expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, meta); + expect(dispatch).toHaveBeenCalledTimes(2); + expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_META_START }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: SHORT_URL_META_EDITED, meta, shortCode }); + }); + + it('dispatches error on failure', async () => { + const error = new Error(); + + updateShortUrlMeta.mockRejectedValue(error); + + try { + await editShortUrlMeta(buildShlinkApiClient)(shortCode, meta)(dispatch); + } catch (e) { + expect(e).toBe(error); + } + + expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); + expect(updateShortUrlMeta).toHaveBeenCalledTimes(1); + expect(updateShortUrlMeta).toHaveBeenCalledWith(shortCode, meta); + expect(dispatch).toHaveBeenCalledTimes(2); + expect(dispatch).toHaveBeenNthCalledWith(1, { type: EDIT_SHORT_URL_META_START }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: EDIT_SHORT_URL_META_ERROR }); + }); + }); +}); diff --git a/test/short-urls/reducers/shortUrlTags.test.js b/test/short-urls/reducers/shortUrlTags.test.js index e5c0ea52..12cbeb39 100644 --- a/test/short-urls/reducers/shortUrlTags.test.js +++ b/test/short-urls/reducers/shortUrlTags.test.js @@ -1,10 +1,11 @@ import reducer, { EDIT_SHORT_URL_TAGS, EDIT_SHORT_URL_TAGS_ERROR, - EDIT_SHORT_URL_TAGS_START, editShortUrlTags, + EDIT_SHORT_URL_TAGS_START, RESET_EDIT_SHORT_URL_TAGS, resetShortUrlsTags, SHORT_URL_TAGS_EDITED, + editShortUrlTags, shortUrlTagsEdited, } from '../../../src/short-urls/reducers/shortUrlTags';