1- import { PROXY_TREE } from 'proxy-state-tree'
2-
31import { IAction , createOvermind , createOvermindMock } from './'
4- import { Statemachine , statemachine } from './statemachine'
2+ import { statemachine } from './statemachine'
53
64describe ( 'Statemachine' , ( ) => {
75
86 test ( 'should set initial state' , ( ) => {
97
10- type States = {
8+ type States = {
119 current : 'FOO'
1210 } | {
1311 current : 'BAR'
@@ -31,24 +29,24 @@ describe('Statemachine', () => {
3129 } )
3230
3331
34-
3532 test ( 'should transition state' , ( ) => {
3633 type States = {
3734 current : 'FOO'
3835 } | {
3936 current : 'BAR'
4037 }
4138
42- const state = statemachine < States > ( {
43- FOO : [ 'BAR' ] ,
44- BAR : [ 'FOO' ]
39+ type Events = {
40+ type : 'TOGGLE' ,
41+ }
42+
43+ const state = statemachine < States , Events > ( {
44+ TOGGLE : ( state ) => state . current === 'FOO' ? 'BAR' : 'FOO'
4545 } ) . create ( {
4646 current : 'FOO'
4747 } )
4848 const transition : Action = ( { state } ) => {
49- state . transition ( {
50- current : 'BAR'
51- } )
49+ state . transition ( 'TOGGLE' )
5250 }
5351
5452 const config = {
@@ -67,166 +65,26 @@ describe('Statemachine', () => {
6765 expect ( overmind . state . current ) . toBe ( 'BAR' )
6866 } )
6967
70- test ( 'should ignore transition to invalid state' , ( ) => {
71-
72- type States = {
73- current : 'FOO'
74- } | {
75- current : 'BAR'
76- }
77-
78- const state = statemachine < States > ( {
79- FOO : [ ] ,
80- BAR : [ 'FOO' ]
81- } ) . create ( {
82- current : 'FOO'
83- } )
84- const transition : Action = ( { state } ) => {
85- state . transition ( {
86- current : 'BAR'
87- } )
88- }
89-
90- const config = {
91- state,
92- actions : {
93- transition
94- }
95- }
96-
97- interface Action extends IAction < typeof config , void , void > { }
98-
99- const overmind = createOvermindMock ( config )
100- overmind . actions . transition ( )
101- expect ( overmind . state . current ) . toBe ( 'FOO' )
102- } )
10368
104- test ( 'should allow dynamic transitions to prevent transition' , ( ) => {
105- type States = {
106- current : 'FOO'
107- } | {
108- current : 'BAR'
109- }
69+ test ( 'should ignore transition when no state returned' , ( ) => {
11070
111- const state = statemachine < States > ( {
112- FOO : [ 'BAR' ] ,
113- BAR : ( ) => false
114- } ) . create ( {
115- current : 'FOO'
116- } )
117- const transition : Action = ( { state } ) => {
118- state . transition ( {
119- current : 'BAR'
120- } )
121- }
122-
123- const config = {
124- state,
125- actions : {
126- transition
127- }
128- }
129-
130- interface Action extends IAction < typeof config , void , void > { }
131-
132- const overmind = createOvermindMock ( config )
133-
134-
135- overmind . actions . transition ( )
136- expect ( overmind . state . current ) . toBe ( 'FOO' )
137- } )
138-
139- test ( 'should allow dynamic transitions' , ( ) => {
14071 type States = {
141- current : 'FOO'
142- } | {
143- current : 'BAR'
144- }
145-
146- const state = statemachine < States > ( {
147- FOO : [ 'BAR' ] ,
148- BAR : ( ) => [ 'FOO' ]
149- } ) . create ( {
15072 current : 'FOO'
151- } )
152- const transition : Action = ( { state } ) => {
153- state . transition ( {
154- current : 'BAR'
155- } )
156- expect ( overmind . state . current ) . toBe ( 'BAR' )
157- state . transition ( {
158- current : 'FOO'
159- } )
160- }
161-
162- const config = {
163- state,
164- actions : {
165- transition
166- }
167- }
168-
169- interface Action extends IAction < typeof config , void , void > { }
170-
171- const overmind = createOvermindMock ( config )
172-
173-
174- overmind . actions . transition ( )
175- expect ( overmind . state . current ) . toBe ( 'FOO' )
176- } )
177-
178- test ( 'should allow transition from dynamic transition' , ( ) => {
179- type States = {
180- current : 'FOO'
18173 } | {
18274 current : 'BAR'
183- }
184-
185- const state = statemachine < States > ( {
186- FOO : [ 'BAR' ] ,
187- BAR : ( ) => ( { current : 'FOO' } )
188- } ) . create ( {
189- current : 'FOO'
190- } )
191- const transition : Action = ( { state } ) => {
192- state . transition ( {
193- current : 'BAR'
194- } )
19575 }
19676
197- const config = {
198- state,
199- actions : {
200- transition
201- }
202- }
203-
204- interface Action extends IAction < typeof config , void , void > { }
205-
206- const overmind = createOvermindMock ( config )
207-
208-
209- overmind . actions . transition ( )
210- expect ( overmind . state . current ) . toBe ( 'FOO' )
211- } )
212-
213- test ( 'should allow back transition' , ( ) => {
214- type States = {
215- current : 'FOO'
216- } | {
217- current : 'BAR'
218- }
77+ type Events = {
78+ type : 'TOGGLE' ,
79+ }
21980
220- const state = statemachine < States > ( {
221- FOO : [ 'BAR' ] ,
222- BAR : ( ) => true
81+ const state = statemachine < States , Events > ( {
82+ TOGGLE : ( ) => { }
22383 } ) . create ( {
22484 current : 'FOO'
22585 } )
22686 const transition : Action = ( { state } ) => {
227- state . transition ( {
228- current : 'BAR'
229- } )
87+ state . transition ( 'TOGGLE' )
23088 }
23189
23290 const config = {
@@ -239,48 +97,11 @@ describe('Statemachine', () => {
23997 interface Action extends IAction < typeof config , void , void > { }
24098
24199 const overmind = createOvermindMock ( config )
242-
243-
244100 overmind . actions . transition ( )
245101 expect ( overmind . state . current ) . toBe ( 'FOO' )
246102 } )
247103
248- test ( 'should allow using callback for transition' , ( ) => {
249- type States = {
250- current : 'FOO'
251- } | {
252- current : 'BAR'
253- bar : string
254- }
255-
256- const state = statemachine < States > ( {
257- FOO : [ 'BAR' ] ,
258- BAR : [ ]
259- } ) . create ( {
260- current : 'FOO'
261- } )
262- const transition : Action = ( { state } ) => {
263- state . transition ( 'BAR' , ( state ) => {
264- state . bar = 'bar2'
265- } )
266- }
267-
268- const config = {
269- state,
270- actions : {
271- transition
272- }
273- }
274-
275- interface Action extends IAction < typeof config , void , void > { }
276-
277- const overmind = createOvermindMock ( config )
278-
279104
280- overmind . actions . transition ( )
281- expect ( overmind . state . current ) . toBe ( 'BAR' )
282- expect ( overmind . state . matches ( 'BAR' ) ?. bar ) . toBe ( 'bar2' )
283- } )
284105
285106 test ( 'should flush changes to transitions' , ( ) => {
286107 expect . assertions ( 1 )
@@ -291,17 +112,18 @@ describe('Statemachine', () => {
291112 current : 'BAR'
292113 }
293114
294- const state = statemachine < States > ( {
295- FOO : [ 'BAR' ] ,
296- BAR : [ 'FOO' ]
115+ type Events = {
116+ type : 'TOGGLE' ,
117+ }
118+
119+ const state = statemachine < States , Events > ( {
120+ TOGGLE : ( ) => 'BAR'
297121 } ) . create ( {
298122 current : 'FOO'
299123 } )
300124
301125 const transition : Action = ( { state } ) => {
302- state . transition ( {
303- current : 'BAR'
304- } )
126+ state . transition ( 'TOGGLE' )
305127 }
306128
307129 const config = {
0 commit comments