@@ -104,12 +104,11 @@ describe('Statemachine', () => {
104104 } )
105105
106106 const transition : Action = ( { state } ) => {
107- return state . transition ( 'BAR' , ( ) => {
107+ if ( state . transition ( 'BAR' ) ) {
108108 expect ( state . state ) . toBe ( 'BAR' )
109109 state . transition ( 'FOO' )
110- } , ( ) => {
111- expect ( state . state ) . toBe ( 'BAR' )
112- } )
110+ expect ( state . state ) . toBe ( 'FOO' )
111+ }
113112 }
114113
115114 const config = {
@@ -176,11 +175,11 @@ describe('Statemachine', () => {
176175 } , {
177176 state : 'FOO'
178177 } )
179- const transition : Action = ( { state } ) => {
180- return state . transition ( 'BAR' , async ( ) => {
178+ const transition : Action = async ( { state } ) => {
179+ if ( state . transition ( 'BAR' ) ) {
181180 await Promise . resolve ( )
182181 expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
183- } )
182+ }
184183 }
185184
186185 const config = {
@@ -196,9 +195,8 @@ describe('Statemachine', () => {
196195
197196 return overmind . actions . transition ( )
198197 } )
199-
200- test ( 'should reset a statemachine' , ( ) => {
201- expect . assertions ( 1 )
198+ test ( 'should enable mutations after new async matching or transition' , async ( ) => {
199+ expect . assertions ( 3 )
202200
203201 type States = {
204202 state : 'FOO'
@@ -207,58 +205,23 @@ describe('Statemachine', () => {
207205 }
208206
209207 const state = statemachine < States > ( {
210- FOO : [ 'BAR' ] ,
211- BAR : [ 'FOO' ]
208+ FOO : [ 'BAR' ] ,
209+ BAR : [ 'FOO' ]
212210 } , {
213211 state : 'FOO'
214212 } )
215-
216- const transition : Action = ( { state } ) => {
217- state . transition ( 'BAR' )
218- }
219-
220- const config = {
221- state,
222- actions : {
223- transition
213+ const transition : Action = async ( { state } ) => {
214+ if ( state . transition ( 'BAR' ) ) {
215+ await Promise . resolve ( )
216+ expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
217+ if ( state . matches ( 'BAR' ) ) {
218+ expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( false )
219+ await Promise . resolve ( )
220+ if ( state . transition ( 'FOO' ) ) {
221+ expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( false )
222+ }
223+ }
224224 }
225- }
226-
227- interface Action extends IAction < typeof config , void , void > { }
228-
229- const overmind = createOvermind ( config )
230- overmind . actions . transition ( )
231- overmind . state . reset ( )
232- expect ( overmind . state . state ) . toBe ( 'FOO' )
233- } )
234-
235- test ( 'should pass statemachine to transition callback, correctly typed' , ( ) => {
236- expect . assertions ( 3 )
237-
238- type States = {
239- state : 'FOO'
240- foo : string
241- } | {
242- state : 'BAR'
243- bar : string
244- }
245-
246- const state = statemachine < States > ( {
247- FOO : [ 'BAR' ] ,
248- BAR : [ 'FOO' ]
249- } , {
250- state : 'FOO' ,
251- foo : 'bar'
252- } )
253-
254- const transition : Action = ( { state } ) => {
255- return state . transition ( 'BAR' , ( ) => {
256- state . transition ( 'FOO' , ( current ) => {
257- current . foo = 'bar2'
258- } )
259- } , ( current ) => {
260- current . bar = 'baz2'
261- } )
262225 }
263226
264227 const config = {
@@ -268,62 +231,10 @@ describe('Statemachine', () => {
268231 }
269232 }
270233
271- interface Action extends IAction < typeof config , void , void | Promise < void > > { }
234+ interface Action extends IAction < typeof config , void , Promise < void > > { }
272235
273236 const overmind = createOvermind ( config )
274- overmind . actions . transition ( )
275- expect ( overmind . state . state ) . toBe ( 'FOO' )
276- // @ts -ignore
277- expect ( overmind . state . foo ) . toBe ( 'bar2' )
278- // @ts -ignore
279- expect ( overmind . state . bar ) . toBe ( 'baz2' )
280- } )
281237
282- test ( 'should allow async exit transition' , async ( ) => {
283- expect . assertions ( 3 )
284-
285- type States = {
286- state : 'FOO'
287- foo : string
288- } | {
289- state : 'BAR'
290- bar : string
291- }
292-
293- const state = statemachine < States > ( {
294- FOO : [ 'BAR' ] ,
295- BAR : [ 'FOO' ]
296- } , {
297- state : 'FOO' ,
298- foo : 'bar'
299- } )
300-
301- const transition : Action = ( { state } ) => {
302- return state . transition ( 'BAR' , async ( ) => {
303- await Promise . resolve ( )
304- state . transition ( 'FOO' , ( current ) => {
305- current . foo = 'bar2'
306- } )
307- } , ( current ) => {
308- current . bar = 'baz2'
309- } )
310- }
311-
312- const config = {
313- state,
314- actions : {
315- transition
316- }
317- }
318-
319- interface Action extends IAction < typeof config , void , void | Promise < void > > { }
320-
321- const overmind = createOvermind ( config )
322- await overmind . actions . transition ( )
323- expect ( overmind . state . state ) . toBe ( 'FOO' )
324- // @ts -ignore
325- expect ( overmind . state . foo ) . toBe ( 'bar2' )
326- // @ts -ignore
327- expect ( overmind . state . bar ) . toBe ( 'baz2' )
238+ return overmind . actions . transition ( )
328239 } )
329240} )
0 commit comments