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
122 lines (106 loc) · 4.55 KB
/
test_initialize.js
File metadata and controls
122 lines (106 loc) · 4.55 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
//-----------------------------------------------------------------------------
QUnit.module("special initialization options", {
setup: function() {
this.called = [];
this.onbeforeevent = function(event,from,to) { this.called.push('onbefore(' + event + ')'); },
this.onafterevent = function(event,from,to) { this.called.push('onafter(' + event + ')'); },
this.onleavestate = function(event,from,to) { this.called.push('onleave(' + from + ')'); },
this.onenterstate = function(event,from,to) { this.called.push('onenter(' + to + ')'); },
this.onchangestate = function(event,from,to) { this.called.push('onchange(' + from + ',' + 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",
"onbefore(startup)",
"onleavenone",
"onleave(none)",
"onentergreen",
"onenter(green)",
"onchange(none,green)",
"onafterstartup",
"onafter(startup)"
]);
});
//-----------------------------------------------------------------------------
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",
"onbefore(init)",
"onleavenone",
"onleave(none)",
"onentergreen",
"onenter(green)",
"onchange(none,green)",
"onafterinit",
"onafter(init)"
]);
});
//-----------------------------------------------------------------------------
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",
"onbefore(init)",
"onleavenone",
"onleave(none)",
"onentergreen",
"onenter(green)",
"onchange(none,green)",
"onafterinit",
"onafter(init)"
]);
});
//-----------------------------------------------------------------------------