deno.land / x / xstate@xstate@4.33.6 / test / event.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
import { Machine, sendParent, interpret, assign } from '../src';import { respond, send } from '../src/actions';
describe('SCXML events', () => { it('should have the origin (id) from the sending machine service', (done) => { const childMachine = Machine({ initial: 'active', states: { active: { entry: sendParent('EVENT') } } });
const parentMachine = Machine({ initial: 'active', states: { active: { invoke: { id: 'child', src: childMachine }, on: { EVENT: { target: 'success', cond: (_: any, __: any, { _event }: any) => { return !!(_event.origin && _event.origin.length > 0); } } } }, success: { type: 'final' } } });
interpret(parentMachine) .onDone(() => done()) .start(); });
it('should have the origin (id) from the sending callback service', () => { const machine = Machine<{ childOrigin?: string }>({ initial: 'active', context: {}, states: { active: { invoke: { id: 'callback_child', src: () => (send) => send({ type: 'EVENT' }) }, on: { EVENT: { target: 'success', actions: assign({ childOrigin: (_, __, { _event }) => _event.origin }) } } }, success: { type: 'final' } } });
const service = interpret(machine).start();
expect(service.state.context.childOrigin).toBe('callback_child'); });
it('respond() should be able to respond to sender', (done) => { const authServerMachine = Machine({ initial: 'waitingForCode', states: { waitingForCode: { on: { CODE: { actions: respond('TOKEN', { delay: 10 }) } } } } });
const authClientMachine = Machine({ initial: 'idle', states: { idle: { on: { AUTH: 'authorizing' } }, authorizing: { invoke: { id: 'auth-server', src: authServerMachine }, entry: send('CODE', { to: 'auth-server' }), on: { TOKEN: 'authorized' } }, authorized: { type: 'final' } } });
const service = interpret(authClientMachine) .onDone(() => done()) .start();
service.send('AUTH'); });});
interface SignInContext { email: string; password: string;}
interface ChangePassword { type: 'changePassword'; password: string;}
const authMachine = Machine<SignInContext, ChangePassword>( { context: { email: '', password: '' }, initial: 'passwordField', states: { passwordField: { initial: 'hidden', states: { hidden: { on: { // We want to assign the new password but remain in the hidden // state changePassword: { actions: 'assignPassword' } } }, valid: {}, invalid: {} }, on: { changePassword: [ { cond: (_, event) => event.password.length >= 10, target: '.invalid', actions: ['assignPassword'] }, { target: '.valid', actions: ['assignPassword'] } ] } } } }, { actions: { assignPassword: assign<SignInContext, ChangePassword>({ password: (_, event) => event.password }) } });
describe('nested transitions', () => { it('only take the transition of the most inner matching event', () => { const password = 'xstate123'; const state = authMachine.transition(authMachine.initialState, { type: 'changePassword', password });
expect(state.value).toEqual({ passwordField: 'hidden' }); expect(state.context).toEqual({ password, email: '' }); });});
xstate

Version Info

Tagged at
2 years ago