1
- import expect from 'expect'
1
+ /* eslint-disable no-console */
2
2
import { combineReducers } from '../src'
3
3
import createStore , { ActionTypes } from '../src/createStore'
4
4
@@ -32,21 +32,24 @@ describe('Utils', () => {
32
32
} )
33
33
34
34
it ( 'warns if a reducer prop is undefined' , ( ) => {
35
- const spy = expect . spyOn ( console , 'error' )
35
+ const preSpy = console . error
36
+ const spy = jest . fn ( )
37
+ console . error = spy
36
38
37
39
let isNotDefined
38
40
combineReducers ( { isNotDefined } )
39
- expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
41
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toMatch (
40
42
/ N o r e d u c e r p r o v i d e d f o r k e y " i s N o t D e f i n e d " /
41
43
)
42
44
43
- spy . reset ( )
45
+ spy . mockClear ( )
44
46
combineReducers ( { thing : undefined } )
45
- expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
47
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toMatch (
46
48
/ N o r e d u c e r p r o v i d e d f o r k e y " t h i n g " /
47
49
)
48
50
49
- spy . restore ( )
51
+ spy . mockClear ( )
52
+ console . error = preSpy
50
53
} )
51
54
52
55
it ( 'throws an error if a reducer returns undefined handling an action' , ( ) => {
@@ -166,7 +169,7 @@ describe('Utils', () => {
166
169
} )
167
170
168
171
const initialState = reducer ( undefined , '@@INIT' )
169
- expect ( reducer ( initialState , { type : 'increment' } ) ) . toNotBe ( initialState )
172
+ expect ( reducer ( initialState , { type : 'increment' } ) ) . not . toBe ( initialState )
170
173
} )
171
174
172
175
it ( 'throws an error on first call if a reducer attempts to handle a private action' , ( ) => {
@@ -191,17 +194,24 @@ describe('Utils', () => {
191
194
} )
192
195
193
196
it ( 'warns if no reducers are passed to combineReducers' , ( ) => {
194
- const spy = expect . spyOn ( console , 'error' )
197
+ const preSpy = console . error
198
+ const spy = jest . fn ( )
199
+ console . error = spy
200
+
195
201
const reducer = combineReducers ( { } )
196
202
reducer ( { } )
197
- expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
203
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toMatch (
198
204
/ S t o r e d o e s n o t h a v e a v a l i d r e d u c e r /
199
205
)
200
- spy . restore ( )
206
+ spy . mockClear ( )
207
+ console . error = preSpy
201
208
} )
202
209
203
210
it ( 'warns if input state does not match reducer shape' , ( ) => {
204
- const spy = expect . spyOn ( console , 'error' )
211
+ const preSpy = console . error
212
+ const spy = jest . fn ( )
213
+ console . error = spy
214
+
205
215
const reducer = combineReducers ( {
206
216
foo ( state = { bar : 1 } ) {
207
217
return state
@@ -212,69 +222,75 @@ describe('Utils', () => {
212
222
} )
213
223
214
224
reducer ( )
215
- expect ( spy . calls . length ) . toBe ( 0 )
225
+ expect ( spy . mock . calls . length ) . toBe ( 0 )
216
226
217
227
reducer ( { foo : { bar : 2 } } )
218
- expect ( spy . calls . length ) . toBe ( 0 )
228
+ expect ( spy . mock . calls . length ) . toBe ( 0 )
219
229
220
230
reducer ( {
221
231
foo : { bar : 2 } ,
222
232
baz : { qux : 4 }
223
233
} )
224
- expect ( spy . calls . length ) . toBe ( 0 )
234
+ expect ( spy . mock . calls . length ) . toBe ( 0 )
225
235
226
236
createStore ( reducer , { bar : 2 } )
227
- expect ( spy . calls [ 0 ] . arguments [ 0 ] ) . toMatch (
237
+ expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toMatch (
228
238
/ U n e x p e c t e d k e y " b a r " .* c r e a t e S t o r e .* i n s t e a d : " f o o " , " b a z " /
229
239
)
230
240
231
241
createStore ( reducer , { bar : 2 , qux : 4 , thud : 5 } )
232
- expect ( spy . calls [ 1 ] . arguments [ 0 ] ) . toMatch (
242
+ expect ( spy . mock . calls [ 1 ] [ 0 ] ) . toMatch (
233
243
/ U n e x p e c t e d k e y s " q u x " , " t h u d " .* c r e a t e S t o r e .* i n s t e a d : " f o o " , " b a z " /
234
244
)
235
245
236
246
createStore ( reducer , 1 )
237
- expect ( spy . calls [ 2 ] . arguments [ 0 ] ) . toMatch (
247
+ expect ( spy . mock . calls [ 2 ] [ 0 ] ) . toMatch (
238
248
/ c r e a t e S t o r e h a s u n e x p e c t e d t y p e o f " N u m b e r " .* k e y s : " f o o " , " b a z " /
239
249
)
240
250
241
251
reducer ( { corge : 2 } )
242
- expect ( spy . calls [ 3 ] . arguments [ 0 ] ) . toMatch (
252
+ expect ( spy . mock . calls [ 3 ] [ 0 ] ) . toMatch (
243
253
/ U n e x p e c t e d k e y " c o r g e " .* r e d u c e r .* i n s t e a d : " f o o " , " b a z " /
244
254
)
245
255
246
256
reducer ( { fred : 2 , grault : 4 } )
247
- expect ( spy . calls [ 4 ] . arguments [ 0 ] ) . toMatch (
257
+ expect ( spy . mock . calls [ 4 ] [ 0 ] ) . toMatch (
248
258
/ U n e x p e c t e d k e y s " f r e d " , " g r a u l t " .* r e d u c e r .* i n s t e a d : " f o o " , " b a z " /
249
259
)
250
260
251
261
reducer ( 1 )
252
- expect ( spy . calls [ 5 ] . arguments [ 0 ] ) . toMatch (
262
+ expect ( spy . mock . calls [ 5 ] [ 0 ] ) . toMatch (
253
263
/ r e d u c e r h a s u n e x p e c t e d t y p e o f " N u m b e r " .* k e y s : " f o o " , " b a z " /
254
264
)
255
265
256
- spy . restore ( )
266
+ spy . mockClear ( )
267
+ console . error = preSpy
257
268
} )
258
269
259
270
it ( 'only warns for unexpected keys once' , ( ) => {
260
- const spy = expect . spyOn ( console , 'error' )
271
+ const preSpy = console . error
272
+ const spy = jest . fn ( )
273
+ console . error = spy
274
+
261
275
const foo = ( state = { foo : 1 } ) => state
262
276
const bar = ( state = { bar : 2 } ) => state
263
277
264
- expect ( spy . calls . length ) . toBe ( 0 )
278
+ expect ( spy . mock . calls . length ) . toBe ( 0 )
265
279
const reducer = combineReducers ( { foo, bar } )
266
280
const state = { foo : 1 , bar : 2 , qux : 3 }
267
281
reducer ( state , { } )
268
282
reducer ( state , { } )
269
283
reducer ( state , { } )
270
284
reducer ( state , { } )
271
- expect ( spy . calls . length ) . toBe ( 1 )
285
+ expect ( spy . mock . calls . length ) . toBe ( 1 )
272
286
reducer ( { ...state , baz : 5 } , { } )
273
287
reducer ( { ...state , baz : 5 } , { } )
274
288
reducer ( { ...state , baz : 5 } , { } )
275
289
reducer ( { ...state , baz : 5 } , { } )
276
- expect ( spy . calls . length ) . toBe ( 2 )
277
- spy . restore ( )
290
+ expect ( spy . mock . calls . length ) . toBe ( 2 )
291
+
292
+ spy . mockClear ( )
293
+ console . error = preSpy
278
294
} )
279
295
} )
280
296
} )
0 commit comments