Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
var browser_history_support = (window.history != null ? window.history.pushState : null) != null;
2
3
createTest('Nested route with the many children as a tokens, callbacks should yield historic params', {
4
'/a': {
5
'/:id': {
6
'/:id': function(a, b) {
7
if (!browser_history_support) {
8
shared.fired.push(location.hash.replace(/^#/, ''), a, b);
9
} else {
10
shared.fired.push(location.pathname, a, b);
11
}
12
}
13
}
14
}
15
}, function() {
16
this.navigate('/a/b/c', function() {
17
deepEqual(shared.fired, ['/a/b/c', 'b', 'c']);
18
this.finish();
19
});
20
});
21
22
createTest('Nested route with the first child as a token, callback should yield a param', {
23
'/foo': {
24
'/:id': {
25
on: function(id) {
26
if (!browser_history_support) {
27
shared.fired.push(location.hash.replace(/^#/, ''), id);
28
} else {
29
shared.fired.push(location.pathname, id);
30
}
31
}
32
}
33
}
34
}, function() {
35
this.navigate('/foo/a', function() {
36
this.navigate('/foo/b/c', function() {
37
deepEqual(shared.fired, ['/foo/a', 'a']);
38
this.finish();
39
});
40
});
41
});
42
43
createTest('Nested route with the first child as a regexp, callback should yield a param', {
44
'/foo': {
45
'/(\\w+)': {
46
on: function(value) {
47
if (!browser_history_support) {
48
shared.fired.push(location.hash.replace(/^#/, ''), value);
49
} else {
50
shared.fired.push(location.pathname, value);
51
}
52
}
53
}
54
}
55
}, function() {
56
this.navigate('/foo/a', function() {
57
this.navigate('/foo/b/c', function() {
58
deepEqual(shared.fired, ['/foo/a', 'a']);
59
this.finish();
60
});
61
});
62
});
63
64
createTest('Nested route with the several regular expressions, callback should yield a param', {
65
'/a': {
66
'/(\\w+)': {
67
'/(\\w+)': function(a, b) {
68
shared.fired.push(a, b);
69
}
70
}
71
}
72
}, function() {
73
this.navigate('/a/b/c', function() {
74
deepEqual(shared.fired, ['b', 'c']);
75
this.finish();
76
});
77
});
78
79
createTest('Single nested route with on member containing function value', {
80
'/a': {
81
'/b': {
82
on: function() {
83
if (!browser_history_support) {
84
shared.fired.push(location.hash.replace(/^#/, ''));
85
} else {
86
shared.fired.push(location.pathname);
87
}
88
}
89
}
90
}
91
}, function() {
92
this.navigate('/a/b', function() {
93
deepEqual(shared.fired, ['/a/b']);
94
this.finish();
95
});
96
});
97
98
createTest('Single non-nested route with on member containing function value', {
99
'/a/b': {
100
on: function() {
101
if (!browser_history_support) {
102
shared.fired.push(location.hash.replace(/^#/, ''));
103
} else {
104
shared.fired.push(location.pathname);
105
}
106
}
107
}
108
}, function() {
109
this.navigate('/a/b', function() {
110
deepEqual(shared.fired, ['/a/b']);
111
this.finish();
112
});
113
});
114
115
createTest('Single nested route with on member containing array of function values', {
116
'/a': {
117
'/b': {
118
on: [function() {
119
if (!browser_history_support) {
120
shared.fired.push(location.hash.replace(/^#/, ''));
121
} else {
122
shared.fired.push(location.pathname);
123
}
124
},
125
function() {
126
if (!browser_history_support) {
127
shared.fired.push(location.hash.replace(/^#/, ''));
128
} else {
129
shared.fired.push(location.pathname);
130
}
131
}]
132
}
133
}
134
}, function() {
135
this.navigate('/a/b', function() {
136
deepEqual(shared.fired, ['/a/b', '/a/b']);
137
this.finish();
138
});
139
});
140
141
createTest('method should only fire once on the route.', {
142
'/a': {
143
'/b': {
144
once: function() {
145
shared.fired_count++;
146
}
147
}
148
}
149
}, function() {
150
this.navigate('/a/b', function() {
151
this.navigate('/a/b', function() {
152
this.navigate('/a/b', function() {
153
deepEqual(shared.fired_count, 1);
154
this.finish();
155
});
156
});
157
});
158
});
159
160
createTest('method should only fire once on the route, multiple nesting.', {
161
'/a': {
162
on: function() { shared.fired_count++; },
163
once: function() { shared.fired_count++; }
164
},
165
'/b': {
166
on: function() { shared.fired_count++; },
167
once: function() { shared.fired_count++; }
168
}
169
}, function() {
170
this.navigate('/a', function() {
171
this.navigate('/b', function() {
172
this.navigate('/a', function() {
173
this.navigate('/b', function() {
174
deepEqual(shared.fired_count, 6);
175
this.finish();
176
});
177
});
178
});
179
});
180
});
181
182
createTest('overlapping routes with tokens.', {
183
'/a/:b/c' : function() {
184
if (!browser_history_support) {
185
shared.fired.push(location.hash.replace(/^#/, ''));
186
} else {
187
shared.fired.push(location.pathname);
188
}
189
},
190
'/a/:b/c/:d' : function() {
191
if (!browser_history_support) {
192
shared.fired.push(location.hash.replace(/^#/, ''));
193
} else {
194
shared.fired.push(location.pathname);
195
}
196
}
197
}, function() {
198
this.navigate('/a/b/c', function() {
199
200
this.navigate('/a/b/c/d', function() {
201
deepEqual(shared.fired, ['/a/b/c', '/a/b/c/d']);
202
this.finish();
203
});
204
});
205
});
206
207
// // //
208
// // // Recursion features
209
// // // ----------------------------------------------------------
210
211
createTest('Nested routes with no recursion', {
212
'/a': {
213
'/b': {
214
'/c': {
215
on: function c() {
216
shared.fired.push('c');
217
}
218
},
219
on: function b() {
220
shared.fired.push('b');
221
}
222
},
223
on: function a() {
224
shared.fired.push('a');
225
}
226
}
227
}, function() {
228
this.navigate('/a/b/c', function() {
229
deepEqual(shared.fired, ['c']);
230
this.finish();
231
});
232
});
233
234
createTest('Nested routes with backward recursion', {
235
'/a': {
236
'/b': {
237
'/c': {
238
on: function c() {
239
shared.fired.push('c');
240
}
241
},
242
on: function b() {
243
shared.fired.push('b');
244
}
245
},
246
on: function a() {
247
shared.fired.push('a');
248
}
249
}
250
}, {
251
recurse: 'backward'
252
}, function() {
253
this.navigate('/a/b/c', function() {
254
deepEqual(shared.fired, ['c', 'b', 'a']);
255
this.finish();
256
});
257
});
258
259
createTest('Breaking out of nested routes with backward recursion', {
260
'/a': {
261
'/:b': {
262
'/c': {
263
on: function c() {
264
shared.fired.push('c');
265
}
266
},
267
on: function b() {
268
shared.fired.push('b');
269
return false;
270
}
271
},
272
on: function a() {
273
shared.fired.push('a');
274
}
275
}
276
}, {
277
recurse: 'backward'
278
}, function() {
279
this.navigate('/a/b/c', function() {
280
deepEqual(shared.fired, ['c', 'b']);
281
this.finish();
282
});
283
});
284
285
createTest('Nested routes with forward recursion', {
286
'/a': {
287
'/b': {
288
'/c': {
289
on: function c() {
290
shared.fired.push('c');
291
}
292
},
293
on: function b() {
294
shared.fired.push('b');
295
}
296
},
297
on: function a() {
298
shared.fired.push('a');
299
}
300
}
301
}, {
302
recurse: 'forward'
303
}, function() {
304
this.navigate('/a/b/c', function() {
305
deepEqual(shared.fired, ['a', 'b', 'c']);
306
this.finish();
307
});
308
});
309
310
createTest('Nested routes with forward recursion, single route with an after event.', {
311
'/a': {
312
'/b': {
313
'/c': {
314
on: function c() {
315
shared.fired.push('c');
316
},
317
after: function() {
318
shared.fired.push('c-after');
319
}
320
},
321
on: function b() {
322
shared.fired.push('b');
323
}
324
},
325
on: function a() {
326
shared.fired.push('a');
327
}
328
}
329
}, {
330
recurse: 'forward'
331
}, function() {
332
this.navigate('/a/b/c', function() {
333
this.navigate('/a/b', function() {
334
deepEqual(shared.fired, ['a', 'b', 'c', 'c-after', 'a', 'b']);
335
this.finish();
336
});
337
});
338
});
339
340
createTest('Breaking out of nested routes with forward recursion', {
341
'/a': {
342
'/b': {
343
'/c': {
344
on: function c() {
345
shared.fired.push('c');
346
}
347
},
348
on: function b() {
349
shared.fired.push('b');
350
return false;
351
}
352
},
353
on: function a() {
354
shared.fired.push('a');
355
}
356
}
357
}, {
358
recurse: 'forward'
359
}, function() {
360
this.navigate('/a/b/c', function() {
361
deepEqual(shared.fired, ['a', 'b']);
362
this.finish();
363
});
364
});
365
366
//
367
// ABOVE IS WORKING
368
//
369
370
// //
371
// // Special Events
372
// // ----------------------------------------------------------
373
374
createTest('All global event should fire after every route', {
375
'/a': {
376
on: function a() {
377
shared.fired.push('a');
378
}
379
},
380
'/b': {
381
'/c': {
382
on: function a() {
383
shared.fired.push('a');
384
}
385
}
386
},
387
'/d': {
388
'/:e': {
389
on: function a() {
390
shared.fired.push('a');
391
}
392
}
393
}
394
}, {
395
after: function() {
396
shared.fired.push('b');
397
}
398
}, function() {
399
this.navigate('/a', function() {
400
this.navigate('/b/c', function() {
401
this.navigate('/d/e', function() {
402
deepEqual(shared.fired, ['a', 'b', 'a', 'b', 'a']);
403
this.finish();
404
});
405
});
406
});
407
408
});
409
410
createTest('Not found.', {
411
'/a': {
412
on: function a() {
413
shared.fired.push('a');
414
}
415
},
416
'/b': {
417
on: function a() {
418
shared.fired.push('b');
419
}
420
}
421
}, {
422
notfound: function() {
423
shared.fired.push('notfound');
424
}
425
}, function() {
426
this.navigate('/c', function() {
427
this.navigate('/d', function() {
428
deepEqual(shared.fired, ['notfound', 'notfound']);
429
this.finish();
430
});
431
});
432
});
433
434
createTest('On all.', {
435
'/a': {
436
on: function a() {
437
shared.fired.push('a');
438
}
439
},
440
'/b': {
441
on: function a() {
442
shared.fired.push('b');
443
}
444
}
445
}, {
446
on: function() {
447
shared.fired.push('c');
448
}
449
}, function() {
450
this.navigate('/a', function() {
451
this.navigate('/b', function() {
452
deepEqual(shared.fired, ['a', 'c', 'b', 'c']);
453
this.finish();
454
});
455
});
456
});
457
458
459
createTest('After all.', {
460
'/a': {
461
on: function a() {
462
shared.fired.push('a');
463
}
464
},
465
'/b': {
466
on: function a() {
467
shared.fired.push('b');
468
}
469
}
470
}, {
471
after: function() {
472
shared.fired.push('c');
473
}
474
}, function() {
475
this.navigate('/a', function() {
476
this.navigate('/b', function() {
477
deepEqual(shared.fired, ['a', 'c', 'b']);
478
this.finish();
479
});
480
});
481
});
482
483
createTest('resource object.', {
484
'/a': {
485
'/b/:c': {
486
on: 'f1'
487
},
488
on: 'f2'
489
},
490
'/d': {
491
on: ['f1', 'f2']
492
}
493
},
494
{
495
resource: {
496
f1: function (name){
497
shared.fired.push("f1-" + name);
498
},
499
f2: function (name){
500
shared.fired.push("f2");
501
}
502
}
503
}, function() {
504
this.navigate('/a/b/c', function() {
505
this.navigate('/d', function() {
506
deepEqual(shared.fired, ['f1-c', 'f1-undefined', 'f2']);
507
this.finish();
508
});
509
});
510
});
511
512
createTest('argument matching should be case agnostic', {
513
'/fooBar/:name': {
514
on: function(name) {
515
shared.fired.push("fooBar-" + name);
516
}
517
}
518
}, function() {
519
this.navigate('/fooBar/tesTing', function() {
520
deepEqual(shared.fired, ['fooBar-tesTing']);
521
this.finish();
522
});
523
});
524
525
createTest('sanity test', {
526
'/is/:this/:sane': {
527
on: function(a, b) {
528
shared.fired.push('yes ' + a + ' is ' + b);
529
}
530
},
531
'/': {
532
on: function() {
533
shared.fired.push('is there sanity?');
534
}
535
}
536
}, function() {
537
this.navigate('/is/there/sanity', function() {
538
deepEqual(shared.fired, ['yes there is sanity']);
539
this.finish();
540
});
541
});
542
543
createTest('`/` route should be navigable from the routing table', {
544
'/': {
545
on: function root() {
546
shared.fired.push('/');
547
}
548
},
549
'/:username': {
550
on: function afunc(username) {
551
shared.fired.push('/' + username);
552
}
553
}
554
}, function() {
555
this.navigate('/', function root() {
556
deepEqual(shared.fired, ['/']);
557
this.finish();
558
});
559
});
560
561
createTest('`/` route should not override a `/:token` route', {
562
'/': {
563
on: function root() {
564
shared.fired.push('/');
565
}
566
},
567
'/:username': {
568
on: function afunc(username) {
569
shared.fired.push('/' + username);
570
}
571
}
572
}, function() {
573
this.navigate('/a', function afunc() {
574
deepEqual(shared.fired, ['/a']);
575
this.finish();
576
});
577
});
578
579
createTest('should accept the root as a token.', {
580
'/:a': {
581
on: function root() {
582
if (!browser_history_support) {
583
shared.fired.push(location.hash.replace(/^#/, ''));
584
} else {
585
shared.fired.push(location.pathname);
586
}
587
}
588
}
589
}, function() {
590
this.navigate('/a', function root() {
591
deepEqual(shared.fired, ['/a']);
592
this.finish();
593
});
594
});
595
596
createTest('routes should allow wildcards.', {
597
'/:a/b*d': {
598
on: function() {
599
if (!browser_history_support) {
600
shared.fired.push(location.hash.replace(/^#/, ''));
601
} else {
602
shared.fired.push(location.pathname);
603
}
604
}
605
}
606
}, function() {
607
this.navigate('/a/bcd', function root() {
608
deepEqual(shared.fired, ['/a/bcd']);
609
this.finish();
610
});
611
});
612
613
createTest('functions should have |this| context of the router instance.', {
614
'/': {
615
on: function root() {
616
shared.fired.push(!!this.routes);
617
}
618
}
619
}, function() {
620
this.navigate('/', function root() {
621
deepEqual(shared.fired, [true]);
622
this.finish();
623
});
624
});
625
626
createTest('setRoute with a single parameter should change location correctly', {
627
'/bonk': {
628
on: function() {
629
if (!browser_history_support) {
630
shared.fired.push(location.hash.replace(/^#/, ''));
631
} else {
632
shared.fired.push(window.location.pathname);
633
}
634
}
635
}
636
}, function() {
637
var self = this;
638
this.router.setRoute('/bonk');
639
setTimeout(function() {
640
deepEqual(shared.fired, ['/bonk']);
641
self.finish();
642
}, 14)
643
});
644
645
createTest('route should accept _ and . within parameters', {
646
'/:a': {
647
on: function root() {
648
if (!browser_history_support) {
649
shared.fired.push(location.hash.replace(/^#/, ''));
650
} else {
651
shared.fired.push(location.pathname);
652
}
653
}
654
}
655
}, function() {
656
this.navigate('/a_complex_route.co.uk', function root() {
657
deepEqual(shared.fired, ['/a_complex_route.co.uk']);
658
this.finish();
659
});
660
});
661
662