@@ -376,6 +376,48 @@ describe('createActionListenerMiddleware', () => {
376
376
}
377
377
)
378
378
379
+ test ( 'Passes both getState and getOriginalState in the API' , ( ) => {
380
+ const store = configureStore ( {
381
+ reducer : counterSlice . reducer ,
382
+ middleware : ( gDM ) => gDM ( ) . prepend ( middleware ) ,
383
+ } )
384
+
385
+ let listener1Calls = 0
386
+ middleware . addListener (
387
+ increment ,
388
+ ( action , listenerApi ) => {
389
+ // TODO getState functions aren't typed right here
390
+ const stateBefore = listenerApi . getOriginalState ( ) as CounterState
391
+ const currentState = listenerApi . getOriginalState ( ) as CounterState
392
+
393
+ listener1Calls ++
394
+ // In the "before" phase, we pass the same state
395
+ expect ( currentState ) . toBe ( stateBefore )
396
+ } ,
397
+ { when : 'beforeReducer' }
398
+ )
399
+
400
+ let listener2Calls = 0
401
+ middleware . addListener (
402
+ increment ,
403
+ ( action , listenerApi ) => {
404
+ // TODO getState functions aren't typed right here
405
+ const stateBefore = listenerApi . getOriginalState ( ) as CounterState
406
+ const currentState = listenerApi . getOriginalState ( ) as CounterState
407
+
408
+ listener2Calls ++
409
+ // In the "after" phase, we pass the new state for `getState`, and still have original state too
410
+ expect ( currentState . value ) . toBe ( stateBefore . value + 1 )
411
+ } ,
412
+ { when : 'afterReducer' }
413
+ )
414
+
415
+ store . dispatch ( increment ( ) )
416
+
417
+ expect ( listener1Calls ) . toBe ( 1 )
418
+ expect ( listener2Calls ) . toBe ( 1 )
419
+ } )
420
+
379
421
test ( 'mixing "before" and "after"' , ( ) => {
380
422
const calls : Function [ ] = [ ]
381
423
function before1 ( ) {
0 commit comments