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

actionCreators.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
import * as actions from '../src/actions';import { toSCXMLEvent } from '../src/utils';
const { actionTypes } = actions;
describe('action creators', () => { (['start', 'stop'] as const).forEach((actionKey) => { describe(`${actionKey}()`, () => { it('should accept a string action', () => { const action = actions[actionKey]('test'); expect(action.type).toEqual(actionTypes[actionKey]); expect(action).toEqual({ type: actionTypes[actionKey], exec: undefined, activity: { type: 'test', exec: undefined, id: 'test' } }); });
it('should accept an action object', () => { const action = actions[actionKey]({ type: 'test', foo: 'bar' } as any); expect(action.type).toEqual(actionTypes[actionKey]); expect(action).toEqual({ type: actionTypes[actionKey], exec: undefined, activity: { type: 'test', id: undefined, foo: 'bar' } }); });
it('should accept an activity definition', () => { const action = actions[actionKey]({ type: 'test', foo: 'bar', src: 'someSrc' } as any); expect(action.type).toEqual(actionTypes[actionKey]); expect(action).toEqual({ type: actionTypes[actionKey], exec: undefined, activity: { type: 'test', id: undefined, foo: 'bar', src: 'someSrc' } }); }); }); });
describe('send()', () => { it('should accept a string event', () => { const action = actions.send('foo'); expect(action).toEqual({ to: undefined, type: actionTypes.send, event: { type: 'foo' }, delay: undefined, id: 'foo' }); });
it('should accept an event object', () => { const action = actions.send({ type: 'foo', bar: 'baz' }); expect(action).toEqual({ to: undefined, type: actionTypes.send, event: { type: 'foo', bar: 'baz' }, delay: undefined, id: 'foo' }); });
it('should accept an id option', () => { const action = actions.send('foo', { id: 'foo-id' }); expect(action).toEqual({ to: undefined, type: actionTypes.send, event: { type: 'foo' }, delay: undefined, id: 'foo-id' }); });
it('should accept a delay option', () => { const action = actions.send('foo', { delay: 1000 }); expect(action).toEqual({ to: undefined, type: actionTypes.send, event: { type: 'foo' }, delay: 1000, id: 'foo' }); });
it('should accept a delay option (expression)', () => { const action = actions.send< { delay: number }, { type: 'EVENT'; value: number } | { type: 'RECEIVED' } >('RECEIVED', { delay: (ctx, e) => ctx.delay + ('value' in e ? e.value : 0) });
const resolvedAction = actions.resolveSend( action, { delay: 100 }, toSCXMLEvent({ type: 'EVENT', value: 50 } as { type: 'EVENT'; value: number; }) );
expect(resolvedAction.delay).toEqual(150); }); });});
xstate

Version Info

Tagged at
2 years ago