@@ -5,11 +5,21 @@ import { handleAction, createAction, createActions, combineActions } from '../';
55describe ( 'handleAction()' , ( ) => {
66 const type = 'TYPE' ;
77 const prevState = { counter : 3 } ;
8+ const defaultState = { counter : 0 } ;
89
910 describe ( 'single handler form' , ( ) => {
11+ it ( 'should throw an error if defaultState is not specified' , ( ) => {
12+ expect ( ( ) => {
13+ handleAction ( type , undefined ) ;
14+ } ) . to . throw (
15+ Error ,
16+ 'defaultState for reducer handling TYPE should be defined'
17+ ) ;
18+ } ) ;
19+
1020 describe ( 'resulting reducer' , ( ) => {
1121 it ( 'returns previous state if type does not match' , ( ) => {
12- const reducer = handleAction ( 'NOTTYPE' , ( ) => null ) ;
22+ const reducer = handleAction ( 'NOTTYPE' , ( ) => null , defaultState ) ;
1323 expect ( reducer ( prevState , { type } ) ) . to . equal ( prevState ) ;
1424 } ) ;
1525
@@ -24,7 +34,7 @@ describe('handleAction()', () => {
2434 it ( 'accepts single function as handler' , ( ) => {
2535 const reducer = handleAction ( type , ( state , action ) => ( {
2636 counter : state . counter + action . payload
27- } ) ) ;
37+ } ) , defaultState ) ;
2838 expect ( reducer ( prevState , { type, payload : 7 } ) )
2939 . to . deep . equal ( {
3040 counter : 10
@@ -35,15 +45,15 @@ describe('handleAction()', () => {
3545 const incrementAction = createAction ( type ) ;
3646 const reducer = handleAction ( incrementAction , ( state , action ) => ( {
3747 counter : state . counter + action . payload
38- } ) ) ;
48+ } ) , defaultState ) ;
3949
4050 expect ( reducer ( prevState , incrementAction ( 7 ) ) )
4151 . to . deep . equal ( {
4252 counter : 10
4353 } ) ;
4454 } ) ;
4555
46- it ( 'accepts single function as handler and a default state' , ( ) => {
56+ it ( 'accepts a default state used when the previous state is undefined ' , ( ) => {
4757 const reducer = handleAction ( type , ( state , action ) => ( {
4858 counter : state . counter + action . payload
4959 } ) , { counter : 3 } ) ;
@@ -59,20 +69,30 @@ describe('handleAction()', () => {
5969
6070 const reducer = handleAction ( increment , ( state , { payload } ) => ( {
6171 counter : state . counter + payload
62- } ) , { counter : 3 } ) ;
72+ } ) , defaultState ) ;
6373
6474 expect ( reducer ( undefined , increment ( 7 ) ) )
6575 . to . deep . equal ( {
66- counter : 10
76+ counter : 7
6777 } ) ;
6878 } ) ;
6979 } ) ;
7080 } ) ;
7181
7282 describe ( 'map of handlers form' , ( ) => {
83+ it ( 'should throw an error if defaultState is not specified' , ( ) => {
84+ expect ( ( ) => {
85+ handleAction ( type , { next : ( ) => null } ) ;
86+ } )
87+ . to . throw (
88+ Error ,
89+ 'defaultState for reducer handling TYPE should be defined'
90+ ) ;
91+ } ) ;
92+
7393 describe ( 'resulting reducer' , ( ) => {
7494 it ( 'returns previous state if type does not match' , ( ) => {
75- const reducer = handleAction ( 'NOTTYPE' , { next : ( ) => null } ) ;
95+ const reducer = handleAction ( 'NOTTYPE' , { next : ( ) => null } , defaultState ) ;
7696 expect ( reducer ( prevState , { type } ) ) . to . equal ( prevState ) ;
7797 } ) ;
7898
@@ -81,7 +101,7 @@ describe('handleAction()', () => {
81101 next : ( state , action ) => ( {
82102 counter : state . counter + action . payload
83103 } )
84- } ) ;
104+ } , defaultState ) ;
85105 expect ( reducer ( prevState , { type, payload : 7 } ) )
86106 . to . deep . equal ( {
87107 counter : 10
@@ -93,7 +113,7 @@ describe('handleAction()', () => {
93113 throw : ( state , action ) => ( {
94114 counter : state . counter + action . payload
95115 } )
96- } ) ;
116+ } , defaultState ) ;
97117
98118 expect ( reducer ( prevState , { type, payload : 7 , error : true } ) )
99119 . to . deep . equal ( {
@@ -102,7 +122,7 @@ describe('handleAction()', () => {
102122 } ) ;
103123
104124 it ( 'returns previous state if matching handler is not function' , ( ) => {
105- const reducer = handleAction ( type , { next : null , error : 123 } ) ;
125+ const reducer = handleAction ( type , { next : null , error : 123 } , defaultState ) ;
106126 expect ( reducer ( prevState , { type, payload : 123 } ) ) . to . equal ( prevState ) ;
107127 expect ( reducer ( prevState , { type, payload : 123 , error : true } ) )
108128 . to . equal ( prevState ) ;
@@ -115,7 +135,8 @@ describe('handleAction()', () => {
115135 const action1 = createAction ( 'ACTION_1' ) ;
116136 const reducer = handleAction (
117137 combineActions ( action1 , 'ACTION_2' , 'ACTION_3' ) ,
118- ( state , { payload } ) => ( { ...state , number : state . number + payload } )
138+ ( state , { payload } ) => ( { ...state , number : state . number + payload } ) ,
139+ defaultState
119140 ) ;
120141
121142 expect ( reducer ( { number : 1 } , action1 ( 1 ) ) ) . to . deep . equal ( { number : 2 } ) ;
@@ -129,7 +150,7 @@ describe('handleAction()', () => {
129150 next ( state , { payload } ) {
130151 return { ...state , number : state . number + payload } ;
131152 }
132- } ) ;
153+ } , defaultState ) ;
133154
134155 expect ( reducer ( { number : 1 } , action1 ( 1 ) ) ) . to . deep . equal ( { number : 2 } ) ;
135156 expect ( reducer ( { number : 1 } , { type : 'ACTION_2' , payload : 2 } ) ) . to . deep . equal ( { number : 3 } ) ;
@@ -146,7 +167,7 @@ describe('handleAction()', () => {
146167 throw ( state ) {
147168 return { ...state , threw : true } ;
148169 }
149- } ) ;
170+ } , defaultState ) ;
150171 const error = new Error ;
151172
152173 expect ( reducer ( { number : 0 } , action1 ( error ) ) )
@@ -161,6 +182,7 @@ describe('handleAction()', () => {
161182 const reducer = handleAction (
162183 combineActions ( 'ACTION_1' , 'ACTION_2' ) ,
163184 ( state , { payload } ) => ( { ...state , state : state . number + payload } ) ,
185+ defaultState
164186 ) ;
165187
166188 const state = { number : 0 } ;
@@ -172,11 +194,11 @@ describe('handleAction()', () => {
172194 const reducer = handleAction (
173195 combineActions ( 'INCREMENT' , 'DECREMENT' ) ,
174196 ( state , { payload } ) => ( { ...state , counter : state . counter + payload } ) ,
175- { counter : 10 }
197+ defaultState
176198 ) ;
177199
178- expect ( reducer ( undefined , { type : 'INCREMENT' , payload : + 1 } ) ) . to . deep . equal ( { counter : 11 } ) ;
179- expect ( reducer ( undefined , { type : 'DECREMENT' , payload : - 1 } ) ) . to . deep . equal ( { counter : 9 } ) ;
200+ expect ( reducer ( undefined , { type : 'INCREMENT' , payload : + 1 } ) ) . to . deep . equal ( { counter : + 1 } ) ;
201+ expect ( reducer ( undefined , { type : 'DECREMENT' , payload : - 1 } ) ) . to . deep . equal ( { counter : - 1 } ) ;
180202 } ) ;
181203
182204 it ( 'should handle combined actions with symbols' , ( ) => {
@@ -185,7 +207,8 @@ describe('handleAction()', () => {
185207 const action3 = createAction ( Symbol ( 'ACTION_3' ) ) ;
186208 const reducer = handleAction (
187209 combineActions ( action1 , action2 , action3 ) ,
188- ( state , { payload } ) => ( { ...state , number : state . number + payload } )
210+ ( state , { payload } ) => ( { ...state , number : state . number + payload } ) ,
211+ defaultState
189212 ) ;
190213
191214 expect ( reducer ( { number : 0 } , action1 ( 1 ) ) )
@@ -199,7 +222,7 @@ describe('handleAction()', () => {
199222
200223 describe ( 'with invalid actions' , ( ) => {
201224 it ( 'should throw a descriptive error when the action object is missing' , ( ) => {
202- const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity ) ;
225+ const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity , { } ) ;
203226 expect (
204227 ( ) => reducer ( undefined )
205228 ) . to . throw (
@@ -209,7 +232,7 @@ describe('handleAction()', () => {
209232 } ) ;
210233
211234 it ( 'should throw a descriptive error when the action type is missing' , ( ) => {
212- const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity ) ;
235+ const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity , { } ) ;
213236 expect (
214237 ( ) => reducer ( undefined , { } )
215238 ) . to . throw (
@@ -219,7 +242,7 @@ describe('handleAction()', () => {
219242 } ) ;
220243
221244 it ( 'should throw a descriptive error when the action type is not a string or symbol' , ( ) => {
222- const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity ) ;
245+ const reducer = handleAction ( createAction ( 'ACTION_1' ) , identity , { } ) ;
223246 expect (
224247 ( ) => reducer ( undefined , { type : false } )
225248 ) . to . throw (
0 commit comments