2015-04-17 12:59:56 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
describe,
|
2015-05-29 17:58:41 -04:00
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
ddescribe,
|
|
|
|
expect,
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
|
|
|
SpyObject
|
|
|
|
} from 'angular2/test_lib';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
|
2015-06-30 16:18:51 -04:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
2015-05-21 16:59:14 -04:00
|
|
|
|
2015-05-01 08:53:38 -04:00
|
|
|
import {RouteRegistry} from 'angular2/src/router/route_registry';
|
2015-05-29 17:58:41 -04:00
|
|
|
import {RouteConfig} from 'angular2/src/router/route_config_decorator';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('RouteRegistry', () => {
|
2015-05-29 17:58:41 -04:00
|
|
|
var registry, rootHostComponent = new Object();
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-07-06 20:41:15 -04:00
|
|
|
beforeEach(() => { registry = new RouteRegistry(); });
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should match the full URL', inject([AsyncTestCompleter], (async) => {
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/test', 'component': DummyCompB});
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.recognize('/test', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
it('should generate URLs starting at the given component', () => {
|
|
|
|
registry.config(rootHostComponent,
|
|
|
|
{'path': '/first/...', 'component': DummyParentComp, 'as': 'firstCmp'});
|
|
|
|
|
2015-07-06 20:41:15 -04:00
|
|
|
expect(registry.generate(['firstCmp', 'secondCmp'], rootHostComponent))
|
|
|
|
.toEqual('first/second');
|
|
|
|
expect(registry.generate(['secondCmp'], DummyParentComp)).toEqual('second');
|
2015-06-30 16:18:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate URLs with params', () => {
|
|
|
|
registry.config(
|
|
|
|
rootHostComponent,
|
|
|
|
{'path': '/first/:param/...', 'component': DummyParentParamComp, 'as': 'firstCmp'});
|
|
|
|
|
2015-07-06 20:41:15 -04:00
|
|
|
var url = registry.generate(['firstCmp', {param: 'one'}, 'secondCmp', {param: 'two'}],
|
2015-06-30 16:18:51 -04:00
|
|
|
rootHostComponent);
|
2015-07-06 20:41:15 -04:00
|
|
|
expect(url).toEqual('first/one/second/two');
|
2015-06-30 16:18:51 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate URLs of loaded components after they are loaded',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
registry.config(rootHostComponent, {
|
|
|
|
'path': '/first/...',
|
|
|
|
'component': {'type': 'loader', 'loader': AsyncParentLoader},
|
|
|
|
'as': 'firstCmp'
|
|
|
|
});
|
|
|
|
|
2015-07-06 20:41:15 -04:00
|
|
|
expect(() => registry.generate(['firstCmp', 'secondCmp'], rootHostComponent))
|
2015-06-30 16:18:51 -04:00
|
|
|
.toThrowError('Could not find route config for "secondCmp".');
|
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent)
|
|
|
|
.then((_) => {
|
2015-07-06 20:41:15 -04:00
|
|
|
expect(registry.generate(['firstCmp', 'secondCmp'], rootHostComponent))
|
|
|
|
.toEqual('first/second');
|
2015-06-30 16:18:51 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer static segments to dynamic', inject([AsyncTestCompleter], (async) => {
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompB});
|
|
|
|
registry.config(rootHostComponent, {'path': '/home', 'component': DummyCompA});
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.recognize('/home', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompA);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer dynamic segments to star', inject([AsyncTestCompleter], (async) => {
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/*site', 'component': DummyCompB});
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.recognize('/home', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompA);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer routes with more dynamic segments', inject([AsyncTestCompleter], (async) => {
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:first/*rest', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/*all', 'component': DummyCompB});
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.recognize('/some/path', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompA);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer routes with more static segments', inject([AsyncTestCompleter], (async) => {
|
2015-06-12 10:50:45 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first/:second', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/:first/:second', 'component': DummyCompB});
|
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompA);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should prefer routes with static segments before dynamic segments',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
registry.config(rootHostComponent,
|
|
|
|
{'path': '/first/second/:third', 'component': DummyCompB});
|
|
|
|
registry.config(rootHostComponent,
|
|
|
|
{'path': '/first/:second/third', 'component': DummyCompA});
|
|
|
|
|
|
|
|
registry.recognize('/first/second/third', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should match the full URL using child components', inject([AsyncTestCompleter], (async) => {
|
2015-06-17 14:57:38 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first/...', 'component': DummyParentComp});
|
2015-06-12 10:50:45 -04:00
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyParentComp);
|
|
|
|
expect(instruction.child.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should match the URL using async child components',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-06-17 14:57:38 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first/...', 'component': DummyAsyncComp});
|
2015-06-12 10:50:45 -04:00
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyAsyncComp);
|
|
|
|
expect(instruction.child.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should match the URL using an async parent component',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
registry.config(
|
|
|
|
rootHostComponent,
|
2015-06-17 14:57:38 -04:00
|
|
|
{'path': '/first/...', 'component': {'loader': AsyncParentLoader, 'type': 'loader'}});
|
2015-06-12 10:50:45 -04:00
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyParentComp);
|
|
|
|
expect(instruction.child.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-21 16:59:14 -04:00
|
|
|
|
|
|
|
it('should throw when a config does not have a component or redirectTo property', () => {
|
2015-06-12 10:50:45 -04:00
|
|
|
expect(() => registry.config(rootHostComponent, {'path': '/some/path'}))
|
|
|
|
.toThrowError(
|
|
|
|
'Route config should contain exactly one \'component\', or \'redirectTo\' property');
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should throw when a config has an invalid component type', () => {
|
2015-06-12 10:50:45 -04:00
|
|
|
expect(() => registry.config(
|
|
|
|
rootHostComponent,
|
|
|
|
{'path': '/some/path', 'component': {'type': 'intentionallyWrongComponentType'}}))
|
|
|
|
.toThrowError('Invalid component type \'intentionallyWrongComponentType\'');
|
2015-05-21 16:59:14 -04:00
|
|
|
});
|
2015-06-17 14:57:38 -04:00
|
|
|
|
|
|
|
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
|
|
|
|
() => {
|
|
|
|
expect(() =>
|
|
|
|
registry.config(rootHostComponent, {'path': '/', 'component': DummyParentComp}))
|
|
|
|
.toThrowError(
|
|
|
|
'Child routes are not allowed for "/". Use "..." on the parent\'s route path.');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
|
|
|
|
() => {
|
|
|
|
expect(() => registry.config(rootHostComponent,
|
|
|
|
{'path': '/home/.../fun/', 'component': DummyParentComp}))
|
|
|
|
.toThrowError('Unexpected "..." before the end of the path for "home/.../fun/".');
|
|
|
|
});
|
2015-04-17 12:59:56 -04:00
|
|
|
});
|
|
|
|
}
|
2015-04-29 18:47:12 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
function AsyncParentLoader() {
|
|
|
|
return PromiseWrapper.resolve(DummyParentComp);
|
|
|
|
}
|
|
|
|
|
|
|
|
function AsyncChildLoader() {
|
|
|
|
return PromiseWrapper.resolve(DummyCompB);
|
|
|
|
}
|
|
|
|
|
2015-06-12 10:50:45 -04:00
|
|
|
@RouteConfig([{'path': '/second', 'component': {'loader': AsyncChildLoader, 'type': 'loader'}}])
|
|
|
|
class DummyAsyncComp {
|
|
|
|
}
|
2015-05-21 16:59:14 -04:00
|
|
|
|
2015-04-29 18:47:12 -04:00
|
|
|
class DummyCompA {}
|
|
|
|
class DummyCompB {}
|
2015-05-29 17:58:41 -04:00
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
@RouteConfig([{'path': '/second', 'component': DummyCompB, 'as': 'secondCmp'}])
|
2015-05-29 17:58:41 -04:00
|
|
|
class DummyParentComp {
|
|
|
|
}
|
2015-06-30 16:18:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
@RouteConfig([{'path': '/second/:param', 'component': DummyCompB, 'as': 'secondCmp'}])
|
|
|
|
class DummyParentParamComp {
|
|
|
|
}
|