@@ -41,7 +41,7 @@ describe('Statemachine', () => {
4141 state : 'FOO'
4242 } )
4343 const transition : Action = ( { state } ) => {
44- state . BAR ( )
44+ state . transition ( 'BAR' )
4545 }
4646
4747 const config = {
@@ -72,7 +72,7 @@ describe('Statemachine', () => {
7272 state : 'FOO'
7373 } )
7474 const transition : Action = ( { state } ) => {
75- state . BAR ( )
75+ state . transition ( 'BAR' )
7676 }
7777
7878 const config = {
@@ -88,7 +88,7 @@ describe('Statemachine', () => {
8888 overmind . actions . transition ( )
8989 expect ( overmind . state . state ) . toBe ( 'FOO' )
9090 } )
91- test ( 'should run entry and exit transition' , ( ) => {
91+ test ( 'should run entry and exit transition' , async ( ) => {
9292 expect . assertions ( 3 )
9393 type States = {
9494 state : 'FOO'
@@ -104,9 +104,9 @@ describe('Statemachine', () => {
104104 } )
105105
106106 const transition : Action = ( { state } ) => {
107- state . BAR ( ( ) => {
107+ return state . transition ( 'BAR' , ( ) => {
108108 expect ( state . state ) . toBe ( 'BAR' )
109- state . FOO ( )
109+ state . transition ( 'FOO' )
110110 } , ( ) => {
111111 expect ( state . state ) . toBe ( 'BAR' )
112112 } )
@@ -119,10 +119,10 @@ describe('Statemachine', () => {
119119 }
120120 }
121121
122- interface Action extends IAction < typeof config , void , void > { }
122+ interface Action extends IAction < typeof config , void , void | Promise < void > > { }
123123
124124 const overmind = createOvermindMock ( config )
125- overmind . actions . transition ( )
125+ await overmind . actions . transition ( )
126126 expect ( overmind . state . state ) . toBe ( 'FOO' )
127127 } )
128128 test ( 'should flush changes to transitions' , ( ) => {
@@ -142,7 +142,7 @@ describe('Statemachine', () => {
142142 } )
143143
144144 const transition : Action = ( { state } ) => {
145- state . BAR ( )
145+ state . transition ( 'BAR' )
146146 }
147147
148148 const config = {
@@ -177,7 +177,7 @@ describe('Statemachine', () => {
177177 state : 'FOO'
178178 } )
179179 const transition : Action = ( { state } ) => {
180- return state . BAR ( async ( ) => {
180+ return state . transition ( 'BAR' , async ( ) => {
181181 await Promise . resolve ( )
182182 expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
183183 } )
@@ -214,7 +214,7 @@ describe('Statemachine', () => {
214214 } )
215215
216216 const transition : Action = ( { state } ) => {
217- state . BAR ( )
217+ state . transition ( 'BAR' )
218218 }
219219
220220 const config = {
@@ -252,8 +252,8 @@ describe('Statemachine', () => {
252252 } )
253253
254254 const transition : Action = ( { state } ) => {
255- state . BAR ( ( ) => {
256- state . FOO ( ( current ) => {
255+ return state . transition ( 'BAR' , ( ) => {
256+ state . transition ( 'FOO' , ( current ) => {
257257 current . foo = 'bar2'
258258 } )
259259 } , ( current ) => {
@@ -268,7 +268,7 @@ describe('Statemachine', () => {
268268 }
269269 }
270270
271- interface Action extends IAction < typeof config , void , void > { }
271+ interface Action extends IAction < typeof config , void , void | Promise < void > > { }
272272
273273 const overmind = createOvermind ( config )
274274 overmind . actions . transition ( )
@@ -278,4 +278,52 @@ describe('Statemachine', () => {
278278 // @ts -ignore
279279 expect ( overmind . state . bar ) . toBe ( 'baz2' )
280280 } )
281+
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' )
328+ } )
281329} )
0 commit comments