-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
370 lines (320 loc) · 10.8 KB
/
test.js
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
var should = chai.should();
var name, options, stateMachine;
beforeEach(function() {
name = "test";
options = {
initialState: "state1",
//verbose: true,
states: ["state1", "state2", "state3"],
actions: {
action1: { from:"state1", to:"state2" },
action2: { from:"state2", to:"state3" },
action3: { from:"state3", to:"state1" },
action4: { from:["state2","state3"], to:"state1" }
}
};
});
describe("PromiseFSM", function() {
describe("#create()", function() {
context("without_promise_adapter", function() {
it("should trow error", function() {
(function() {
stateMachine = PromiseFSM.create(name, options)
}).should.throw(Error);
});
});
context("with_valid_options", function() {
it("should return new machine instance", function() {
PromiseFSM.setPromiseAdapter(P);
stateMachine = PromiseFSM.create(name, options);
stateMachine.should.be.an.Object;
});
});
});
describe("#getMachine()", function() {
context("with_invalid_name", function() {
it("should return undefined", function() {
should.equal(PromiseFSM.getMachine("somename"), undefined);
});
});
context("with_valid_name", function() {
it("should return machine instance", function() {
PromiseFSM.setPromiseAdapter(P);
stateMachine = PromiseFSM.create(name, options);
PromiseFSM.getMachine(name).should.equal(stateMachine);
});
});
});
});
describe("stateMachineInstance", function() {
beforeEach(function() {
PromiseFSM.setPromiseAdapter(P);
});
it("should have automatically generated action methods", function() {
stateMachine = PromiseFSM.create(name, options);
stateMachine.action1.should.be.a("function");
stateMachine.action2.should.be.a("function");
stateMachine.action3.should.be.a("function");
});
describe("#$getState()", function() {
context("with_initialState_undefined", function() {
it("should use the first state in options.states as initial state", function() {
stateMachine = PromiseFSM.create(name, options);
stateMachine.$getState().should.equal(options.states[0]);
});
});
context("with_initialState_defined", function() {
it("should return same as options.initialState", function() {
options.initialState = "state2";
stateMachine = PromiseFSM.create(name, options);
stateMachine.$getState().should.equal(options.initialState);
});
});
context("after_state_change", function() {
it("should return new state name", function() {
stateMachine = PromiseFSM.create(name, options);
stateMachine.$getState().should.equal(options.states[0]);
stateMachine.action1();
stateMachine.$getState().should.equal(options.states[1]);
});
});
});
describe("#actionMethods", function() {
beforeEach(function() {
stateMachine = PromiseFSM.create(name, options);
});
it("should return a \"thenable\"", function() {
var promise = stateMachine.action1();
promise.then.should.be.a("function");
});
describe("when_transition_is_legal", function() {
it("should trigger state change", function() {
stateMachine.action1();
stateMachine.$getState().should.equal("state2");
});
it("should invoke onFulfill callback", function(done) {
stateMachine.action1().then(function() {
done();
}, {
// not called
});
});
});
describe("when_transition_is_illegal", function() {
it("should not trigger state change", function() {
stateMachine.action2();
stateMachine.$getState().should.equal("state1");
});
it("should invoke onReject callback passing an ILLEGAL_TRANSITION error", function(done) {
stateMachine.action2().then(onFulfill, onReject);
function onFulfill() {
// not called
}
function onReject(e) {
try {
e.message.should.equal(PromiseFSM.ERRORS.ILLEGAL_TRANSITION);
done();
} catch(e) {
done(e);
}
}
});
});
it("should be possible to use multiple 'from' states", function() {
// Change to state2
stateMachine.action1();
stateMachine.$getState().should.equal("state2");
// Change back to state1
stateMachine.action4();
stateMachine.$getState().should.equal("state1");
// Change to state2 again
stateMachine.action1();
// Then change to state3
stateMachine.action2();
stateMachine.$getState().should.equal("state3");
// Change back to state1
stateMachine.action4();
stateMachine.$getState().should.equal("state1");
});
});
describe("#$addEventListener", function() {
beforeEach(function() {
stateMachine = PromiseFSM.create(name, options);
});
["ENTER_STATE", "EXIT_STATE", "LOCKED", "UNLOCKED", "STATE_CHANGED"].forEach(function(key) {
it("should be possible to subscribe to and receive " + key + " events", function(done) {
stateMachine.$addEventListener(PromiseFSM.EVENTS[key], function(evt) {
evt.type.should.equal(PromiseFSM.EVENTS[key]);
evt.from.should.equal("state1");
evt.to.should.equal("state2");
done();
});
stateMachine.action1();
});
});
});
describe("#$removeEventListener", function() {
beforeEach(function() {
stateMachine = PromiseFSM.create(name, options);
});
["ENTER_STATE", "EXIT_STATE", "LOCKED", "UNLOCKED", "STATE_CHANGED"].forEach(function(key) {
it("should be possible to unsubscribe to " + key + " events", function() {
var receivedOnce = false;
function listener() {
if(receivedOnce === true) {
throw new Error("still subscribed to " + key);
}
receivedOnce = true;
}
stateMachine.$addEventListener(PromiseFSM.EVENTS[key], listener);
stateMachine.action1();
stateMachine.$removeEventListener(PromiseFSM.EVENTS[key], listener);
stateMachine.action2();
});
});
});
describe("#$addTransition()", function() {
beforeEach(function() {
stateMachine = PromiseFSM.create(name, options);
});
it("should be possible to add a synchronous blocking transition", function(done) {
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.action1().then(function() {
try {
stateMachine.$getState().should.equal("state2");
done();
} catch(e) {
done(e);
}
});
});
it("should be possible to add an asynchronous blocking transition", function(done) {
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
setTimeout(resolve, 50);
});
stateMachine.action1().then(function() {
try {
stateMachine.$getState().should.equal("state2");
done();
} catch(e) {
done(e);
}
});
});
it("should be possible to add several synchronous blocking transitions", function(done) {
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.action1().then(function() {
try {
stateMachine.$getState().should.equal("state2");
done();
} catch(e) {
done(e);
}
});
});
it("should be possible to add several asynchronous blocking transitions", function(done) {
var firstDone = false;
var secondDone = false;
var thirdDone = false;
stateMachine.$addTransition("state1", "state2", function(resolve) {
setTimeout(function() {
firstDone = true;
resolve();
}, 20);
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
setTimeout(function() {
secondDone = true;
resolve();
}, 40);
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
setTimeout(function() {
thirdDone = true;
resolve();
}, 60);
});
stateMachine.action1().then(function() {
try {
(firstDone).should.be.true;
(secondDone).should.be.true;
(thirdDone).should.be.true;
done();
} catch(e) {
done(e);
}
});
});
it("should be possible to mix synchronous and asynchronous transitions", function(done) {
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
setTimeout(function() {
resolve();
}, 50);
});
stateMachine.$addTransition("state1", "state2", function(resolve) {
stateMachine.$getState().should.equal("state1");
resolve();
});
stateMachine.action1().then(function() {
try {
stateMachine.$getState().should.equal("state2");
done();
} catch(e) {
done(e);
}
});
});
it("callback should retrieve arguments passed to action methods", function(done) {
stateMachine.$addTransition("state1", "state2", function(resolve, param1, param2) {
should.exist(param1);
param1.should.equal("param1");
should.exist(param2);
param2.should.equal("param2");
resolve();
});
stateMachine.action1("param1", "param2").then(function() {
done();
});
})
});
describe("#$removeTransition()", function() {
beforeEach(function() {
stateMachine = PromiseFSM.create(name, options);
});
it("should be possible to remove a blocking transition", function(done) {
function doAsyncStuff(resolve) {
throw new Error("doAsyncStuff() called");
}
stateMachine.$addTransition("state1", "state2", doAsyncStuff);
stateMachine.$removeTransition("state1", "state2", doAsyncStuff);
stateMachine.action1().then(completeTest);
function completeTest() {
try {
stateMachine.$getState().should.equal("state2");
done();
} catch(e) {
done(e);
}
}
});
});
});