@@ -5,11 +5,21 @@ import { handleAction, createAction, createActions, combineActions } from '../';
5
5
describe ( 'handleAction()' , ( ) => {
6
6
const type = 'TYPE' ;
7
7
const prevState = { counter : 3 } ;
8
+ const defaultState = { counter : 0 } ;
8
9
9
10
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
+
10
20
describe ( 'resulting reducer' , ( ) => {
11
21
it ( 'returns previous state if type does not match' , ( ) => {
12
- const reducer = handleAction ( 'NOTTYPE' , ( ) => null ) ;
22
+ const reducer = handleAction ( 'NOTTYPE' , ( ) => null , defaultState ) ;
13
23
expect ( reducer ( prevState , { type } ) ) . to . equal ( prevState ) ;
14
24
} ) ;
15
25
@@ -24,7 +34,7 @@ describe('handleAction()', () => {
24
34
it ( 'accepts single function as handler' , ( ) => {
25
35
const reducer = handleAction ( type , ( state , action ) => ( {
26
36
counter : state . counter + action . payload
27
- } ) ) ;
37
+ } ) , defaultState ) ;
28
38
expect ( reducer ( prevState , { type, payload : 7 } ) )
29
39
. to . deep . equal ( {
30
40
counter : 10
@@ -35,15 +45,15 @@ describe('handleAction()', () => {
35
45
const incrementAction = createAction ( type ) ;
36
46
const reducer = handleAction ( incrementAction , ( state , action ) => ( {
37
47
counter : state . counter + action . payload
38
- } ) ) ;
48
+ } ) , defaultState ) ;
39
49
40
50
expect ( reducer ( prevState , incrementAction ( 7 ) ) )
41
51
. to . deep . equal ( {
42
52
counter : 10
43
53
} ) ;
44
54
} ) ;
45
55
46
- it ( 'accepts single function as handler and a default state' , ( ) => {
56
+ it ( 'accepts a default state used when the previous state is undefined ' , ( ) => {
47
57
const reducer = handleAction ( type , ( state , action ) => ( {
48
58
counter : state . counter + action . payload
49
59
} ) , { counter : 3 } ) ;
@@ -59,20 +69,30 @@ describe('handleAction()', () => {
59
69
60
70
const reducer = handleAction ( increment , ( state , { payload } ) => ( {
61
71
counter : state . counter + payload
62
- } ) , { counter : 3 } ) ;
72
+ } ) , defaultState ) ;
63
73
64
74
expect ( reducer ( undefined , increment ( 7 ) ) )
65
75
. to . deep . equal ( {
66
- counter : 10
76
+ counter : 7
67
77
} ) ;
68
78
} ) ;
69
79
} ) ;
70
80
} ) ;
71
81
72
82
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
+
73
93
describe ( 'resulting reducer' , ( ) => {
74
94
it ( 'returns previous state if type does not match' , ( ) => {
75
- const reducer = handleAction ( 'NOTTYPE' , { next : ( ) => null } ) ;
95
+ const reducer = handleAction ( 'NOTTYPE' , { next : ( ) => null } , defaultState ) ;
76
96
expect ( reducer ( prevState , { type } ) ) . to . equal ( prevState ) ;
77
97
} ) ;
78
98
@@ -81,7 +101,7 @@ describe('handleAction()', () => {
81
101
next : ( state , action ) => ( {
82
102
counter : state . counter + action . payload
83
103
} )
84
- } ) ;
104
+ } , defaultState ) ;
85
105
expect ( reducer ( prevState , { type, payload : 7 } ) )
86
106
. to . deep . equal ( {
87
107
counter : 10
@@ -93,7 +113,7 @@ describe('handleAction()', () => {
93
113
throw : ( state , action ) => ( {
94
114
counter : state . counter + action . payload
95
115
} )
96
- } ) ;
116
+ } , defaultState ) ;
97
117
98
118
expect ( reducer ( prevState , { type, payload : 7 , error : true } ) )
99
119
. to . deep . equal ( {
@@ -102,7 +122,7 @@ describe('handleAction()', () => {
102
122
} ) ;
103
123
104
124
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 ) ;
106
126
expect ( reducer ( prevState , { type, payload : 123 } ) ) . to . equal ( prevState ) ;
107
127
expect ( reducer ( prevState , { type, payload : 123 , error : true } ) )
108
128
. to . equal ( prevState ) ;
@@ -115,7 +135,8 @@ describe('handleAction()', () => {
115
135
const action1 = createAction ( 'ACTION_1' ) ;
116
136
const reducer = handleAction (
117
137
combineActions ( action1 , 'ACTION_2' , 'ACTION_3' ) ,
118
- ( state , { payload } ) => ( { ...state , number : state . number + payload } )
138
+ ( state , { payload } ) => ( { ...state , number : state . number + payload } ) ,
139
+ defaultState
119
140
) ;
120
141
121
142
expect ( reducer ( { number : 1 } , action1 ( 1 ) ) ) . to . deep . equal ( { number : 2 } ) ;
@@ -129,7 +150,7 @@ describe('handleAction()', () => {
129
150
next ( state , { payload } ) {
130
151
return { ...state , number : state . number + payload } ;
131
152
}
132
- } ) ;
153
+ } , defaultState ) ;
133
154
134
155
expect ( reducer ( { number : 1 } , action1 ( 1 ) ) ) . to . deep . equal ( { number : 2 } ) ;
135
156
expect ( reducer ( { number : 1 } , { type : 'ACTION_2' , payload : 2 } ) ) . to . deep . equal ( { number : 3 } ) ;
@@ -146,7 +167,7 @@ describe('handleAction()', () => {
146
167
throw ( state ) {
147
168
return { ...state , threw : true } ;
148
169
}
149
- } ) ;
170
+ } , defaultState ) ;
150
171
const error = new Error ;
151
172
152
173
expect ( reducer ( { number : 0 } , action1 ( error ) ) )
@@ -161,6 +182,7 @@ describe('handleAction()', () => {
161
182
const reducer = handleAction (
162
183
combineActions ( 'ACTION_1' , 'ACTION_2' ) ,
163
184
( state , { payload } ) => ( { ...state , state : state . number + payload } ) ,
185
+ defaultState
164
186
) ;
165
187
166
188
const state = { number : 0 } ;
@@ -172,11 +194,11 @@ describe('handleAction()', () => {
172
194
const reducer = handleAction (
173
195
combineActions ( 'INCREMENT' , 'DECREMENT' ) ,
174
196
( state , { payload } ) => ( { ...state , counter : state . counter + payload } ) ,
175
- { counter : 10 }
197
+ defaultState
176
198
) ;
177
199
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 } ) ;
180
202
} ) ;
181
203
182
204
it ( 'should handle combined actions with symbols' , ( ) => {
@@ -185,7 +207,8 @@ describe('handleAction()', () => {
185
207
const action3 = createAction ( Symbol ( 'ACTION_3' ) ) ;
186
208
const reducer = handleAction (
187
209
combineActions ( action1 , action2 , action3 ) ,
188
- ( state , { payload } ) => ( { ...state , number : state . number + payload } )
210
+ ( state , { payload } ) => ( { ...state , number : state . number + payload } ) ,
211
+ defaultState
189
212
) ;
190
213
191
214
expect ( reducer ( { number : 0 } , action1 ( 1 ) ) )
@@ -199,7 +222,7 @@ describe('handleAction()', () => {
199
222
200
223
describe ( 'with invalid actions' , ( ) => {
201
224
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 , { } ) ;
203
226
expect (
204
227
( ) => reducer ( undefined )
205
228
) . to . throw (
@@ -209,7 +232,7 @@ describe('handleAction()', () => {
209
232
} ) ;
210
233
211
234
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 , { } ) ;
213
236
expect (
214
237
( ) => reducer ( undefined , { } )
215
238
) . to . throw (
@@ -219,7 +242,7 @@ describe('handleAction()', () => {
219
242
} ) ;
220
243
221
244
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 , { } ) ;
223
246
expect (
224
247
( ) => reducer ( undefined , { type : false } )
225
248
) . to . throw (
0 commit comments