-
-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathrouter.spec.js
More file actions
501 lines (422 loc) · 18.2 KB
/
router.spec.js
File metadata and controls
501 lines (422 loc) · 18.2 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import {History} from 'aurelia-history';
import {Container} from 'aurelia-dependency-injection';
import {AppRouter} from '../src/app-router';
import {PipelineProvider} from '../src/pipeline-provider';
let absoluteRoot = 'http://aurelia.io/docs/';
export class MockHistory extends History {
activate() {}
deactivate() {}
navigate() {}
navigateBack() {}
getAbsoluteRoot() {
return absoluteRoot;
}
setState(key, value) {}
getState(key) {
return null;
}
}
describe('the router', () => {
let router;
let history;
beforeEach(() => {
history = new MockHistory();
router = new AppRouter(new Container(), history, new PipelineProvider(new Container()));
});
describe('addRoute', () => {
it('should register named routes', () => {
const child = router.createChild(new Container());
router.addRoute({ name: 'parent', route: 'parent', moduleId: 'parent' });
child.addRoute({ name: 'child', route: 'child', moduleId: 'child' });
expect(child.hasRoute('child')).toBe(true);
expect(child.hasRoute('parent')).toBe(true);
expect(child.hasOwnRoute('child')).toBe(true);
expect(child.hasOwnRoute('parent')).toBe(false);
expect(router.hasRoute('child')).toBe(false);
expect(router.hasRoute('parent')).toBe(true);
expect(router.hasOwnRoute('child')).toBe(false);
expect(router.hasOwnRoute('parent')).toBe(true);
});
it('should add a route to navigation if it has a nav=true', () => {
const config = { route: 'test', moduleId: 'test', title: 'Resume', nav: true };
const navModel = router.createNavModel(config);
router.addRoute(config, navModel);
expect(router.navigation).toContain(navModel);
});
it('should not add a route to navigation if it has a nav=false', () => {
let testRoute = {};
router.addRoute({ route: 'test', moduleId: 'test', title: 'Resume', nav: false }, testRoute);
expect(router.navigation).not.toContain(testRoute);
});
it('should reject dynamic routes specifying nav=true with no href', () => {
expect(() => router.addRoute({ route: 'test/:id', href: 'test', moduleId: 'test', nav: true })).not.toThrow();
expect(() => router.addRoute({ route: 'test/:id', moduleId: 'test', nav: true })).toThrow();
});
it('should add a route with multiple view ports', () => {
expect(() => router.addRoute({ route: 'multiple/viewports', viewPorts: {
'default': { moduleId: 'test1' },
'number2': { moduleId: 'test2' }
}})).not.toThrow();
});
it('should map a routeconfig with an array of routes to multiple routeconfigs with one route each', () => {
router.addRoute({ route: ['test1', 'test2'], moduleId: 'test', nav: true });
expect(router.routes[0].route).toEqual('test1');
expect(router.routes[1].route).toEqual('test2');
});
});
describe('generate', () => {
it('should generate route URIs', (done) => {
const child = router.createChild(new Container());
child.baseUrl = 'child-router';
Promise.all([
router.configure(config => config.map({ name: 'parent', route: 'parent', moduleId: './test' })),
child.configure(config => config.map({ name: 'child', route: 'child', moduleId: './test' }))
]).then(() => {
expect(router.generate('parent')).toBe('#/parent');
expect(child.generate('parent')).toBe('#/parent');
expect(child.generate('child')).toBe('#/child-router/child');
router.history._usePushState = true;
expect(router.generate('parent')).toBe('/parent');
expect(child.generate('parent')).toBe('/parent');
expect(child.generate('child')).toBe('/child-router/child');
done();
});
});
it('should delegate to parent when not configured', (done) => {
const child = router.createChild(new Container());
router.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => {
expect(child.generate('test', { id: 1 })).toBe('#/test/1');
done();
});
});
it('should delegate to parent when generating unknown route', (done) => {
const child = router.createChild(new Container());
Promise.all([
router.configure(config => config.map({ name: 'parent', route: 'parent/:id', moduleId: './test' })),
child.configure(config => config.map({ name: 'child', route: 'child/:id', moduleId: './test' }))
]).then(() => {
expect(child.generate('child', { id: 1 })).toBe('#/child/1');
expect(child.generate('parent', { id: 1 })).toBe('#/parent/1');
done();
});
});
it('should return a fully-qualified URL when options.absolute is true', (done) => {
const child = router.createChild(new Container());
let options = { absolute: true };
Promise.all([
router.configure(config => config.map({ name: 'parent', route: 'parent/:id', moduleId: './test' })),
child.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
])
.then(() => {
expect(child.generate('test', { id: 1 }, options)).toBe(`${absoluteRoot}#/test/1`);
expect(child.generate('parent', { id: 2 }, options)).toBe(`${absoluteRoot}#/parent/2`);
router.history._usePushState = true;
expect(child.generate('test', { id: 1 }, options)).toBe(`${absoluteRoot}test/1`);
expect(child.generate('parent', { id: 2 }, options)).toBe(`${absoluteRoot}parent/2`);
done();
});
});
});
describe('navigate', () => {
it('should navigate to absolute paths', (done) => {
const options = {};
spyOn(history, 'navigate');
const child = router.createChild(new Container());
child.baseUrl = 'child-router';
Promise.all([
router.configure(config => {
config.map([
{ name: 'parent', route: 'parent/:id', moduleId: './test' },
{ name: 'parent-empty', route: '', moduleId: './parent-empty' }
]);
}),
child.configure(config => {
config.map([
{ name: 'child', route: 'child/:id', moduleId: './test' },
{ name: 'empty', route: '', moduleId: './empty' }
]);
})
]).then(() => {
router.navigate('', options);
expect(history.navigate).toHaveBeenCalledWith('#/', options);
history.navigate.calls.reset();
router.navigate('#/test1', options);
expect(history.navigate).toHaveBeenCalledWith('#/test1', options);
history.navigate.calls.reset();
router.navigate('/test2', options);
expect(history.navigate).toHaveBeenCalledWith('#/test2', options);
history.navigate.calls.reset();
router.navigate('test3', options);
expect(history.navigate).toHaveBeenCalledWith('#/test3', options);
history.navigate.calls.reset();
child.navigate('#/test4', options);
expect(history.navigate).toHaveBeenCalledWith('#/test4', options);
history.navigate.calls.reset();
child.navigate('/test5', options);
expect(history.navigate).toHaveBeenCalledWith('#/test5', options);
history.navigate.calls.reset();
child.navigate('test6', options);
expect(history.navigate).toHaveBeenCalledWith('#/child-router/test6', options);
history.navigate.calls.reset();
child.navigate('#/child-router/test7', options);
expect(history.navigate).toHaveBeenCalledWith('#/child-router/test7', options);
history.navigate.calls.reset();
child.navigate('/child-router/test8', options);
expect(history.navigate).toHaveBeenCalledWith('#/child-router/test8', options);
history.navigate.calls.reset();
child.navigate('child-router/test9', options);
expect(history.navigate).toHaveBeenCalledWith('#/child-router/child-router/test9', options);
history.navigate.calls.reset();
child.navigate('', options);
expect(history.navigate).toHaveBeenCalledWith('#/child-router/', options);
history.navigate.calls.reset();
done();
});
});
it('should navigate to named routes', (done) => {
const options = {};
spyOn(history, 'navigate');
router.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => {
router.navigateToRoute('test', { id: 123 }, options);
expect(history.navigate).toHaveBeenCalledWith('#/test/123', options);
done();
});
});
});
describe('_createNavigationInstruction', () => {
it('should reject when router not configured', (done) => {
router._createNavigationInstruction()
.then(x => expect(true).toBeFalsy('should have rejected'))
.catch(reason => expect(reason).toBeTruthy())
.then(done);
});
it('should reject when route not found', (done) => {
router.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => router._createNavigationInstruction('test'))
.then(() => expect(true).toBeFalsy('should have rejected'))
.catch(reason => expect(reason).toBeTruthy())
.then(done);
});
it('should resolve matching routes', (done) => {
router.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => router._createNavigationInstruction('test/123?foo=456'))
.then(x => expect(x).toEqual(jasmine.objectContaining({ fragment: 'test/123', queryString: 'foo=456' })))
.catch(reason => fail(reason))
.then(done);
});
describe('should match routes with same pattern based on href', () => {
it('', (done) => {
router.configure(config => config.map([
{ name: 'a', route: 'test/:p', moduleId: './test', href: '/test/a' },
{ name: 'b', route: 'test/:p', moduleId: './test', href: '/test/b' }
]))
.then(() => router._createNavigationInstruction('test/b?foo=456'))
.then(i => {
expect(i.fragment).toEqual('test/b');
expect(i.queryString).toEqual('foo=456');
expect(i.params.p).toEqual('b');
expect(i.config.name).toEqual('b');
})
.catch(reason => fail(reason))
.then(done);
});
it('when fragment matches the child router', (done) => {
const childRouter = router.createChild(new Container());
router.configure(config => config.map([
{ name: 'a', route: 'parent/:p', moduleId: './parent', href: '/parent/a' },
{ name: 'b', route: 'parent/:p', moduleId: './parent', href: '/parent/b' }
]))
.then(() => childRouter.configure(config => config.map([
{ name: 'c', route: 'child/:p', moduleId: './child', href: '/child/c' },
{ name: 'd', route: 'child/:p', moduleId: './child', href: '/child/d' },
])))
.then(() => router._createNavigationInstruction('parent/b/child/c?foo=456'))
.then(i => {
expect(i.fragment).toEqual('parent/b/child/c');
expect(i.queryString).toEqual('foo=456');
expect(i.params.p).toEqual('b');
expect(i.config.name).toEqual('b');
})
.then(() => childRouter._createNavigationInstruction('child/c?foo=456'))
.then(i => {
// This failing test needs to be implemented.
// expect(i.fragment).toEqual('child/c');
// expect(i.queryString).toEqual('foo=456');
// expect(i.params.p).toEqual('c');
// expect(i.config.name).toEqual('c');
})
.catch(reason => fail(reason))
.then(done);
});
});
it('should be case insensitive by default', (done) => {
router.configure(config => config.map({ name: 'test', route: 'test/:id', moduleId: './test' }))
.then(() => router._createNavigationInstruction('TeSt/123?foo=456'))
.then(x => expect(x).toEqual(jasmine.objectContaining({ fragment: 'TeSt/123', queryString: 'foo=456' })))
.catch(reason => fail(reason))
.then(done);
});
it('should reject when route is case sensitive', (done) => {
router.configure(config => config.map({ name: 'test', route: 'Test/:id', moduleId: './test', caseSensitive: true }))
.then(() => router._createNavigationInstruction('test/123'))
.then(() => expect(true).toBeFalsy('should have rejected'))
.catch(reason => expect(reason).toBeTruthy())
.then(done);
});
describe('catchAllHandler', () => {
it('should use a parent routers catchAllHandler if one exists', (done) => {
const child = router.createChild(new Container());
child.baseUrl = 'empty';
Promise.all([
router.configure(config => {
config.unknownRouteConfig = 'test';
config.mapRoute({route: 'foo', moduleId: './empty'});
}),
child.configure(config => {
config.mapRoute({ route: '', moduleId: './child-empty' });
})
])
.then(() => router._createNavigationInstruction('foo/bar/123?bar=456'))
.then(parentInstruction => {
expect(parentInstruction.config.moduleId).toEqual('./empty');
return child._createNavigationInstruction('bar/123?bar=456', parentInstruction);
})
.then(childInstruction => expect(childInstruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should use string moduleId handler', (done) => {
router
.configure(config => {
config.unknownRouteConfig = 'test';
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should use route config handler', (done) => {
router
.configure(config => {
config.unknownRouteConfig = { moduleId: 'test' };
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should use function handler', (done) => {
router
.configure(config => {
config.unknownRouteConfig = instruction => ({ moduleId: 'test' });
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should use async function handler', (done) => {
router
.configure(config => {
config.unknownRouteConfig = instruction => Promise.resolve({ moduleId: 'test' });
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should pass instruction to function handler', (done) => {
router
.configure(config => {
config.unknownRouteConfig = instruction => {
expect(instruction.fragment).toBe('foo/123');
expect(instruction.queryString).toBe('bar=456');
expect(instruction.config).toBe(null);
return { moduleId: 'test' };
};
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(instruction => expect(instruction.config.moduleId).toEqual('test'))
.catch(fail)
.then(done);
});
it('should throw on invalid handlers', () => {
expect(() => {
router.handleUnknownRoutes(null);
}).toThrow();
});
it('should reject invalid configs', (done) => {
router
.configure(config => {
config.unknownRouteConfig = instruction => null;
})
.then(() => router._createNavigationInstruction('foo/123?bar=456'))
.then(() => fail('should have rejected'))
.catch(done);
});
});
});
describe('configure', () => {
it('notifies when configured', (done) => {
expect(router.isConfigured).toBe(false);
router.ensureConfigured().then(() => {
expect(router.isConfigured).toBe(true);
done();
});
router.configure(config => config.map({ route: '', moduleId: './test' }));
});
it('notifies when already configured', (done) => {
expect(router.isConfigured).toBe(false);
router.configure(config => config.map({ route: '', moduleId: './test' }))
.then(() => {
expect(router.isConfigured).toBe(true);
router.ensureConfigured().then(() => {
expect(router.isConfigured).toBe(true);
done();
});
});
});
it('waits for async callbacks', (done) => {
let resolve;
let promise = new Promise(r => resolve = r);
expect(router.isConfigured).toBe(false);
router.configure(config => {
return promise.then(x => {
config.map({ route: '', moduleId: './test' });
});
});
expect(router.isConfigured).toBe(true);
expect(router.routes.length).toBe(0);
router.ensureConfigured().then(() => {
expect(router.routes.length).toBe(1);
done();
});
resolve();
});
});
describe('refreshNavigation', () => {
let staticHref;
beforeEach((done) => {
staticHref = '#/a/static/href';
router.baseUrl = 'initial-root';
router.configure(config => config.map([
{ name: 'dynamic', route: 'dynamic', moduleId: 'dynamic', nav: true },
{ name: 'static', route: 'static', moduleId: 'static', href: staticHref, nav: true }])).then(() => {
router.refreshNavigation();
done();
});
});
it('updates a dynamic href ', () => {
router.baseUrl = 'updated-root';
router.refreshNavigation();
expect(router.navigation[0].href).toEqual('#/updated-root/dynamic');
});
it('respects a static href ', () => {
router.baseUrl = 'updated-root';
router.refreshNavigation();
expect(router.navigation[1].href).toEqual(staticHref);
});
});
});