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,
|
2015-08-11 17:00:54 -04:00
|
|
|
SpyObject
|
2015-05-29 17:58:41 -04:00
|
|
|
} from 'angular2/test_lib';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-08-20 17:28:25 -04:00
|
|
|
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
|
|
|
|
import {Type} from 'angular2/src/core/facade/lang';
|
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-08-13 14:09:22 -04:00
|
|
|
import {
|
|
|
|
RouteConfig,
|
|
|
|
Route,
|
|
|
|
Redirect,
|
|
|
|
AuxRoute,
|
|
|
|
AsyncRoute
|
|
|
|
} from 'angular2/src/router/route_config_decorator';
|
2015-07-17 16:36:53 -04:00
|
|
|
import {stringifyInstruction} from 'angular2/src/router/instruction';
|
2015-08-11 17:00:54 -04:00
|
|
|
import {IS_DART} from '../platform';
|
2015-04-17 12:59:56 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('RouteRegistry', () => {
|
2015-07-13 18:29:14 -04:00
|
|
|
var registry;
|
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-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/', component: DummyCmpA}));
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/test', component: DummyCmpB}));
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/test', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpB);
|
2015-06-12 10:50:45 -04:00
|
|
|
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', () => {
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.config(RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/...', component: DummyParentCmp, as: 'FirstCmp'}));
|
2015-06-30 16:18:51 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(stringifyInstruction(registry.generate(['FirstCmp', 'SecondCmp'], RootHostCmp)))
|
2015-07-17 16:36:53 -04:00
|
|
|
.toEqual('first/second');
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(stringifyInstruction(registry.generate(['SecondCmp'], DummyParentCmp)))
|
2015-07-17 16:36:53 -04:00
|
|
|
.toEqual('second');
|
2015-06-30 16:18:51 -04:00
|
|
|
});
|
|
|
|
|
2015-08-13 14:09:22 -04:00
|
|
|
it('should generate URLs that account for redirects', () => {
|
|
|
|
registry.config(
|
|
|
|
RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/...', component: DummyParentRedirectCmp, as: 'FirstCmp'}));
|
2015-08-13 14:09:22 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(stringifyInstruction(registry.generate(['FirstCmp'], RootHostCmp)))
|
2015-08-13 14:09:22 -04:00
|
|
|
.toEqual('first/second');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should generate URLs in a hierarchy of redirects', () => {
|
|
|
|
registry.config(
|
|
|
|
RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/...', component: DummyMultipleRedirectCmp, as: 'FirstCmp'}));
|
2015-08-13 14:09:22 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(stringifyInstruction(registry.generate(['FirstCmp'], RootHostCmp)))
|
2015-08-13 14:09:22 -04:00
|
|
|
.toEqual('first/second/third');
|
|
|
|
});
|
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
it('should generate URLs with params', () => {
|
|
|
|
registry.config(
|
2015-07-13 18:29:14 -04:00
|
|
|
RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/:param/...', component: DummyParentParamCmp, as: 'FirstCmp'}));
|
2015-06-30 16:18:51 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var url = stringifyInstruction(registry.generate(
|
2015-09-14 20:36:32 -04:00
|
|
|
['FirstCmp', {param: 'one'}, 'SecondCmp', {param: 'two'}], RootHostCmp));
|
2015-07-06 20:41:15 -04:00
|
|
|
expect(url).toEqual('first/one/second/two');
|
2015-06-30 16:18:51 -04:00
|
|
|
});
|
|
|
|
|
2015-09-09 10:49:02 -04:00
|
|
|
it('should generate params as an empty StringMap when no params are given', () => {
|
2015-09-14 20:36:32 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/test', component: DummyCmpA, as: 'Test'}));
|
|
|
|
var instruction = registry.generate(['Test'], RootHostCmp);
|
2015-08-25 11:59:57 -04:00
|
|
|
expect(instruction.component.params).toEqual({});
|
|
|
|
});
|
|
|
|
|
2015-06-30 16:18:51 -04:00
|
|
|
it('should generate URLs of loaded components after they are loaded',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-07-13 19:12:48 -04:00
|
|
|
registry.config(
|
|
|
|
RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new AsyncRoute({path: '/first/...', loader: AsyncParentLoader, as: 'FirstCmp'}));
|
2015-06-30 16:18:51 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(() => registry.generate(['FirstCmp', 'SecondCmp'], RootHostCmp))
|
|
|
|
.toThrowError('Could not find route named "SecondCmp".');
|
2015-06-30 16:18:51 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second', RootHostCmp)
|
2015-06-30 16:18:51 -04:00
|
|
|
.then((_) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(
|
2015-09-14 20:36:32 -04:00
|
|
|
stringifyInstruction(registry.generate(['FirstCmp', 'SecondCmp'], RootHostCmp)))
|
2015-07-06 20:41:15 -04:00
|
|
|
.toEqual('first/second');
|
2015-06-30 16:18:51 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
|
|
|
|
it('should throw when generating a url and a parent has no config', () => {
|
2015-09-14 20:36:32 -04:00
|
|
|
expect(() => registry.generate(['FirstCmp', 'SecondCmp'], RootHostCmp))
|
2015-07-13 18:29:14 -04:00
|
|
|
.toThrowError('Component "RootHostCmp" has no route config.');
|
|
|
|
});
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer static segments to dynamic', inject([AsyncTestCompleter], (async) => {
|
2015-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpB}));
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/home', component: DummyCmpA}));
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/home', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpA);
|
2015-06-12 10:50:45 -04:00
|
|
|
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-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/:site', component: DummyCmpA}));
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/*site', component: DummyCmpB}));
|
2015-05-12 17:53:13 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/home', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpA);
|
2015-06-12 10:50:45 -04:00
|
|
|
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-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/:first/*rest', component: DummyCmpA}));
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/*all', component: DummyCmpB}));
|
2015-05-15 05:05:57 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/some/path', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpA);
|
2015-06-12 10:50:45 -04:00
|
|
|
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-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/first/:second', component: DummyCmpA}));
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/:first/:second', component: DummyCmpB}));
|
2015-06-12 10:50:45 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpA);
|
2015-06-12 10:50:45 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should prefer routes with static segments before dynamic segments',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp,
|
|
|
|
new Route({path: '/first/second/:third', component: DummyCmpB}));
|
|
|
|
registry.config(RootHostCmp,
|
|
|
|
new Route({path: '/first/:second/third', component: DummyCmpA}));
|
2015-06-12 10:50:45 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second/third', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyCmpB);
|
2015-06-12 10:50:45 -04:00
|
|
|
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-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyParentCmp}));
|
2015-06-12 10:50:45 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
|
|
|
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
2015-06-12 10:50:45 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should match the URL using async child components',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyAsyncCmp}));
|
2015-06-12 10:50:45 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyAsyncCmp);
|
|
|
|
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
2015-06-12 10:50:45 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should match the URL using an async parent component',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-07-13 19:12:48 -04:00
|
|
|
registry.config(RootHostCmp,
|
|
|
|
new AsyncRoute({path: '/first/...', loader: AsyncParentLoader}));
|
2015-06-12 10:50:45 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
registry.recognize('/first/second', RootHostCmp)
|
2015-06-12 10:50:45 -04:00
|
|
|
.then((instruction) => {
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
|
|
|
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
2015-06-12 10:50:45 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
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',
|
|
|
|
() => {
|
2015-07-13 19:12:48 -04:00
|
|
|
expect(() =>
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/', component: DummyParentCmp})))
|
2015-06-17 14:57:38 -04:00
|
|
|
.toThrowError(
|
|
|
|
'Child routes are not allowed for "/". Use "..." on the parent\'s route path.');
|
|
|
|
});
|
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
it('should throw when a parent config uses `...` suffix before the end of the route', () => {
|
|
|
|
expect(() => registry.config(RootHostCmp,
|
2015-07-13 19:12:48 -04:00
|
|
|
new Route({path: '/home/.../fun/', component: DummyParentCmp})))
|
2015-07-13 18:29:14 -04:00
|
|
|
.toThrowError('Unexpected "..." before the end of the path for "home/.../fun/".');
|
|
|
|
});
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2015-08-10 16:05:08 -04:00
|
|
|
|
|
|
|
it('should throw if a config has a component that is not defined', () => {
|
|
|
|
expect(() => registry.config(RootHostCmp, new Route({path: '/', component: null})))
|
|
|
|
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
|
|
|
expect(() => registry.config(RootHostCmp, new AuxRoute({path: '/', component: null})))
|
|
|
|
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
|
|
|
|
|
|
|
// This would never happen in Dart
|
2015-08-11 17:00:54 -04:00
|
|
|
if (!IS_DART) {
|
2015-08-10 16:05:08 -04:00
|
|
|
expect(() => registry.config(RootHostCmp, new Route({path: '/', component:<Type>(<any>4)})))
|
|
|
|
.toThrowError('Component for route "/" is not defined, or is not a class.');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-09 10:49:02 -04:00
|
|
|
it('should throw when linkParams are not terminal', () => {
|
|
|
|
registry.config(RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/...', component: DummyParentCmp, as: 'First'}));
|
|
|
|
expect(() => { registry.generate(['First'], RootHostCmp); })
|
|
|
|
.toThrowError('Link "["First"]" does not resolve to a terminal or async instruction.');
|
2015-09-09 10:49:02 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should match matrix params on child components and query params on the root component',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
registry.config(RootHostCmp, new Route({path: '/first/...', component: DummyParentCmp}));
|
|
|
|
|
|
|
|
registry.recognize('/first/second;filter=odd?comments=all', RootHostCmp)
|
|
|
|
.then((instruction) => {
|
|
|
|
expect(instruction.component.componentType).toBe(DummyParentCmp);
|
|
|
|
expect(instruction.component.params).toEqual({'comments': 'all'});
|
|
|
|
|
|
|
|
expect(instruction.child.component.componentType).toBe(DummyCmpB);
|
|
|
|
expect(instruction.child.component.params).toEqual({'filter': 'odd'});
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should generate URLs with matrix and query params', () => {
|
|
|
|
registry.config(
|
|
|
|
RootHostCmp,
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/first/:param/...', component: DummyParentParamCmp, as: 'FirstCmp'}));
|
2015-07-17 16:36:53 -04:00
|
|
|
|
|
|
|
var url = stringifyInstruction(registry.generate(
|
|
|
|
[
|
2015-09-14 20:36:32 -04:00
|
|
|
'FirstCmp',
|
2015-07-17 16:36:53 -04:00
|
|
|
{param: 'one', query: 'cats'},
|
2015-09-14 20:36:32 -04:00
|
|
|
'SecondCmp',
|
2015-07-17 16:36:53 -04:00
|
|
|
{
|
|
|
|
param: 'two',
|
|
|
|
sort: 'asc',
|
|
|
|
}
|
|
|
|
],
|
|
|
|
RootHostCmp));
|
|
|
|
expect(url).toEqual('first/one/second/two;sort=asc?query=cats');
|
|
|
|
});
|
|
|
|
|
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() {
|
2015-07-13 18:29:14 -04:00
|
|
|
return PromiseWrapper.resolve(DummyParentCmp);
|
2015-05-21 16:59:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function AsyncChildLoader() {
|
2015-07-13 18:29:14 -04:00
|
|
|
return PromiseWrapper.resolve(DummyCmpB);
|
2015-05-21 16:59:14 -04:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
class RootHostCmp {}
|
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([new AsyncRoute({path: '/second', loader: AsyncChildLoader})])
|
2015-07-13 18:29:14 -04:00
|
|
|
class DummyAsyncCmp {
|
2015-06-12 10:50:45 -04:00
|
|
|
}
|
2015-05-21 16:59:14 -04:00
|
|
|
|
2015-07-13 18:29:14 -04:00
|
|
|
class DummyCmpA {}
|
|
|
|
class DummyCmpB {}
|
2015-05-29 17:58:41 -04:00
|
|
|
|
2015-08-13 14:09:22 -04:00
|
|
|
@RouteConfig([
|
|
|
|
new Redirect({path: '/', redirectTo: '/third'}),
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/third', component: DummyCmpB, as: 'ThirdCmp'})
|
2015-08-13 14:09:22 -04:00
|
|
|
])
|
|
|
|
class DummyRedirectCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@RouteConfig([
|
|
|
|
new Redirect({path: '/', redirectTo: '/second'}),
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/second/...', component: DummyRedirectCmp, as: 'SecondCmp'})
|
2015-08-13 14:09:22 -04:00
|
|
|
])
|
|
|
|
class DummyMultipleRedirectCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
@RouteConfig([
|
|
|
|
new Redirect({path: '/', redirectTo: '/second'}),
|
2015-09-14 20:36:32 -04:00
|
|
|
new Route({path: '/second', component: DummyCmpB, as: 'SecondCmp'})
|
2015-08-13 14:09:22 -04:00
|
|
|
])
|
|
|
|
class DummyParentRedirectCmp {
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
@RouteConfig([new Route({path: '/second', component: DummyCmpB, as: 'SecondCmp'})])
|
2015-07-13 18:29:14 -04:00
|
|
|
class DummyParentCmp {
|
2015-05-29 17:58:41 -04:00
|
|
|
}
|
2015-06-30 16:18:51 -04:00
|
|
|
|
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
@RouteConfig([new Route({path: '/second/:param', component: DummyCmpB, as: 'SecondCmp'})])
|
2015-07-13 18:29:14 -04:00
|
|
|
class DummyParentParamCmp {
|
2015-06-30 16:18:51 -04:00
|
|
|
}
|