From d3491869bd8edb0cfc2207dda0000b9fd4a22b19 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 19 May 2019 12:54:19 +0200 Subject: [PATCH] Created tags list reducer test --- src/tags/reducers/tagsList.js | 8 +- test/tags/reducers/tagsList.test.js | 144 ++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 test/tags/reducers/tagsList.test.js diff --git a/src/tags/reducers/tagsList.js b/src/tags/reducers/tagsList.js index 622389c1..684a64d3 100644 --- a/src/tags/reducers/tagsList.js +++ b/src/tags/reducers/tagsList.js @@ -4,10 +4,10 @@ import { TAG_DELETED } from './tagDelete'; import { TAG_EDITED } from './tagEdit'; /* eslint-disable padding-line-between-statements */ -const LIST_TAGS_START = 'shlink/tagsList/LIST_TAGS_START'; -const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR'; -const LIST_TAGS = 'shlink/tagsList/LIST_TAGS'; -const FILTER_TAGS = 'shlink/tagsList/FILTER_TAGS'; +export const LIST_TAGS_START = 'shlink/tagsList/LIST_TAGS_START'; +export const LIST_TAGS_ERROR = 'shlink/tagsList/LIST_TAGS_ERROR'; +export const LIST_TAGS = 'shlink/tagsList/LIST_TAGS'; +export const FILTER_TAGS = 'shlink/tagsList/FILTER_TAGS'; /* eslint-enable padding-line-between-statements */ const initialState = { diff --git a/test/tags/reducers/tagsList.test.js b/test/tags/reducers/tagsList.test.js new file mode 100644 index 00000000..e3fe168c --- /dev/null +++ b/test/tags/reducers/tagsList.test.js @@ -0,0 +1,144 @@ +import reducer, { + FILTER_TAGS, + filterTags, + LIST_TAGS, + LIST_TAGS_ERROR, + LIST_TAGS_START, listTags, +} from '../../../src/tags/reducers/tagsList'; +import { TAG_DELETED } from '../../../src/tags/reducers/tagDelete'; +import { TAG_EDITED } from '../../../src/tags/reducers/tagEdit'; + +describe('tagsListReducer', () => { + describe('reducer', () => { + it('returns loading on LIST_TAGS_START', () => { + expect(reducer({}, { type: LIST_TAGS_START })).toEqual({ + loading: true, + error: false, + }); + }); + + it('returns error on LIST_TAGS_ERROR', () => { + expect(reducer({}, { type: LIST_TAGS_ERROR })).toEqual({ + loading: false, + error: true, + }); + }); + + it('returns provided tags as filtered and regular tags on LIST_TAGS', () => { + const tags = [ 'foo', 'bar', 'baz' ]; + + expect(reducer({}, { type: LIST_TAGS, tags })).toEqual({ + tags, + filteredTags: tags, + loading: false, + error: false, + }); + }); + + it('removes provided tag from filtered and regular tags on TAG_DELETED', () => { + const tags = [ 'foo', 'bar', 'baz' ]; + const tag = 'foo'; + const expectedTags = [ 'bar', 'baz' ]; + + expect(reducer({ tags, filteredTags: tags }, { type: TAG_DELETED, tag })).toEqual({ + tags: expectedTags, + filteredTags: expectedTags, + }); + }); + + it('renames provided tag from filtered and regular tags on TAG_EDITED', () => { + const tags = [ 'foo', 'bar', 'baz' ]; + const oldName = 'bar'; + const newName = 'renamed'; + const expectedTags = [ 'foo', 'renamed', 'baz' ].sort(); + + expect(reducer({ tags, filteredTags: tags }, { type: TAG_EDITED, oldName, newName })).toEqual({ + tags: expectedTags, + filteredTags: expectedTags, + }); + }); + + it('filters original list of tags by provided search term on FILTER_TAGS', () => { + const tags = [ 'foo', 'bar', 'baz', 'foo2', 'fo' ]; + const searchTerm = 'fo'; + const filteredTags = [ 'foo', 'foo2', 'fo' ]; + + expect(reducer({ tags }, { type: FILTER_TAGS, searchTerm })).toEqual({ + tags, + filteredTags, + }); + }); + }); + + describe('filterTags', () => + it('creates expected action', () => expect(filterTags('foo')).toEqual({ type: FILTER_TAGS, searchTerm: 'foo' }))); + + describe('listTags', () => { + const dispatch = jest.fn(); + const getState = jest.fn(() => ({})); + const buildShlinkApiClient = jest.fn(); + const listTagsMock = jest.fn(); + + afterEach(() => { + dispatch.mockReset(); + getState.mockClear(); + buildShlinkApiClient.mockReset(); + listTagsMock.mockReset(); + }); + + const assertNoAction = async (tagsList) => { + getState.mockReturnValue({ tagsList }); + + await listTags(buildShlinkApiClient, false)()(dispatch, getState); + + expect(buildShlinkApiClient).not.toHaveBeenCalled(); + expect(dispatch).not.toHaveBeenCalled(); + expect(getState).toHaveBeenCalledTimes(1); + }; + + it('does nothing when loading', async () => await assertNoAction({ loading: true })); + it('does nothing when list is not empty', async () => await assertNoAction({ loading: false, tags: [ 'foo', 'bar' ] })); + + it('dispatches loaded lists when no error occurs', async () => { + const tags = [ 'foo', 'bar', 'baz' ]; + + listTagsMock.mockResolvedValue(tags); + buildShlinkApiClient.mockResolvedValue({ listTags: listTagsMock }); + + await listTags(buildShlinkApiClient, true)()(dispatch, getState); + + expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); + expect(getState).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenCalledTimes(2); + expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_TAGS_START }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_TAGS, tags }); + }); + + const assertErrorResult = async () => { + await listTags(buildShlinkApiClient, true)()(dispatch, getState); + + expect(buildShlinkApiClient).toHaveBeenCalledTimes(1); + expect(getState).toHaveBeenCalledTimes(1); + expect(dispatch).toHaveBeenCalledTimes(2); + expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_TAGS_START }); + expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_TAGS_ERROR }); + }; + + it('dispatches error when error occurs on list call', async () => { + listTagsMock.mockRejectedValue(new Error()); + buildShlinkApiClient.mockResolvedValue({ listTags: listTagsMock }); + + await assertErrorResult(); + + expect(listTagsMock).toHaveBeenCalledTimes(1); + }); + + it('dispatches error when error occurs on build call', async () => { + buildShlinkApiClient.mockRejectedValue(new Error()); + + await assertErrorResult(); + + expect(listTagsMock).not.toHaveBeenCalled(); + }); + }); +});