@@ -179,6 +179,25 @@ describe('Reactor', () => {
179
179
180
180
expect ( mockFn . calls . count ( ) ) . toEqual ( 0 )
181
181
} )
182
+
183
+ it ( 'should raise an error if already dispatching another action' , ( ) => {
184
+ reactor . observe ( [ ] , state => reactor . dispatch ( 'noop' , { } ) )
185
+
186
+ expect ( ( ) => checkoutActions . setTaxPercent ( 5 ) ) . toThrow (
187
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
188
+ } )
189
+
190
+ it ( 'should keep working after it raised for dispatching while dispatching' , ( ) => {
191
+ var unWatchFn = reactor . observe ( [ ] , state => reactor . dispatch ( 'noop' , { } ) )
192
+
193
+ expect ( ( ) => checkoutActions . setTaxPercent ( 5 ) ) . toThrow (
194
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
195
+
196
+ unWatchFn ( )
197
+
198
+ expect ( ( ) => checkoutActions . setTaxPercent ( 5 ) ) . not . toThrow (
199
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
200
+ } )
182
201
} ) // when dispatching a relevant action
183
202
184
203
describe ( '#observe' , ( ) => {
@@ -1013,5 +1032,39 @@ describe('Reactor', () => {
1013
1032
expect ( observeSpy . calls . count ( ) ) . toBe ( 1 )
1014
1033
expect ( firstCallArg ) . toEqual ( [ 'one' , 'two' , 'three' ] )
1015
1034
} )
1035
+
1036
+ it ( 'should not allow dispatch to be called from an observer' , ( ) => {
1037
+ reactor . observe ( [ ] , state => reactor . dispatch ( 'noop' , { } ) )
1038
+
1039
+ expect ( ( ) => {
1040
+ reactor . batch ( ( ) => {
1041
+ reactor . dispatch ( 'add' , 'one' )
1042
+ reactor . dispatch ( 'add' , 'two' )
1043
+ } )
1044
+ } ) . toThrow (
1045
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
1046
+ } )
1047
+
1048
+ it ( 'should keep working after it raised for dispatching while dispatching' , ( ) => {
1049
+ var unWatchFn = reactor . observe ( [ ] , state => reactor . dispatch ( 'noop' , { } ) )
1050
+
1051
+ expect ( ( ) => {
1052
+ reactor . batch ( ( ) => {
1053
+ reactor . dispatch ( 'add' , 'one' )
1054
+ reactor . dispatch ( 'add' , 'two' )
1055
+ } )
1056
+ } ) . toThrow (
1057
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
1058
+
1059
+ unWatchFn ( )
1060
+
1061
+ expect ( ( ) => {
1062
+ reactor . batch ( ( ) => {
1063
+ reactor . dispatch ( 'add' , 'one' )
1064
+ reactor . dispatch ( 'add' , 'two' )
1065
+ } )
1066
+ } ) . not . toThrow (
1067
+ new Error ( 'Dispatch may not be called while a dispatch is in progress' ) )
1068
+ } )
1016
1069
} )
1017
1070
} )
0 commit comments