From 6696fb13d61c10959cdb761c891c20733228e7ab Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 25 Aug 2020 20:23:12 +0200 Subject: [PATCH] Created redux test --- src/utils/helpers/redux.ts | 10 +++--- test/utils/helpers/redux.test.ts | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 test/utils/helpers/redux.test.ts diff --git a/src/utils/helpers/redux.ts b/src/utils/helpers/redux.ts index 2eec915e..d2607707 100644 --- a/src/utils/helpers/redux.ts +++ b/src/utils/helpers/redux.ts @@ -1,17 +1,17 @@ import { Action } from 'redux'; -type ActionDispatcher = (currentState: State, action: AT) => State; -type ActionDispatcherMap = Record>; +type ActionHandler = (currentState: State, action: AT) => State; +type ActionHandlerMap = Record>; -export const buildReducer = (map: ActionDispatcherMap, initialState: State) => ( +export const buildReducer = (map: ActionHandlerMap, initialState: State) => ( state: State | undefined, action: AT, ): State => { const { type } = action; - const actionDispatcher = map[type]; + const actionHandler = map[type]; const currentState = state ?? initialState; - return actionDispatcher ? actionDispatcher(currentState, action) : currentState; + return actionHandler ? actionHandler(currentState, action) : currentState; }; export const buildActionCreator = (type: T) => (): Action => ({ type }); diff --git a/test/utils/helpers/redux.test.ts b/test/utils/helpers/redux.test.ts new file mode 100644 index 00000000..e34e9a3b --- /dev/null +++ b/test/utils/helpers/redux.test.ts @@ -0,0 +1,61 @@ +import { Action } from 'redux'; +import { buildActionCreator, buildReducer } from '../../../src/utils/helpers/redux'; + +describe('redux', () => { + beforeEach(jest.clearAllMocks); + + describe('buildActionCreator', () => { + it.each([ + [ 'foo', { type: 'foo' }], + [ 'bar', { type: 'bar' }], + [ 'something', { type: 'something' }], + ])('returns an action creator', (type, expected) => { + const actionCreator = buildActionCreator(type); + + expect(actionCreator).toBeInstanceOf(Function); + expect(actionCreator()).toEqual(expected); + }); + }); + + describe('buildReducer', () => { + const fooActionHandler = jest.fn(() => 'foo result'); + const barActionHandler = jest.fn(() => 'bar result'); + const initialState = 'initial state'; + let reducer: Function; + + beforeEach(() => { + reducer = buildReducer({ + foo: fooActionHandler, + bar: barActionHandler, + }, initialState); + }); + + it('returns a reducer which returns initial state when provided with unknown action', () => { + expect(reducer(undefined, { type: 'unknown action' })).toEqual(initialState); + expect(fooActionHandler).not.toHaveBeenCalled(); + expect(barActionHandler).not.toHaveBeenCalled(); + }); + + it.each([ + [ 'foo', 'foo result', fooActionHandler, barActionHandler ], + [ 'bar', 'bar result', barActionHandler, fooActionHandler ], + ])( + 'returns a reducer which calls corresponding action handler', + (type, expected, invokedActionHandler, notInvokedActionHandler) => { + expect(reducer(undefined, { type })).toEqual(expected); + expect(invokedActionHandler).toHaveBeenCalled(); + expect(notInvokedActionHandler).not.toHaveBeenCalled(); + }, + ); + + it.each([ + [ undefined, initialState ], + [ 'foo', 'foo' ], + [ 'something', 'something' ], + ])('returns a reducer which calls action handler with provided state or initial', (state, expected) => { + reducer(state, { type: 'foo' }); + + expect(fooActionHandler).toHaveBeenCalledWith(expected, expect.anything()); + }); + }); +});