@@ -23,9 +23,11 @@ describe('Statemachine', () => {
2323
2424 const overmind = createOvermindMock ( config )
2525
26- expect ( overmind . state . current ) . toBe ( 'FOO' )
26+ expect ( overmind . state . FOO ) . toBeDefined ( )
27+ expect ( overmind . state . BAR ) . toBe ( null )
2728 } )
2829
30+
2931 test ( 'should transition state' , ( ) => {
3032 type States = {
3133 FOO : { } ,
@@ -53,7 +55,7 @@ describe('Statemachine', () => {
5355
5456
5557 overmind . actions . transition ( )
56- expect ( overmind . state . current ) . toBe ( 'BAR' )
58+ expect ( overmind . state . BAR ) . toBeDefined ( )
5759 } )
5860
5961 test ( 'should ignore transition to invalid state' , ( ) => {
@@ -82,7 +84,7 @@ describe('Statemachine', () => {
8284
8385 const overmind = createOvermindMock ( config )
8486 overmind . actions . transition ( )
85- expect ( overmind . state . current ) . toBe ( 'FOO' )
87+ expect ( overmind . state . FOO ) . toBeDefined ( )
8688 } )
8789
8890 test ( 'should run entry and exit transition' , async ( ) => {
@@ -99,9 +101,9 @@ describe('Statemachine', () => {
99101
100102 const transition : Action = async ( { state } ) => {
101103 if ( state . transition ( 'BAR' , { } ) ) {
102- expect ( state . current ) . toBe ( 'BAR' )
104+ expect ( state . BAR ) . toBeDefined ( )
103105 if ( state . transition ( 'FOO' , { } ) ) {
104- expect ( state . current ) . toBe ( 'FOO' )
106+ expect ( state . FOO ) . toBeDefined ( )
105107 }
106108 }
107109 }
@@ -118,7 +120,7 @@ describe('Statemachine', () => {
118120 const overmind = createOvermindMock ( config )
119121
120122 await overmind . actions . transition ( )
121- expect ( overmind . state . current ) . toBe ( 'FOO' )
123+ expect ( overmind . state . FOO ) . toBeDefined ( )
122124 } )
123125
124126 test ( 'should flush changes to transitions' , ( ) => {
@@ -148,90 +150,11 @@ describe('Statemachine', () => {
148150 interface Action extends IAction < typeof config , void , void > { }
149151
150152 const overmind = createOvermind ( config )
151- overmind . reaction ( ( state ) => state . current , ( value ) => {
152- expect ( value ) . toBe ( 'BAR' )
153+ overmind . reaction ( ( state ) => state . BAR , ( value ) => {
154+ expect ( value ) . toEqual ( { } )
153155 } )
154156 overmind . actions . transition ( )
155157 } )
156-
157- test ( 'should error when mutating async in transitions' , async ( ) => {
158- expect . assertions ( 1 )
159-
160- type States = {
161- FOO : { } ,
162- BAR : { }
163- }
164-
165- const state = statemachine < States > ( {
166- FOO : [ 'BAR' ] ,
167- BAR : [ 'FOO' ]
168- } ) . create ( 'FOO' , { } )
169-
170- const transition : Action = async ( { state } ) => {
171- if ( state . transition ( 'BAR' , { } ) ) {
172- await Promise . resolve ( )
173- expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
174- }
175- }
176-
177- const config = {
178- state,
179- actions : {
180- transition
181- }
182- }
183-
184- interface Action extends IAction < typeof config , void , Promise < void > > { }
185-
186- const overmind = createOvermind ( config )
187-
188- return overmind . actions . transition ( )
189- } )
190-
191- test ( 'should only enable mutations with matches' , async ( ) => {
192- expect . assertions ( 4 )
193-
194- type States = {
195- FOO : { } ,
196- BAR : { }
197- }
198-
199- const state = statemachine < States > ( {
200- FOO : [ 'BAR' ] ,
201- BAR : [ 'FOO' ]
202- } ) . create ( 'FOO' , { } )
203-
204- const transition : Action = async ( { state } ) => {
205- if ( state . transition ( 'BAR' , { } ) ) {
206- await Promise . resolve ( )
207- expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
208- if ( state . matches ( 'BAR' ) ) {
209- expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( false )
210- await Promise . resolve ( )
211- . then ( ( ) => {
212- expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( true )
213- } )
214- if ( state . transition ( 'FOO' , { } ) ) {
215- expect ( state [ PROXY_TREE ] . master . mutationTree . isBlocking ) . toBe ( false )
216- }
217- }
218- }
219- }
220-
221-
222- const config = {
223- state,
224- actions : {
225- transition
226- }
227- }
228-
229- interface Action extends IAction < typeof config , void , Promise < void > > { }
230-
231- const overmind = createOvermind ( config )
232-
233- return overmind . actions . transition ( )
234- } )
235158
236159 test ( 'should make copy of statemachine during tests' , ( ) => {
237160
@@ -270,12 +193,13 @@ describe('Statemachine', () => {
270193
271194 // @ts -ignore
272195 overmind . actions . changeFoo ( )
273- expect ( overmind . state . FOO . obj . foo ) . toBe ( 'bar2' )
196+
197+ expect ( overmind . state . FOO ?. obj . foo ) . toBe ( 'bar2' )
274198
275199
276200 const overmind2 = createOvermindMock ( config )
277201
278- expect ( overmind2 . state . FOO . obj . foo ) . toBe ( 'bar' )
202+ expect ( overmind2 . state . FOO ? .obj . foo ) . toBe ( 'bar' )
279203 } )
280204
281205 test ( 'should have base state' , ( ) => {
@@ -349,11 +273,11 @@ describe('Statemachine', () => {
349273 if ( state . transition ( 'BAR' , {
350274 bar : 123
351275 } ) ) {
352- expect ( state . BAR . bar ) . toBe ( 123 )
276+ expect ( state . BAR ? .bar ) . toBe ( 123 )
353277 await Promise . resolve ( )
354278 if ( state . transition ( 'FOO' , { foo : '123' } ) ) {
355279 expect ( state . BAR ) . toBe ( null )
356- expect ( state . FOO . foo ) . toBe ( '123' )
280+ expect ( state . FOO ? .foo ) . toBe ( '123' )
357281 await Promise . resolve ( )
358282 }
359283 }
@@ -372,68 +296,6 @@ describe('Statemachine', () => {
372296
373297 overmind . actions . transition ( )
374298 } )
375-
376- test ( 'should not allow mutations in strict mode' , ( ) => {
377- type States = {
378- FOO : { } ,
379- BAR : { }
380- }
381-
382- const state = statemachine < States > ( {
383- FOO : [ 'BAR' ] ,
384- BAR : [ 'FOO' ]
385- } ) . create ( 'FOO' , { } )
386- const transition : Action = ( { state } ) => {
387- state . current = 'BAR'
388- }
389-
390- const config = {
391- state,
392- actions : {
393- transition
394- }
395- }
396-
397- interface Action extends IAction < typeof config , void , void > { }
398-
399- const overmind = createOvermindMock ( config )
400- // @ts -ignore
401- overmind . isStrict = true
402-
403-
404- expect ( ( ) => overmind . actions . transition ( ) ) . toThrow ( )
405- } )
406-
407- test ( 'should allow mutations using transition' , ( ) => {
408- type States = {
409- FOO : { }
410- BAR : { }
411- }
412-
413- const state = statemachine < States > ( {
414- FOO : [ 'BAR' ] ,
415- BAR : [ 'FOO' ]
416- } ) . create ( 'FOO' , { } )
417- const transition : Action = ( { state } ) => {
418- state . transition ( 'BAR' , { } )
419- }
420-
421- const config = {
422- state,
423- actions : {
424- transition
425- }
426- }
427-
428- interface Action extends IAction < typeof config , void , void > { }
429-
430- const overmind = createOvermindMock ( config )
431- // @ts -ignore
432- overmind . isStrict = true
433-
434-
435- expect ( ( ) => overmind . actions . transition ( ) ) . not . toThrow ( )
436- } )
437299
438300 test ( 'should dispose nested and base machines' , async ( ) => {
439301 expect . assertions ( 3 )
@@ -470,15 +332,15 @@ describe('Statemachine', () => {
470332 } )
471333
472334 const transition : Action = ( { state } ) => {
473- if ( state . matches ( ' FOO' ) ) {
335+ if ( state . FOO ) {
474336 const ref = state . FOO
475337 if ( state . transition ( 'BAR' , { } ) ) {
476338 expect ( state . FOO ) . toBe ( null )
477339 ref . transition ( 'BAR' , { } )
478340 ref . base . transition ( 'BAR' , { } )
479341
480- expect ( ref . current ) . toBe ( 'FOO' )
481- expect ( ref . base . current ) . toBe ( 'FOO' )
342+ expect ( ref . FOO ) . toBeDefined ( )
343+ expect ( ref . base . FOO ) . toBeDefined ( )
482344 }
483345 }
484346 }
0 commit comments