router.js
10.4 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
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
371
372
373
374
375
376
377
378
379
define([
"../_base/array",
"../hash",
"../router/RouterBase",
"doh"
], function(arrayUtil, hash, RouterBase, doh){
// This test uses RouterBase so that I can test a few different behaviors of the router
// which require re-initializing a new router
var count = 0,
router = new RouterBase(),
handle, foo;
// Simple helper to make tearDown simpler
function removeAll(handles) {
arrayUtil.forEach(handles, function(handle){
handle.remove();
});
}
doh.register("tests.router", [
{
name: "Router API",
setUp: function(t){
// Reset the hash to make sure we get a clean test
hash("", true);
},
runTest: function(t){
t.t(router.register, "Router has a register");
t.t(router.go, "Router has a go");
t.t(router.startup, "Router has a startup");
t.t(router.destroy, "Router has a destroy");
}
},
{
name: "Registering a route by string",
runTest: function(t){
handle = router.register("/foo", function(){
count++;
console.log("/foo fired! New count:", count);
});
// Make sure it looks right
t.t(handle.remove, "Handle has a remove");
t.t(handle.register, "Handle has a register");
}
},
{
name: "Ensuring routes don't fire before startup",
setUp: function(){
count = 0;
},
runTest: function(t){
hash("/foo");
t.t(count === 0, "Count should have been 0, was " + count);
}
},
{
name: "Ensuring routes do fire after startup",
runTest: function(t){
router.startup();
t.t(count === 1, "Count should have been 1, was " + count);
}
},
{
name: "Ensuring that hash changes fire routes",
runTest: function(t){
// Due to the nature of the hashchange event,
// this test is going to be async - but we have to nest it,
// sadly.
var d = new doh.Deferred();
// Reset the hash
hash("");
setTimeout(function(){
// As soon as possible, set it back to our test...
hash("/foo");
console.log("Setting hash");
// ... and then check to make sure the events fired
setTimeout(d.getTestCallback(function(){
console.log("Checking count, current hash:", hash());
t.t(count === 2, "Count should have been 2, was " + count);
}), 50);
}, 0);
return d;
}
},
{
name: "Ensuring that router.go fires changes",
runTest: function(t){
var d = new doh.Deferred();
// Since router.go fires off routes immediately, this should
// kick off changes!
router.go("");
router.go("/foo");
t.t(count === 3, "Count should have been 3, was " + count);
}
},
{
name: "Ensuring route doesn't fire after removal",
runTest: function(t){
handle.remove();
router.go("");
router.go("/foo");
t.t(count === 3, "Count should have been 3, was " + count);
}
},
{
name: "Registering a route by regexp",
runTest: function(t){
handle = router.register(/^\/bar$/, function(){
count++;
});
router.go("/bar");
t.t(count === 4, "Count should have been 4, was " + count);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Checking event object",
runTest: function(t){
var oldPath, newPath, params, stopImmediatePropagation, preventDefault;
router.go("");
handle = router.register("/checkEventObject/:foo", function(event){
oldPath = event.oldPath;
newPath = event.newPath;
params = event.params;
stopImmediatePropagation = typeof event.stopImmediatePropagation;
preventDefault = typeof event.preventDefault;
});
router.go("/checkEventObject/bar");
t.t(oldPath === "", "oldPath should be empty string, was " + oldPath);
t.t(newPath === "/checkEventObject/bar", "newPath should be '/checkEventObject/bar', was " + newPath);
t.t(params, "params should be a truthy value, was " + params);
t.t(params.hasOwnProperty("foo"), "params should have a .foo property");
t.t(params.foo === "bar", "params.foo should be bar, was " + params.foo);
t.t(stopImmediatePropagation === "function", "stopImmediatePropagation should be a function, was " + stopImmediatePropagation);
t.t(preventDefault === "function", "preventDefault should be a function, was " + preventDefault);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Checking extra arguments - string route",
runTest: function(t){
var a, b;
handle = router.register("/stringtest/:applied/:arg", function(event, applied, arg){
a = applied;
b = arg;
});
router.go("/stringtest/extra/args");
t.t(a === "extra", "a should have been 'extra', was " + a);
t.t(b === "args", "b should have been 'args', was " + b);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Checking extra arguments - regex route",
runTest: function(t){
var a, b;
handle = router.register(/\/regextest\/(\w+)\/(\w+)/, function(event, applied, arg){
a = applied;
b = arg;
});
router.go("/regextest/extra/args");
t.t(a === "extra", "a should have been 'extra', was " + a);
t.t(b === "args", "b should have been 'args', was " + b);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Registering long routes with placeholders",
runTest: function(t){
var testObject;
handle = router.register("/path/:to/:some/:long/*thing", function(event){
testObject = event.params;
});
router.go("/path/to/some/long/thing/this/is/in/splat");
t.t(testObject instanceof Object, "testObject should have been an object, but wasn't");
t.t(testObject.to === "to", "testObject.to should have been 'to', was " + testObject.to);
t.t(testObject.some === "some", "testObject.some should have been 'some', was " + testObject.some);
t.t(testObject["long"] === "long", "testObject.long should have been 'long', was " + testObject["long"]);
t.t(testObject.thing === "thing/this/is/in/splat", "testObject.thing should have been 'thing/this/is/in/splat', was " + testObject.thing);
testObject = null;
router.go("/path/1/2/3/4/5/6");
t.t(testObject instanceof Object, "testObject should have been an object, but wasn't");
t.t(testObject.to === "1", "testObject.to should have been '1', was " + testObject.to);
t.t(testObject.some === "2", "testObject.some should have been '2', was " + testObject.some);
t.t(testObject["long"] === "3", "testObject.long should have been '3', was " + testObject["long"]);
t.t(testObject.thing === "4/5/6", "testObject.thing should have been '4/5/6', was " + testObject.thing);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Using capture groups in a regex route",
runTest: function(t){
var testObject;
handle = router.register(/^\/path\/(\w+)\/(\d+)$/, function(event){
testObject = event.params;
});
router.go("/path/abcdef/1234");
t.t(testObject instanceof Array, "testObject should have been an array, but wasn't");
t.t(testObject[0] === "abcdef", "testObject[0] should have been 'abcdef', was " + testObject[0]);
t.t(testObject[1] === "1234", "testObject[1] should have been '1234', was " + testObject[1]);
testObject = null;
router.go("/path/abc/def");
t.t(testObject === null, "testObject should have been null, but wasn't");
router.go("/path/abc123/456def");
t.t(testObject === null, "testObject should have been null, but wasn't");
router.go("/path/abc123/456");
t.t(testObject instanceof Array, "testObject should have been an array, but wasn't");
t.t(testObject[0] === "abc123", "testObject[0] should have been 'abc123', was " + testObject[0]);
t.t(testObject[1] === "456", "testObject[1] should have been '456', was " + testObject[1]);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Testing registerBefore",
runTest: function(t){
var test = "";
handle = [];
handle.push(router.register("/isBefore", function(){
test += "1";
}));
handle.push(router.registerBefore("/isBefore", function(){
test += "2";
}));
handle.push(router.register("/isBefore", function(){
test += "3";
}));
handle.push(router.registerBefore("/isBefore", function(){
test += "4";
}));
handle.push(router.register("/isBefore", function(){
test += "5";
}));
router.go("/isBefore");
t.t(test === "42135", "test should have been '42135', was " + test);
},
tearDown: function(){
removeAll(handle);
}
},
{
name: "Stopping propagation",
runTest: function(t){
var test = "";
handle = [];
handle.push(router.register("/stopImmediatePropagation", function(){ test += "A"; }));
handle.push(router.register("/stopImmediatePropagation", function(){ test += "B"; }));
handle.push(router.register("/stopImmediatePropagation", function(event){
event.stopImmediatePropagation();
test += "C";
}));
handle.push(router.register("/stopImmediatePropagation", function(){ test += "D"; }));
handle.push(router.register("/stopImmediatePropagation", function(){ test += "E"; }));
router.go("/stopImmediatePropagation");
t.t(test === "ABC", "test should have been 'ABC', was " + test);
},
tearDown: function(){
removeAll(handle);
}
},
{
name: "Preventing default (change)",
runTest: function(t){
var prevented = false, goResult;
hash("");
t.t(hash() === "", "hash should be empty");
handle.push(router.register("/preventDefault", function(event){
event.preventDefault();
}));
goResult = router.go("/preventDefault");
t.t(hash() === "", "hash should still be empty");
t.t(goResult === false, "goResult should be false");
goResult = router.go("/someOtherPath");
t.t(hash() === "/someOtherPath", "hash should be '/someOtherPath'");
t.t(goResult === true, "goResult should be true");
handle.push(router.register("/allowDefault", function(event){
console.log("Doing something here without explicitly stopping");
}));
},
tearDown: function(){
removeAll(handle);
}
},
{
name: "Default router path",
setUp: function(){
// Set up a new router for use in this test
router.destroy();
router = new RouterBase();
// Start with a clean hash
hash("");
},
runTest: function(t){
var routeHit = false;
handle = router.register("/default", function(event){
routeHit = true;
});
router.startup("/default");
t.t(routeHit, "Our route was not hit, but should have been");
},
tearDown: function(){
handle.remove();
}
}
]);
});