@@ -15,7 +15,7 @@ import {
15
15
const middlewareApi = {
16
16
getState : expect . any ( Function ) ,
17
17
dispatch : expect . any ( Function ) ,
18
- stopPropagation : expect . any ( Function ) ,
18
+ currentPhase : expect . stringMatching ( / b e f o r e R e d u c e r | a f t e r R e d u c e r / ) ,
19
19
unsubscribe : expect . any ( Function ) ,
20
20
}
21
21
@@ -233,14 +233,15 @@ describe('createActionListenerMiddleware', () => {
233
233
] )
234
234
} )
235
235
236
- const whenMap : [ When , string , string ] [ ] = [
237
- [ undefined , 'reducer' , 'listener' ] ,
238
- [ 'before' , 'listener' , 'reducer' ] ,
239
- [ 'after' , 'reducer' , 'listener' ] ,
236
+ const whenMap : [ When , string , string , number ] [ ] = [
237
+ [ undefined , 'reducer' , 'listener' , 1 ] ,
238
+ [ 'beforeReducer' , 'listener' , 'reducer' , 1 ] ,
239
+ [ 'afterReducer' , 'reducer' , 'listener' , 1 ] ,
240
+ [ 'both' , 'reducer' , 'listener' , 2 ] ,
240
241
]
241
242
test . each ( whenMap ) (
242
243
'with "when" set to %s, %s runs before %s' ,
243
- ( when , _ , shouldRunLast ) => {
244
+ ( when , _ , shouldRunLast , listenerCalls ) => {
244
245
let whoRanLast = ''
245
246
246
247
reducer . mockClear ( )
@@ -255,7 +256,7 @@ describe('createActionListenerMiddleware', () => {
255
256
256
257
store . dispatch ( testAction1 ( 'a' ) )
257
258
expect ( reducer ) . toHaveBeenCalledTimes ( 1 )
258
- expect ( listener ) . toHaveBeenCalledTimes ( 1 )
259
+ expect ( listener ) . toHaveBeenCalledTimes ( listenerCalls )
259
260
expect ( whoRanLast ) . toBe ( shouldRunLast )
260
261
}
261
262
)
@@ -275,48 +276,17 @@ describe('createActionListenerMiddleware', () => {
275
276
calls . push ( after2 )
276
277
}
277
278
278
- middleware . addListener ( testAction1 , before1 , { when : 'before ' } )
279
- middleware . addListener ( testAction1 , before2 , { when : 'before ' } )
280
- middleware . addListener ( testAction1 , after1 , { when : 'after ' } )
281
- middleware . addListener ( testAction1 , after2 , { when : 'after ' } )
279
+ middleware . addListener ( testAction1 , before1 , { when : 'beforeReducer ' } )
280
+ middleware . addListener ( testAction1 , before2 , { when : 'beforeReducer ' } )
281
+ middleware . addListener ( testAction1 , after1 , { when : 'afterReducer ' } )
282
+ middleware . addListener ( testAction1 , after2 , { when : 'afterReducer ' } )
282
283
283
284
store . dispatch ( testAction1 ( 'a' ) )
284
285
store . dispatch ( testAction2 ( 'a' ) )
285
286
286
287
expect ( calls ) . toEqual ( [ before1 , before2 , after1 , after2 ] )
287
288
} )
288
289
289
- test ( 'mixing "before" and "after" with stopPropagation' , ( ) => {
290
- const calls : Function [ ] = [ ]
291
- function before1 ( ) {
292
- calls . push ( before1 )
293
- }
294
- function before2 ( _ : any , api : any ) {
295
- calls . push ( before2 )
296
- api . stopPropagation ( )
297
- }
298
- function before3 ( ) {
299
- calls . push ( before3 )
300
- }
301
- function after1 ( ) {
302
- calls . push ( after1 )
303
- }
304
- function after2 ( ) {
305
- calls . push ( after2 )
306
- }
307
-
308
- middleware . addListener ( testAction1 , before1 , { when : 'before' } )
309
- middleware . addListener ( testAction1 , before2 , { when : 'before' } )
310
- middleware . addListener ( testAction1 , before3 , { when : 'before' } )
311
- middleware . addListener ( testAction1 , after1 , { when : 'after' } )
312
- middleware . addListener ( testAction1 , after2 , { when : 'after' } )
313
-
314
- store . dispatch ( testAction1 ( 'a' ) )
315
- store . dispatch ( testAction2 ( 'a' ) )
316
-
317
- expect ( calls ) . toEqual ( [ before1 , before2 ] )
318
- } )
319
-
320
290
test ( 'by default, actions are forwarded to the store' , ( ) => {
321
291
reducer . mockClear ( )
322
292
@@ -328,73 +298,4 @@ describe('createActionListenerMiddleware', () => {
328
298
329
299
expect ( reducer . mock . calls ) . toEqual ( [ [ { } , testAction1 ( 'a' ) ] ] )
330
300
} )
331
-
332
- test ( 'calling `api.stopPropagation` in the listeners prevents actions from being forwarded to the store' , ( ) => {
333
- reducer . mockClear ( )
334
-
335
- middleware . addListener (
336
- testAction1 ,
337
- ( action : TestAction1 , api ) => {
338
- if ( action . payload === 'b' ) {
339
- api . stopPropagation ( )
340
- }
341
- } ,
342
- { when : 'before' }
343
- )
344
-
345
- store . dispatch ( testAction1 ( 'a' ) )
346
- store . dispatch ( testAction1 ( 'b' ) )
347
- store . dispatch ( testAction1 ( 'c' ) )
348
-
349
- expect ( reducer . mock . calls ) . toEqual ( [
350
- [ { } , testAction1 ( 'a' ) ] ,
351
- [ { } , testAction1 ( 'c' ) ] ,
352
- ] )
353
- } )
354
-
355
- test ( 'calling `api.stopPropagation` with `when` set to "after" causes an error to be thrown' , ( ) => {
356
- reducer . mockClear ( )
357
-
358
- middleware . addListener (
359
- testAction1 ,
360
- ( action : TestAction1 , api ) => {
361
- if ( action . payload === 'b' ) {
362
- // @ts -ignore TypeScript would already prevent this from being called with "after"
363
- api . stopPropagation ( )
364
- }
365
- } ,
366
- { when : 'after' }
367
- )
368
-
369
- store . dispatch ( testAction1 ( 'a' ) )
370
- expect ( ( ) => {
371
- store . dispatch ( testAction1 ( 'b' ) )
372
- } ) . toThrowErrorMatchingInlineSnapshot (
373
- `"stopPropagation can only be called by action listeners with the \`when\` option set to \\"before\\""`
374
- )
375
- } )
376
-
377
- test ( 'calling `api.stopPropagation` asynchronously causes an error to be thrown' , ( finish ) => {
378
- reducer . mockClear ( )
379
-
380
- middleware . addListener (
381
- testAction1 ,
382
- ( action : TestAction1 , api ) => {
383
- if ( action . payload === 'b' ) {
384
- setTimeout ( ( ) => {
385
- expect ( ( ) => {
386
- api . stopPropagation ( )
387
- } ) . toThrowErrorMatchingInlineSnapshot (
388
- `"stopPropagation can only be called synchronously"`
389
- )
390
- finish ( )
391
- } )
392
- }
393
- } ,
394
- { when : 'before' }
395
- )
396
-
397
- store . dispatch ( testAction1 ( 'a' ) )
398
- store . dispatch ( testAction1 ( 'b' ) )
399
- } )
400
301
} )
0 commit comments