reducer.test.js
1 import util from './reducer' 2 3 describe('reducer utility', () => { 4 const reducers = { 5 TEST_ACTION: (state, payload) => ({ 6 ...state, 7 ...payload, 8 }), 9 } 10 11 const state = { foo: 'bar' } 12 const action = { type: 'TEST_ACTION', payload: { baz: true } } 13 const missingAction = { type: 'MISSING_ACTION' } 14 const reducer = util(reducers, state) 15 16 test("it should return the existing state when the action doesn't exist", () => { 17 // Given an existing state 18 // And an action we can't reduce 19 20 // We should expect the function to pass through the existing state 21 expect(reducer(state, missingAction)).toEqual({ foo: 'bar' }) 22 }) 23 24 test('it should call the correct reducer when the action exists', () => { 25 // Given an existing state 26 // And an action we can reduce 27 28 // We expect the function to return the correctly reduced new state 29 expect(reducer(state, action)).toEqual({ 30 foo: 'bar', 31 baz: true, 32 }) 33 }) 34 35 test('it should return the default state if the existing state is null', () => { 36 // Given a null state 37 // And an action we can't reduce 38 39 // We expect the default state 40 expect(reducer(null, missingAction)).toEqual({ foo: 'bar' }) 41 }) 42 43 test('it should return the existing state if the action is null', () => { 44 // Given a null state 45 // And an action that is undefined 46 47 // We expect the default state 48 expect(reducer(state, undefined)).toEqual({ foo: 'bar' }) 49 }) 50 })