deno.land / x / xstate@xstate@4.33.6 / test / activities.test.ts

activities.test.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import { Machine } from '../src/index';import { start, stop } from '../src/actions';
const lightMachine = Machine({ key: 'light', initial: 'green', states: { green: { activities: ['fadeInGreen'], on: { TIMER: 'yellow' } }, yellow: { on: { TIMER: 'red' } }, red: { initial: 'walk', activities: ['activateCrosswalkLight'], on: { TIMER: 'green' }, states: { walk: { on: { PED_WAIT: 'wait' } }, wait: { activities: ['blinkCrosswalkLight'], on: { PED_STOP: 'stop' } }, stop: {} } } }});
describe('activities with guarded transitions', () => { const B_ACTIVITY = () => void 0; const machine = Machine( { initial: 'A', states: { A: { on: { E: 'B' } }, B: { on: { '': [{ cond: () => false, target: 'A' }] }, activities: ['B_ACTIVITY'] } } }, { activities: { B_ACTIVITY } } );
it('should activate even if there are subsequent automatic, but blocked transitions', () => { let state = machine.initialState; state = machine.transition(state, 'E'); expect(state.activities.B_ACTIVITY).toBeTruthy(); expect(state.actions).toEqual([ start({ type: 'B_ACTIVITY', id: 'B_ACTIVITY', exec: undefined }) ]); });});
describe('remembering activities', () => { const machine = Machine({ initial: 'A', states: { A: { on: { E: 'B' } }, B: { on: { E: 'A' }, activities: ['B_ACTIVITY'] } } });
it('should remember the activities even after an event', () => { let state = machine.initialState; state = machine.transition(state, 'E'); state = machine.transition(state, 'IGNORE'); expect(state.activities.B_ACTIVITY).toBeTruthy(); });});
describe('activities', () => { it('identifies initial activities', () => { const { initialState } = lightMachine;
expect(initialState.activities.fadeInGreen).toBeTruthy(); }); it('identifies start activities', () => { const nextState = lightMachine.transition('yellow', 'TIMER'); expect(nextState.activities.activateCrosswalkLight).toBeTruthy(); expect(nextState.actions).toEqual([start('activateCrosswalkLight')]); });
it('identifies start activities for child states and active activities', () => { const redWalkState = lightMachine.transition('yellow', 'TIMER'); const nextState = lightMachine.transition(redWalkState, 'PED_WAIT'); expect(nextState.activities.activateCrosswalkLight).toBeTruthy(); expect(nextState.activities.blinkCrosswalkLight).toBeTruthy(); expect(nextState.actions).toEqual([start('blinkCrosswalkLight')]); });
it('identifies stop activities for child states', () => { const redWalkState = lightMachine.transition('yellow', 'TIMER'); const redWaitState = lightMachine.transition(redWalkState, 'PED_WAIT'); const nextState = lightMachine.transition(redWaitState, 'PED_STOP');
expect(nextState.activities.activateCrosswalkLight).toBeTruthy(); expect(nextState.activities.blinkCrosswalkLight).toBe(false); expect(nextState.actions).toEqual([stop('blinkCrosswalkLight')]); });
it('identifies multiple stop activities for child and parent states', () => { const redWalkState = lightMachine.transition('yellow', 'TIMER'); const redWaitState = lightMachine.transition(redWalkState, 'PED_WAIT'); const redStopState = lightMachine.transition(redWaitState, 'PED_STOP'); const nextState = lightMachine.transition(redStopState, 'TIMER');
expect(nextState.activities.fadeInGreen).toBeTruthy(); expect(nextState.activities.activateCrosswalkLight).toBe(false); expect(nextState.activities.blinkCrosswalkLight).toBe(false);
expect(nextState.actions).toEqual([ stop('activateCrosswalkLight'), start('fadeInGreen') ]); });});
describe('transient activities', () => { const machine = Machine({ type: 'parallel', states: { A: { activities: ['A'], initial: 'A1', states: { A1: { activities: ['A1'], on: { A: 'AWAIT' } }, AWAIT: { activities: ['AWAIT'], on: { '': 'A2' } }, A2: { activities: ['A2'], on: { A: 'A1' } } }, on: { A1: '.A1', A2: '.A2' } }, B: { initial: 'B1', activities: ['B'], states: { B1: { activities: ['B1'], on: { '': [ { in: 'A.AWAIT', target: 'B2' } ], B: 'B2' } }, B2: { activities: ['B2'], on: { B: 'B1' } } }, on: { B1: '.B1', B2: '.B2' } }, C: { initial: 'C1', states: { C1: { activities: ['C1'], on: { C: 'C1', C_SIMILAR: 'C2' } }, C2: { activities: ['C1'] } } } } });
it('should have started initial activities', () => { const state = machine.initialState; expect(state.activities.A).toBeTruthy(); });
it('should have started deep initial activities', () => { const state = machine.initialState; expect(state.activities.A1).toBeTruthy(); });
it('should have kept existing activities', () => { let state = machine.initialState; state = machine.transition(state, 'A'); expect(state.activities.A).toBeTruthy(); });
it('should have kept same activities', () => { let state = machine.initialState; state = machine.transition(state, 'C_SIMILAR'); expect(state.activities.C1).toBeTruthy(); });
it('should have kept same activities after self transition', () => { let state = machine.initialState; state = machine.transition(state, 'C'); expect(state.activities.C1).toBeTruthy(); });
it('should have stopped after automatic transitions', () => { let state = machine.initialState; state = machine.transition(state, 'A'); expect(state.value).toEqual({ A: 'A2', B: 'B2', C: 'C1' }); expect(state.activities.B2).toBeTruthy(); });});
xstate

Version Info

Tagged at
2 years ago