deno.land / x / xstate@xstate@4.33.6 / test / tags.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
import { createMachine } from '../src';
describe('tags', () => { it('supports tagging states', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: ['go'] }, yellow: { tags: ['go'], on: { TIMER: 'red' } }, red: { tags: ['stop'] } } });
expect(machine.initialState.hasTag('go')).toBeTruthy(); expect(machine.transition('yellow', 'TIMER').hasTag('go')).toBeFalsy(); });
it('supports tags in compound states', () => { const machine = createMachine({ initial: 'red', states: { green: { tags: ['go'] }, yellow: {}, red: { tags: ['stop'], initial: 'walk', states: { walk: { tags: ['crosswalkLight'] }, wait: { tags: ['crosswalkLight'] } } } } });
expect(machine.initialState.hasTag('go')).toBeFalsy(); expect(machine.initialState.hasTag('stop')).toBeTruthy(); expect(machine.initialState.hasTag('crosswalkLight')).toBeTruthy(); });
it('supports tags in parallel states', () => { const machine = createMachine({ type: 'parallel', states: { foo: { initial: 'active', states: { active: { tags: 'yes' }, inactive: { tags: 'no' } } }, bar: { initial: 'active', states: { active: { tags: 'yes', on: { DEACTIVATE: 'inactive' } }, inactive: { tags: 'no' } } } } });
let state = machine.initialState;
expect(state.tags).toEqual(new Set(['yes'])); state = machine.transition(state, 'DEACTIVATE'); expect(state.tags).toEqual(new Set(['yes', 'no'])); });
it('sets tags correctly after not selecting any transition', () => { const machine = createMachine({ initial: 'a', states: { a: { tags: 'myTag' } } });
const state = machine.transition(machine.initialState, { type: 'UNMATCHED' }); expect(state.hasTag('myTag')).toBeTruthy(); });
it('tags can be single (not array)', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: 'go' } } });
expect(machine.initialState.hasTag('go')).toBeTruthy(); });
it('stringifies to an array', () => { const machine = createMachine({ initial: 'green', states: { green: { tags: ['go', 'light'] } } });
const jsonState = machine.initialState.toJSON();
expect(jsonState.tags).toEqual(['go', 'light']); });});
xstate

Version Info

Tagged at
2 years ago