-
Notifications
You must be signed in to change notification settings - Fork 0
/
injectigator_test.js
247 lines (198 loc) · 7.41 KB
/
injectigator_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
/**
* @fileoverview The Injectigator Unit Tests. See
* {@ link http://code.google.com/p/js-test-driver/wiki/GettingStarted} on how
* to get started.
*
* NOTE(ibolmo): You'll need to have ../jsunit/jsUnitMockTimeout.js from
* http://bit.ly/jsUnitMockTimeout.
*
* @author ibolmo@gmail.com (Olmo Maldonado)
*/
TestCase('Injectigator', {
setUp: function() {
Injectigator.initialize();
},
testMockClock: function() {
assertTrue('ensure jstd loaded Clock mock first',
(setTimeout+'').indexOf('Injectigator') != -1);
},
testIsNode: function() {
var node = function(){};
assertFalse('A normal function is not an Injectigator node.',
Injectigator.isNode(node));
node = Injectigator.fn();
assertTrue('A method from Injectigator.node is an Injectigator node',
Injectigator.isNode(node));
node = Injectigator.fn(function(){});
assertTrue(Injectigator.isNode(node));
},
testGetPreviousNode: function() {
assertEquals('At time 0, the previous node is the ROOT node.',
Injectigator.ROOT,
Injectigator.getPreviousNode());
},
testEnter: function() {
var called = 0;
var node = Injectigator.fn(function(){
called++;
});
Injectigator.enter(node);
assertEquals('The scoped called variable should still be 0.', called, 0);
// TODO(ibolmo): May be inappropriate to access these internal variables
// directly during testing. Use public interface once available.
assertEquals('The node has been called once', node.$called, 1);
assertNotNull('The node should have a start time', node.$start);
assertNull('The node should not have an end time', node.$end);
assertEquals('For the simplest case, the node\'s parent is the root node.',
node.$parent,
Injectigator.ROOT);
assertEquals('Previous node should be the ROOT for the first node',
Injectigator.ROOT,
node.$prev);
assertEquals('First child of the parent (ROOT) is the node',
node,
Injectigator.ROOT.$first);
assertEquals('Last child of the parent (ROOT) is the node',
node,
Injectigator.ROOT.$last);
// TODO(ibolmo): Consider calling enter on the same node twice.
},
testExit: function() {
var called = 0;
var node = Injectigator.fn(function() {
called++;
});
var result = Injectigator.enter(node).exit(node, 'result');
assertEquals('Exit should return the passed result.', 'result', result);
// TODO(ibolmo): Same as above. Use public interface once available.
assertNotNull('The node should have an end time.', node.$end);
assertNotNull('The node should have recorded an elapsed time.',
node.$elapsed[0]);
assertEquals(
'For the simplest case, the exit should not modify the previous node.',
node,
Injectigator.getPreviousNode());
// TODO(ibolmo): Consider calling exit on the same node twice.
},
testFn: function() {
var called = 0;
var node = Injectigator.fn(function() {
called++;
});
node();
assertEquals('called should be 1', 1, called);
node();
assertEquals('ensure fn is not broken after the first use', 2, called);
var AClass = function(){};
AClass.prototype.called = 0;
AClass.prototype.incr = Injectigator.fn(function(){
this.called++;
});
var A = new AClass();
A.incr();
assertEquals('Instance called should be 1', 1, A.called);
AClass.prototype.set = Injectigator.fn(function(){
this.args = Array.prototype.slice.call(arguments);
});
var B = new AClass();
var args = ['0', 1, null];
B.set.apply(B, args);
assertEquals('Arguments should pass correctly.', B.args, args);
},
testFnAndReturnedEmbeddedNode: function() {
var called = 0;
var rootNode = Injectigator.fn(function() {
return Injectigator.fn(function() {
called++;
});
});
var innerNode = rootNode();
assertEquals('called should still be 0', 0, called);
assertEquals('should be at rootNode',
rootNode,
Injectigator.getPreviousNode());
innerNode();
assertEquals(called, 1);
assertEquals('should be at innerNode',
innerNode,
Injectigator.getPreviousNode());
assertEquals('current parent should be ROOT',
Injectigator.ROOT,
innerNode.$parent);
assertEquals(innerNode, rootNode.$next);
assertEquals(rootNode, innerNode.$prev);
assertEquals('parent first child is rootNode',
rootNode,
Injectigator.ROOT.$first);
assertEquals('parent last child is innerNode',
innerNode,
Injectigator.ROOT.$last);
},
testFnAndOneEmbeddedCall: function() {
var rootNode, childNode;
var rootNode = Injectigator.fn(function(){
childNode = Injectigator.fn(function(){});
childNode();
return Injectigator.fn(function(){});
});
var innerNode = rootNode();
assertEquals('should be at rootNode since child exited',
rootNode,
Injectigator.getPreviousNode());
assertEquals(rootNode, Injectigator.ROOT.$first);
assertEquals('Child should not be the last node for the ROOT',
rootNode,
Injectigator.ROOT.$last);
assertEquals('rootNode should have a child',
childNode,
rootNode.$first);
assertEquals(childNode, rootNode.$last);
assertEquals('childNode has rootNode as parent',
rootNode,
childNode.$parent);
innerNode();
assertEquals('should be at innerNode',
innerNode,
Injectigator.getPreviousNode());
assertEquals('current parent should be ROOT',
Injectigator.ROOT,
innerNode.$parent);
// Check that links are correct.
assertEquals(innerNode, rootNode.$next);
assertEquals(rootNode, innerNode.$prev);
assertEquals('parent first child is rootNode',
rootNode,
Injectigator.ROOT.$first);
assertEquals('parent last child is innerNode',
innerNode,
Injectigator.ROOT.$last);
// Ensure that nothing has happened to the child.
assertEquals('Child should not be the last node for the ROOT',
innerNode,
Injectigator.ROOT.$last);
assertEquals('rootNode should have a child',
childNode,
rootNode.$first);
assertEquals(childNode, rootNode.$last);
assertEquals('childNode has rootNode as parent',
rootNode,
childNode.$parent);
},
testSetTimeoutFns: function() {
var called = 0;
var node = Injectigator.fn(function() {
called++;
});
setTimeout(node, 1000);
Clock.tick(1000);
assertEquals('function gets called normally', 1, called);
// TODO(ibolmo): Use a public method when available.
assertTrue('Node should be marked as delayed', node.$delayed);
// The node should not be part of the normal execution path.
assertFalse(!!Injectigator.ROOT.$first);
assertFalse(!!Injectigator.ROOT.$last);
// The node should be part of the async root (AROOT) path.
assertEquals(node, Injectigator.AROOT.$first);
assertEquals(node, Injectigator.AROOT.$last);
}
});