forked from jakesgordon/javascript-state-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_initialize.js
More file actions
88 lines (72 loc) · 3.83 KB
/
test_initialize.js
File metadata and controls
88 lines (72 loc) · 3.83 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
//-----------------------------------------------------------------------------
module("special initialization options", {
setup: function() {
this.called = [];
this.onchangestate = function(event,from,to) { this.called.push('onchange from ' + from + ' to ' + to); };
this.onbeforeinit = function() { this.called.push("onbeforeinit"); };
this.onafterinit = function() { this.called.push("onafterinit"); };
this.onbeforestartup = function() { this.called.push("onbeforestartup"); };
this.onafterstartup = function() { this.called.push("onafterstartup"); };
this.onbeforepanic = function() { this.called.push("onbeforepanic"); };
this.onafterpanic = function() { this.called.push("onafterpanic"); };
this.onbeforecalm = function() { this.called.push("onbeforecalm"); };
this.onaftercalm = function() { this.called.push("onaftercalm"); };
this.onenternone = function() { this.called.push("onenternone"); };
this.onentergreen = function() { this.called.push("onentergreen"); };
this.onenterred = function() { this.called.push("onenterred"); };
this.onleavenone = function() { this.called.push("onleavenone"); };
this.onleavegreen = function() { this.called.push("onleavegreen"); };
this.onleavered = function() { this.called.push("onleavered"); };
}
});
//-----------------------------------------------------------------------------
test("initial state defaults to 'none'", function() {
StateMachine.create({
target: this,
events: [
{ name: 'panic', from: 'green', to: 'red' },
{ name: 'calm', from: 'red', to: 'green' }
]});
equal(this.current, 'none');
deepEqual(this.called, []);
});
//-----------------------------------------------------------------------------
test("initial state can be specified", function() {
StateMachine.create({
target: this,
initial: 'green',
events: [
{ name: 'panic', from: 'green', to: 'red' },
{ name: 'calm', from: 'red', to: 'green' }
]});
equal(this.current, 'green');
deepEqual(this.called, ["onbeforestartup", "onleavenone", "onentergreen", "onchange from none to green", "onafterstartup"]);
});
//-----------------------------------------------------------------------------
test("startup event name can be specified", function() {
StateMachine.create({
target: this,
initial: { state: 'green', event: 'init' },
events: [
{ name: 'panic', from: 'green', to: 'red' },
{ name: 'calm', from: 'red', to: 'green' }
]});
equal(this.current, 'green');
deepEqual(this.called, ["onbeforeinit", "onleavenone", "onentergreen", "onchange from none to green", "onafterinit"]);
});
//-----------------------------------------------------------------------------
test("startup event can be deferred", function() {
StateMachine.create({
target: this,
initial: { state: 'green', event: 'init', defer: true },
events: [
{ name: 'panic', from: 'green', to: 'red' },
{ name: 'calm', from: 'red', to: 'green' }
]});
equal(this.current, 'none');
deepEqual(this.called, []);
this.init();
equal(this.current, 'green');
deepEqual(this.called, ["onbeforeinit", "onleavenone", "onentergreen", "onchange from none to green", "onafterinit"]);
});
//-----------------------------------------------------------------------------