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

scheduler.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
import { Scheduler } from '../src/scheduler';
describe('scheduler', () => { it('should process event only once', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) scheduler.schedule(() => { calledCount++; });
const expectedCount = 1; expect(calledCount).toEqual(expectedCount); });
it('should process more than one event', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) scheduler.schedule(() => { calledCount++; scheduler.schedule(() => { calledCount++; }); });
const expectedCount = 2; expect(calledCount).toEqual(expectedCount); });
it('should process events in the same order they were hit', () => { const order: number[] = []; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) scheduler.schedule(() => { order.push(1); scheduler.schedule(() => { order.push(2); }); scheduler.schedule(() => { order.push(3); scheduler.schedule(() => { order.push(5); }); }); scheduler.schedule(() => { order.push(4); }); });
const expectedOrder = [1, 2, 3, 4, 5]; expect(order.length).toEqual(expectedOrder.length); for (let i = 0; i < expectedOrder.length; i++) { expect(order[i]).toEqual(expectedOrder[i]); } });
it('should recover if error is thrown while processing event', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) expect(() => scheduler.schedule(() => { calledCount++; throw Error('Test'); }) ).toThrowErrorMatchingInlineSnapshot(`"Test"`); scheduler.schedule(() => { calledCount++; });
const expectedCount = 2; expect(calledCount).toEqual(expectedCount); });
it('should recover if error is thrown while processing the queue', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) expect(() => scheduler.schedule(() => { calledCount++; scheduler.schedule(() => { calledCount++; throw Error('Test'); }); }) ).toThrowErrorMatchingInlineSnapshot(`"Test"`); scheduler.schedule(() => { calledCount++; });
const expectedCount = 3; expect(calledCount).toEqual(expectedCount); });
it('should stop processing events if error condition is met', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) expect(() => scheduler.schedule(() => { calledCount++; scheduler.schedule(() => { calledCount++; throw Error('Test'); }); scheduler.schedule(() => { calledCount++; }); }) ).toThrowErrorMatchingInlineSnapshot(`"Test"`);
const expectedCount = 2; expect(calledCount).toEqual(expectedCount); });
it('should discard not processed events in the case of error condition', () => { let calledCount = 0; const scheduler = new Scheduler(); scheduler.initialize(); // TODO: refactor (use .start()) expect(() => scheduler.schedule(() => { calledCount++; scheduler.schedule(() => { calledCount++; throw Error('Test'); }); scheduler.schedule(() => { calledCount++; }); }) ).toThrowErrorMatchingInlineSnapshot(`"Test"`); scheduler.schedule(() => { calledCount++; });
const expectedCount = 3; expect(calledCount).toEqual(expectedCount); });
describe('deferred events', () => { it('should be able to defer events', () => { let calledCount = 0; const scheduler = new Scheduler({ deferEvents: true }); scheduler.schedule(() => { calledCount++; });
const expectedCount = 0; expect(calledCount).toEqual(expectedCount); scheduler.initialize(); // TODO: refactor (use .start()) const expectedFinalCount = 1; expect(calledCount).toEqual(expectedFinalCount); });
it('should process initialization before other events', () => { const callOrder: number[] = []; const scheduler = new Scheduler({ deferEvents: true }); scheduler.schedule(() => { callOrder.push(2); }); scheduler.schedule(() => { callOrder.push(3); }); scheduler.initialize(() => { callOrder.push(1); });
const expectedOrder = [1, 2, 3]; expect(callOrder.length).toEqual(expectedOrder.length); for (let i = 0; i < expectedOrder.length; i++) { expect(callOrder[i]).toEqual(expectedOrder[i]); } });
it('should not defer events after initialization', () => { const scheduler = new Scheduler({ deferEvents: true }); scheduler.initialize(); // TODO: refactor (use .start()) let calledCount = 0; scheduler.schedule(() => { calledCount++; });
const expectedCount = 1; expect(calledCount).toEqual(expectedCount); }); });});
xstate

Version Info

Tagged at
2 years ago