@@ -21,6 +21,8 @@ import type {
21
21
TypedAddListener ,
22
22
Unsubscribe ,
23
23
} from '../index'
24
+ import { JobCancellationException } from '../job'
25
+ import { createNonNullChain } from 'typescript'
24
26
25
27
const middlewareApi = {
26
28
getState : expect . any ( Function ) ,
@@ -732,40 +734,42 @@ describe('createActionListenerMiddleware', () => {
732
734
middleware : ( gDM ) => gDM ( ) . prepend ( middleware ) ,
733
735
} )
734
736
737
+ let takeResult : any = undefined
738
+
735
739
middleware . addListener ( {
736
740
predicate : incrementByAmount . match ,
737
741
listener : async ( _ , listenerApi ) => {
738
- const result = await listenerApi . take ( increment . match , 50 )
739
-
740
- expect ( result ) . toBe ( null )
742
+ takeResult = await listenerApi . take ( increment . match , 15 )
741
743
} ,
742
744
} )
743
745
store . dispatch ( incrementByAmount ( 1 ) )
744
- await delay ( 200 )
746
+ await delay ( 25 )
747
+
748
+ expect ( takeResult ) . toBe ( null )
745
749
} )
746
750
747
- test ( "take resolves to [A, CurrentState, PreviousState] if the timeout is provided but doesn't expires" , ( done ) => {
751
+ test ( "take resolves to [A, CurrentState, PreviousState] if the timeout is provided but doesn't expires" , async ( ) => {
748
752
const store = configureStore ( {
749
753
reducer : counterSlice . reducer ,
750
754
middleware : ( gDM ) => gDM ( ) . prepend ( middleware ) ,
751
755
} )
756
+ let takeResult : any = undefined
757
+ let stateBefore : any = undefined
758
+ let stateCurrent : any = undefined
752
759
753
760
middleware . addListener ( {
754
761
predicate : incrementByAmount . match ,
755
762
listener : async ( _ , listenerApi ) => {
756
- const stateBefore = listenerApi . getState ( )
757
- const result = await listenerApi . take ( increment . match , 50 )
758
-
759
- expect ( result ) . toEqual ( [
760
- increment ( ) ,
761
- listenerApi . getState ( ) ,
762
- stateBefore ,
763
- ] )
764
- done ( )
763
+ stateBefore = listenerApi . getState ( )
764
+ takeResult = await listenerApi . take ( increment . match , 50 )
765
+ stateCurrent = listenerApi . getState ( )
765
766
} ,
766
767
} )
767
768
store . dispatch ( incrementByAmount ( 1 ) )
768
769
store . dispatch ( increment ( ) )
770
+
771
+ await delay ( 25 )
772
+ expect ( takeResult ) . toEqual ( [ increment ( ) , stateCurrent , stateBefore ] )
769
773
} )
770
774
771
775
test ( 'condition method resolves promise when the predicate succeeds' , async ( ) => {
@@ -799,11 +803,11 @@ describe('createActionListenerMiddleware', () => {
799
803
800
804
store . dispatch ( increment ( ) )
801
805
expect ( listenerStarted ) . toBe ( true )
802
- await delay ( 50 )
806
+ await delay ( 25 )
803
807
store . dispatch ( increment ( ) )
804
808
store . dispatch ( increment ( ) )
805
809
806
- await delay ( 50 )
810
+ await delay ( 25 )
807
811
808
812
expect ( finalCount ) . toBe ( 3 )
809
813
} )
@@ -828,7 +832,7 @@ describe('createActionListenerMiddleware', () => {
828
832
listenerStarted = true
829
833
const result = await listenerApi . condition ( ( action , currentState ) => {
830
834
return ( currentState as CounterState ) . value === 3
831
- } , 50 )
835
+ } , 25 )
832
836
833
837
expect ( result ) . toBe ( false )
834
838
const latestState = listenerApi . getState ( ) as CounterState
@@ -842,34 +846,49 @@ describe('createActionListenerMiddleware', () => {
842
846
843
847
store . dispatch ( increment ( ) )
844
848
845
- await delay ( 150 )
849
+ await delay ( 50 )
846
850
store . dispatch ( increment ( ) )
847
851
848
852
expect ( finalCount ) . toBe ( 2 )
849
853
} )
850
854
} )
851
855
852
856
describe ( 'Job API' , ( ) => {
853
- test ( 'Allows canceling previous jobs' , ( ) => {
857
+ test ( 'Allows canceling previous jobs' , async ( ) => {
854
858
let jobsStarted = 0
859
+ let jobsContinued = 0
860
+ let jobsCanceled = 0
855
861
856
862
middleware . addListener ( {
857
863
actionCreator : increment ,
858
864
listener : async ( action , listenerApi ) => {
859
865
jobsStarted ++
860
866
861
867
if ( jobsStarted < 3 ) {
862
- await listenerApi . condition ( decrement . match )
863
- // Cancelation _should_ cause `condition()` to throw so we never
864
- // end up hitting this next line
865
- console . log ( 'Continuing after decrement' )
868
+ try {
869
+ await listenerApi . condition ( decrement . match )
870
+ // Cancelation _should_ cause `condition()` to throw so we never
871
+ // end up hitting this next line
872
+ jobsContinued ++
873
+ } catch ( err ) {
874
+ if ( err instanceof JobCancellationException ) {
875
+ jobsCanceled ++
876
+ }
877
+ }
866
878
} else {
867
879
listenerApi . cancelPrevious ( )
868
880
}
869
881
} ,
870
882
} )
871
883
872
- // TODO Write the rest of this test
884
+ store . dispatch ( increment ( ) )
885
+ store . dispatch ( increment ( ) )
886
+ store . dispatch ( increment ( ) )
887
+
888
+ await delay ( 10 )
889
+ expect ( jobsStarted ) . toBe ( 3 )
890
+ expect ( jobsContinued ) . toBe ( 0 )
891
+ expect ( jobsCanceled ) . toBe ( 2 )
873
892
} )
874
893
} )
875
894
0 commit comments