feat(router): add interfaces for route definitions in RouteConfig

Closes #2261
This commit is contained in:
Brian Ford
2015-07-13 16:12:48 -07:00
parent 61c73576c8
commit 4d28167bc0
16 changed files with 457 additions and 227 deletions

View File

@ -13,7 +13,7 @@ import {
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
import {RouteRegistry} from 'angular2/src/router/route_registry';
import {RouteConfig} from 'angular2/src/router/route_config_decorator';
import {RouteConfig, Route, AsyncRoute} from 'angular2/src/router/route_config_decorator';
export function main() {
describe('RouteRegistry', () => {
@ -22,8 +22,8 @@ export function main() {
beforeEach(() => { registry = new RouteRegistry(); });
it('should match the full URL', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/', 'component': DummyCmpA});
registry.config(RootHostCmp, {'path': '/test', 'component': DummyCmpB});
registry.config(RootHostCmp, new Route({path: '/', component: DummyCmpA}));
registry.config(RootHostCmp, new Route({path: '/test', component: DummyCmpB}));
registry.recognize('/test', RootHostCmp)
.then((instruction) => {
@ -34,7 +34,7 @@ export function main() {
it('should generate URLs starting at the given component', () => {
registry.config(RootHostCmp,
{'path': '/first/...', 'component': DummyParentCmp, 'as': 'firstCmp'});
new Route({path: '/first/...', component: DummyParentCmp, as: 'firstCmp'}));
expect(registry.generate(['firstCmp', 'secondCmp'], RootHostCmp)).toEqual('first/second');
expect(registry.generate(['secondCmp'], DummyParentCmp)).toEqual('second');
@ -43,7 +43,7 @@ export function main() {
it('should generate URLs with params', () => {
registry.config(
RootHostCmp,
{'path': '/first/:param/...', 'component': DummyParentParamCmp, 'as': 'firstCmp'});
new Route({path: '/first/:param/...', component: DummyParentParamCmp, as: 'firstCmp'}));
var url =
registry.generate(['firstCmp', {param: 'one'}, 'secondCmp', {param: 'two'}], RootHostCmp);
@ -52,11 +52,9 @@ export function main() {
it('should generate URLs of loaded components after they are loaded',
inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {
'path': '/first/...',
'component': {'type': 'loader', 'loader': AsyncParentLoader},
'as': 'firstCmp'
});
registry.config(
RootHostCmp,
new AsyncRoute({path: '/first/...', loader: AsyncParentLoader, as: 'firstCmp'}));
expect(() => registry.generate(['firstCmp', 'secondCmp'], RootHostCmp))
.toThrowError('Could not find route named "secondCmp".');
@ -77,8 +75,8 @@ export function main() {
it('should prefer static segments to dynamic', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/:site', 'component': DummyCmpB});
registry.config(RootHostCmp, {'path': '/home', 'component': DummyCmpA});
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpB}));
registry.config(RootHostCmp, new Route({path: '/home', component: DummyCmpA}));
registry.recognize('/home', RootHostCmp)
.then((instruction) => {
@ -88,8 +86,8 @@ export function main() {
}));
it('should prefer dynamic segments to star', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/:site', 'component': DummyCmpA});
registry.config(RootHostCmp, {'path': '/*site', 'component': DummyCmpB});
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpA}));
registry.config(RootHostCmp, new Route({path: '/*site', component: DummyCmpB}));
registry.recognize('/home', RootHostCmp)
.then((instruction) => {
@ -99,8 +97,8 @@ export function main() {
}));
it('should prefer routes with more dynamic segments', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/:first/*rest', 'component': DummyCmpA});
registry.config(RootHostCmp, {'path': '/*all', 'component': DummyCmpB});
registry.config(RootHostCmp, new Route({path: '/:first/*rest', component: DummyCmpA}));
registry.config(RootHostCmp, new Route({path: '/*all', component: DummyCmpB}));
registry.recognize('/some/path', RootHostCmp)
.then((instruction) => {
@ -110,8 +108,8 @@ export function main() {
}));
it('should prefer routes with more static segments', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/first/:second', 'component': DummyCmpA});
registry.config(RootHostCmp, {'path': '/:first/:second', 'component': DummyCmpB});
registry.config(RootHostCmp, new Route({path: '/first/:second', component: DummyCmpA}));
registry.config(RootHostCmp, new Route({path: '/:first/:second', component: DummyCmpB}));
registry.recognize('/first/second', RootHostCmp)
.then((instruction) => {
@ -122,8 +120,10 @@ export function main() {
it('should prefer routes with static segments before dynamic segments',
inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/first/second/:third', 'component': DummyCmpB});
registry.config(RootHostCmp, {'path': '/first/:second/third', 'component': DummyCmpA});
registry.config(RootHostCmp,
new Route({path: '/first/second/:third', component: DummyCmpB}));
registry.config(RootHostCmp,
new Route({path: '/first/:second/third', component: DummyCmpA}));
registry.recognize('/first/second/third', RootHostCmp)
.then((instruction) => {
@ -133,7 +133,7 @@ export function main() {
}));
it('should match the full URL using child components', inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/first/...', 'component': DummyParentCmp});
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyParentCmp}));
registry.recognize('/first/second', RootHostCmp)
.then((instruction) => {
@ -145,7 +145,7 @@ export function main() {
it('should match the URL using async child components',
inject([AsyncTestCompleter], (async) => {
registry.config(RootHostCmp, {'path': '/first/...', 'component': DummyAsyncCmp});
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyAsyncCmp}));
registry.recognize('/first/second', RootHostCmp)
.then((instruction) => {
@ -157,9 +157,8 @@ export function main() {
it('should match the URL using an async parent component',
inject([AsyncTestCompleter], (async) => {
registry.config(
RootHostCmp,
{'path': '/first/...', 'component': {'loader': AsyncParentLoader, 'type': 'loader'}});
registry.config(RootHostCmp,
new AsyncRoute({path: '/first/...', loader: AsyncParentLoader}));
registry.recognize('/first/second', RootHostCmp)
.then((instruction) => {
@ -169,31 +168,36 @@ export function main() {
});
}));
it('should throw when a config does not have a component or redirectTo property', () => {
expect(() => registry.config(RootHostCmp, {'path': '/some/path'}))
.toThrowError(
'Route config should contain exactly one \'component\', or \'redirectTo\' property');
});
it('should throw when a config has an invalid component type', () => {
expect(() => registry.config(
RootHostCmp,
{'path': '/some/path', 'component': {'type': 'intentionallyWrongComponentType'}}))
.toThrowError('Invalid component type \'intentionallyWrongComponentType\'');
});
// TODO: not sure what to do with these tests
// it('should throw when a config does not have a component or redirectTo property', () => {
// expect(() => registry.config(rootHostComponent, {'path': '/some/path'}))
// .toThrowError(
// 'Route config should contain exactly one \'component\', or \'redirectTo\'
// property');
//});
//
// it('should throw when a config has an invalid component type', () => {
// expect(() => registry.config(
// rootHostComponent,
// {'path': '/some/path', 'component': {'type':
// 'intentionallyWrongComponentType'}}))
// .toThrowError('Invalid component type \'intentionallyWrongComponentType\'');
//});
it('should throw when a parent config is missing the `...` suffix any of its children add routes',
() => {
expect(() => registry.config(RootHostCmp, {'path': '/', 'component': DummyParentCmp}))
expect(() =>
registry.config(RootHostCmp, new Route({path: '/', component: DummyParentCmp})))
.toThrowError(
'Child routes are not allowed for "/". Use "..." on the parent\'s route path.');
});
it('should throw when a parent config uses `...` suffix before the end of the route', () => {
expect(() => registry.config(RootHostCmp,
{'path': '/home/.../fun/', 'component': DummyParentCmp}))
new Route({path: '/home/.../fun/', component: DummyParentCmp})))
.toThrowError('Unexpected "..." before the end of the path for "home/.../fun/".');
});
});
}
@ -207,18 +211,18 @@ function AsyncChildLoader() {
class RootHostCmp {}
@RouteConfig([{'path': '/second', 'component': {'loader': AsyncChildLoader, 'type': 'loader'}}])
@RouteConfig([new AsyncRoute({path: '/second', loader: AsyncChildLoader})])
class DummyAsyncCmp {
}
class DummyCmpA {}
class DummyCmpB {}
@RouteConfig([{'path': '/second', 'component': DummyCmpB, 'as': 'secondCmp'}])
@RouteConfig([new Route({path: '/second', component: DummyCmpB, as: 'secondCmp'})])
class DummyParentCmp {
}
@RouteConfig([{'path': '/second/:param', 'component': DummyCmpB, 'as': 'secondCmp'}])
@RouteConfig([new Route({path: '/second/:param', component: DummyCmpB, as: 'secondCmp'})])
class DummyParentParamCmp {
}