-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
143 lines (116 loc) · 4.11 KB
/
tests.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
// Simple tests for stated view
(function() {
window.StatedViewTests = function() {
function assert(pass, msg) {
console.debug((pass ? 'PASS: ' : 'FAIL: ') + msg);
}
/**
* Test simple state transition
*/
var SimpleView = Backbone.StatedView.extend({
tagName: 'div',
className: 'test-elt',
state: 'off',
states: {
'click': {
'off' : 'on',
'on' : 'off'
}
},
on : function() {
},
off : function() {
},
});
/**
* Test simple state transition
*/
var NestedView = Backbone.StatedView.extend({
tagName: 'div',
className: 'test-elt',
template: _.template('<div class="inner"></div>'),
state: 'off',
states: {
'click .inner': {
'off' : 'on',
'on' : 'off'
}
},
render: function() {
$(this.el).html(this.template());
return this;
},
on : function() {
},
off : function() {
},
});
var SpaceShuttleView = Backbone.StatedView.extend({
tagName: 'div',
className: 'test-elt',
template: _.template('<div class="fuelup"></div><div class="launch"></div><div class="abort"></div><div class="reset"></div>'),
state: 'pad',
states: {
'click .fuelup': {
'pad' : 'fueled',
},
'click .launch': {
'fueled' : 'launch',
},
'click .abort': {
'pad' : 'abort',
'fueled' : 'abort',
'launch' : 'abort'
},
'click .reset': {
'launch' : 'pad',
'abort' : 'pad'
},
},
render: function() {
$(this.el).html(this.template());
return this;
},
pad : function() {
},
fueled : function() {
},
launch : function() {
},
abort : function() {
},
});
var simpleView = new SimpleView();
var nestedView = new NestedView();
var shuttleView = new SpaceShuttleView();
console.log("-------- Starting Simple Test --------");
$(simpleView.el).trigger('click');
assert(simpleView.state == 'on', 'Click moved the view into the "on" state. ');
$(simpleView.el).trigger('click');
assert(simpleView.state == 'off', 'Click moved the view into the "off" state. ');
$(simpleView.el).trigger('click');
assert(simpleView.state == 'on', 'Click moved the view back into the "on" state. ');
console.log("-------- Starting Nested Element Test --------");
nestedView.render();
$('.inner', nestedView.el).trigger('click');
assert(nestedView.state == 'on', 'Click moved the view into the "on" state. ');
$('.inner', nestedView.el).trigger('click');
assert(nestedView.state == 'off', 'Click moved the view into the "off" state. ');
$('.inner', nestedView.el).trigger('click');
assert(nestedView.state == 'on', 'Click moved the view back into the "on" state. ');
console.log("-------- Starting Complex State Graph Test --------");
shuttleView.render();
assert(shuttleView.state == 'pad', 'Space shuttle is on the pad');
$('.fuelup', shuttleView.el).trigger('click');
assert(shuttleView.state == 'fueled', 'Space shuttle has fuel');
$('.launch', shuttleView.el).trigger('click');
assert(shuttleView.state == 'launch', 'Space shuttle is ready for launch');
$('.reset', shuttleView.el).trigger('click');
assert(shuttleView.state == 'pad', 'Space shuttle has been reset');
$('.fuelup', shuttleView.el).trigger('click');
$('.launch', shuttleView.el).trigger('click');
$('.abort', shuttleView.el).trigger('click');
assert(shuttleView.state == 'abort', 'Space shuttle successfully aborted');
return 'Tests done.';
};
}).call(this);