forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async.js
More file actions
408 lines (320 loc) · 18.8 KB
/
test_async.js
File metadata and controls
408 lines (320 loc) · 18.8 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//-----------------------------------------------------------------------------
QUnit.module("async");
//-----------------------------------------------------------------------------
test("state transitions", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { return StateMachine.ASYNC; },
onleaveyellow: function() { return StateMachine.ASYNC; },
onleavered: function() { return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'red', "should still be red because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
});
//-----------------------------------------------------------------------------
test("state transitions with delays", function() {
stop(); // doing async stuff - dont run next qunit test until I call start() below
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { return StateMachine.ASYNC; },
onleaveyellow: function() { return StateMachine.ASYNC; },
onleavered: function() { return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
setTimeout(function() {
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
setTimeout(function() {
fsm.transition(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'red', "should still be red because we haven't transitioned yet");
setTimeout(function() {
fsm.transition(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
setTimeout(function() {
fsm.transition(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
start();
}, 10);
}, 10);
}, 10);
}, 10);
});
//-----------------------------------------------------------------------------
test("state transition fired during onleavestate callback - immediate", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { this.transition(); return StateMachine.ASYNC; },
onleaveyellow: function() { this.transition(); return StateMachine.ASYNC; },
onleavered: function() { this.transition(); return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
});
//-----------------------------------------------------------------------------
test("state transition fired during onleavestate callback - with delay", function() {
stop(); // doing async stuff - dont run next qunit test until I call start() below
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'panic', from: 'green', to: 'red' }
],
callbacks: {
onleavegreen: function() { setTimeout(function() { fsm.transition(); }, 10); return StateMachine.ASYNC; },
onenterred: function() {
equal(fsm.current, 'red', "panic event should transition from green to red");
start();
}
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.panic(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
});
//-----------------------------------------------------------------------------
test("state transition fired during onleavestate callback - but forgot to return ASYNC!", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { this.transition(); /* return StateMachine.ASYNC; */ },
onleaveyellow: function() { this.transition(); /* return StateMachine.ASYNC; */ },
onleavered: function() { this.transition(); /* return StateMachine.ASYNC; */ }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
});
//-----------------------------------------------------------------------------
test("state transitions sometimes synchronous and sometimes asynchronous", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
]
});
// default behavior is synchronous
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
// but add callbacks that return ASYNC and it magically becomes asynchronous
fsm.onleavegreen = function() { return StateMachine.ASYNC; }
fsm.onleaveyellow = function() { return StateMachine.ASYNC; }
fsm.onleavered = function() { return StateMachine.ASYNC; }
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'red', "panic event should transition from yellow to red");
fsm.calm(); equal(fsm.current, 'red', "should still be red because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "calm event should transition from red to yellow");
fsm.clear(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
// this allows you to make on-the-fly decisions about whether async or not ...
fsm.onleavegreen = function(event, from, to, async) {
if (async) {
setTimeout(function() {
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
start(); // move on to next test
}, 10);
return StateMachine.ASYNC;
}
}
fsm.onleaveyellow = fsm.onleavered = null;
fsm.warn(false); equal(fsm.current, 'yellow', "expected synchronous transition from green to yellow");
fsm.clear(); equal(fsm.current, 'green', "clear event should transition from yellow to green");
fsm.warn(true); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
stop(); // doing async stuff - dont run next qunit test until I call start() in callback above
});
//-----------------------------------------------------------------------------
test("state transition fired without completing previous transition", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { return StateMachine.ASYNC; },
onleaveyellow: function() { return StateMachine.ASYNC; },
onleavered: function() { return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
throws(fsm.calm.bind(fsm), /event calm inappropriate because previous transition did not complete/);
});
//-----------------------------------------------------------------------------
test("state transition can be cancelled (github issue #22)", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { return StateMachine.ASYNC; },
onleaveyellow: function() { return StateMachine.ASYNC; },
onleavered: function() { return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
fsm.warn(); equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
fsm.transition(); equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
fsm.panic(); equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
equal(fsm.can('panic'), false, "but cannot panic a 2nd time because a transition is still pending")
throws(fsm.panic.bind(fsm), /event panic inappropriate because previous transition did not complete/);
fsm.transition.cancel();
equal(fsm.current, 'yellow', "should still be yellow because we cancelled the async transition");
equal(fsm.can('panic'), true, "can now panic again because we cancelled previous async transition");
fsm.panic();
fsm.transition();
equal(fsm.current, 'red', "should finally be red now that we completed the async transition");
});
//-----------------------------------------------------------------------------
test("callbacks are ordered correctly", function() {
var called = [];
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' },
],
callbacks: {
// generic callbacks
onbeforeevent: function(event,from,to) { called.push('onbefore(' + event + ')'); },
onafterevent: function(event,from,to) { called.push('onafter(' + event + ')'); },
onleavestate: function(event,from,to) { called.push('onleave(' + from + ')'); },
onenterstate: function(event,from,to) { called.push('onenter(' + to + ')'); },
onchangestate: function(event,from,to) { called.push('onchange(' + from + ',' + to + ')'); },
// specific state callbacks
onentergreen: function() { called.push('onentergreen'); },
onenteryellow: function() { called.push('onenteryellow'); },
onenterred: function() { called.push('onenterred'); },
onleavegreen: function() { called.push('onleavegreen'); return StateMachine.ASYNC; },
onleaveyellow: function() { called.push('onleaveyellow'); return StateMachine.ASYNC; },
onleavered: function() { called.push('onleavered'); return StateMachine.ASYNC; },
// specific event callbacks
onbeforewarn: function() { called.push('onbeforewarn'); },
onbeforepanic: function() { called.push('onbeforepanic'); },
onbeforecalm: function() { called.push('onbeforecalm'); },
onbeforeclear: function() { called.push('onbeforeclear'); },
onafterwarn: function() { called.push('onafterwarn'); },
onafterpanic: function() { called.push('onafterpanic'); },
onaftercalm: function() { called.push('onaftercalm'); },
onafterclear: function() { called.push('onafterclear'); }
}
});
called = [];
fsm.warn(); deepEqual(called, ['onbeforewarn', 'onbefore(warn)', 'onleavegreen', 'onleave(green)']);
fsm.transition(); deepEqual(called, ['onbeforewarn', 'onbefore(warn)', 'onleavegreen', 'onleave(green)', 'onenteryellow', 'onenter(yellow)', 'onchange(green,yellow)', 'onafterwarn', 'onafter(warn)']);
called = [];
fsm.panic(); deepEqual(called, ['onbeforepanic', 'onbefore(panic)', 'onleaveyellow', 'onleave(yellow)']);
fsm.transition(); deepEqual(called, ['onbeforepanic', 'onbefore(panic)', 'onleaveyellow', 'onleave(yellow)', 'onenterred', 'onenter(red)', 'onchange(yellow,red)', 'onafterpanic', 'onafter(panic)']);
called = [];
fsm.calm(); deepEqual(called, ['onbeforecalm', 'onbefore(calm)', 'onleavered', 'onleave(red)']);
fsm.transition(); deepEqual(called, ['onbeforecalm', 'onbefore(calm)', 'onleavered', 'onleave(red)', 'onenteryellow', 'onenter(yellow)', 'onchange(red,yellow)', 'onaftercalm', 'onafter(calm)']);
called = [];
fsm.clear(); deepEqual(called, ['onbeforeclear', 'onbefore(clear)', 'onleaveyellow', 'onleave(yellow)']);
fsm.transition(); deepEqual(called, ['onbeforeclear', 'onbefore(clear)', 'onleaveyellow', 'onleave(yellow)', 'onentergreen', 'onenter(green)', 'onchange(yellow,green)', 'onafterclear', 'onafter(clear)']);
});
//-----------------------------------------------------------------------------
test("cannot fire event during existing transition", function() {
var fsm = StateMachine.create({
initial: 'green',
events: [
{ name: 'warn', from: 'green', to: 'yellow' },
{ name: 'panic', from: 'yellow', to: 'red' },
{ name: 'calm', from: 'red', to: 'yellow' },
{ name: 'clear', from: 'yellow', to: 'green' }
],
callbacks: {
onleavegreen: function() { return StateMachine.ASYNC; },
onleaveyellow: function() { return StateMachine.ASYNC; },
onleavered: function() { return StateMachine.ASYNC; }
}
});
equal(fsm.current, 'green', "initial state should be green");
equal(fsm.can('warn'), true, "should be able to warn");
equal(fsm.can('panic'), false, "should NOT be able to panic");
equal(fsm.can('calm'), false, "should NOT be able to calm");
equal(fsm.can('clear'), false, "should NOT be able to clear");
fsm.warn();
equal(fsm.current, 'green', "should still be green because we haven't transitioned yet");
equal(fsm.can('warn'), false, "should NOT be able to warn - during transition");
equal(fsm.can('panic'), false, "should NOT be able to panic - during transition");
equal(fsm.can('calm'), false, "should NOT be able to calm - during transition");
equal(fsm.can('clear'), false, "should NOT be able to clear - during transition");
fsm.transition();
equal(fsm.current, 'yellow', "warn event should transition from green to yellow");
equal(fsm.can('warn'), false, "should NOT be able to warn");
equal(fsm.can('panic'), true, "should be able to panic");
equal(fsm.can('calm'), false, "should NOT be able to calm");
equal(fsm.can('clear'), true, "should be able to clear");
fsm.panic();
equal(fsm.current, 'yellow', "should still be yellow because we haven't transitioned yet");
equal(fsm.can('warn'), false, "should NOT be able to warn - during transition");
equal(fsm.can('panic'), false, "should NOT be able to panic - during transition");
equal(fsm.can('calm'), false, "should NOT be able to calm - during transition");
equal(fsm.can('clear'), false, "should NOT be able to clear - during transition");
fsm.transition();
equal(fsm.current, 'red', "panic event should transition from yellow to red");
equal(fsm.can('warn'), false, "should NOT be able to warn");
equal(fsm.can('panic'), false, "should NOT be able to panic");
equal(fsm.can('calm'), true, "should be able to calm");
equal(fsm.can('clear'), false, "should NOT be able to clear");
});
//-----------------------------------------------------------------------------