deno.land / x / xstate@xstate@4.33.6 / test / meta.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
import { createMachine, interpret, Machine } from '../src/index';
describe('state meta data', () => { const pedestrianStates = { initial: 'walk', states: { walk: { meta: { walkData: 'walk data' }, on: { PED_COUNTDOWN: 'wait' }, onEntry: 'enter_walk', onExit: 'exit_walk' }, wait: { meta: { waitData: 'wait data' }, on: { PED_COUNTDOWN: 'stop' }, onEntry: 'enter_wait', onExit: 'exit_wait' }, stop: { meta: { stopData: 'stop data' }, onEntry: 'enter_stop', onExit: 'exit_stop' } } };
const lightMachine = Machine({ key: 'light', initial: 'green', states: { green: { meta: ['green', 'array', 'data'], on: { TIMER: 'yellow', POWER_OUTAGE: 'red', NOTHING: 'green' }, onEntry: 'enter_green', onExit: 'exit_green' }, yellow: { meta: { yellowData: 'yellow data' }, on: { TIMER: 'red', POWER_OUTAGE: 'red' }, onEntry: 'enter_yellow', onExit: 'exit_yellow' }, red: { meta: { redData: { nested: { red: 'data', array: [1, 2, 3] } } }, on: { TIMER: 'green', POWER_OUTAGE: 'red', NOTHING: 'red' }, onEntry: 'enter_red', onExit: 'exit_red', ...pedestrianStates } } });
it('states should aggregate meta data', () => { const yellowState = lightMachine.transition('green', 'TIMER'); expect(yellowState.meta).toEqual({ 'light.yellow': { yellowData: 'yellow data' } }); expect('light.green' in yellowState.meta).toBeFalsy(); expect('light' in yellowState.meta).toBeFalsy(); });
it('states should aggregate meta data (deep)', () => { expect(lightMachine.transition('yellow', 'TIMER').meta).toEqual({ 'light.red': { redData: { nested: { array: [1, 2, 3], red: 'data' } } }, 'light.red.walk': { walkData: 'walk data' } }); });
// https://github.com/statelyai/xstate/issues/1105 it('services started from a persisted state should calculate meta data', (done) => { const machine = createMachine({ id: 'test', initial: 'first', states: { first: { meta: { name: 'first state' } }, second: { meta: { name: 'second state' } } } });
const service = interpret(machine).onTransition((state) => { expect(state.meta).toEqual({ 'test.second': { name: 'second state' } }); done(); }); service.start('second'); });});
describe('transition meta data', () => { it('should show meta data in transitions', () => { const machine = createMachine({ initial: 'inactive', states: { inactive: { on: { EVENT: { target: 'active', meta: { description: 'Going from inactive to active' } } } }, active: {} } });
const nextState = machine.transition(undefined, 'EVENT');
expect(nextState.transitions.map((t) => t.meta)).toMatchInlineSnapshot(` Array [ Object { "description": "Going from inactive to active", }, ] `); });});
describe('state description', () => { it('state node should have its description', () => { const machine = createMachine({ initial: 'test', states: { test: { description: 'This is a test' } } });
expect(machine.states.test.description).toEqual('This is a test'); });});
describe('transition description', () => { it('state node should have its description', () => { const machine = createMachine({ on: { EVENT: { description: 'This is a test' } } });
expect(machine.on['EVENT'][0].description).toEqual('This is a test'); });});
xstate

Version Info

Tagged at
2 years ago