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-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-05-29 17:58:41 -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-04-29 18:47:12 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/test', 'component': DummyCompB});
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
registry.recognize('/test', rootHostComponent).then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
it('should prefer static segments to dynamic', inject([AsyncTestCompleter], (async) => {
|
2015-05-12 17:53:13 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompB});
|
|
|
|
registry.config(rootHostComponent, {'path': '/home', 'component': DummyCompA});
|
|
|
|
|
2015-05-21 16:59:14 -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-05-12 17:53:13 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:site', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/*site', 'component': DummyCompB});
|
|
|
|
|
2015-05-21 16:59:14 -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-05-15 05:05:57 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/:first/*rest', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/*all', 'component': DummyCompB});
|
|
|
|
|
2015-05-21 16:59:14 -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-05-15 05:05:57 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first/:second', 'component': DummyCompA});
|
|
|
|
registry.config(rootHostComponent, {'path': '/:first/:second', 'component': DummyCompB});
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
registry.recognize('/first/second', 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 static segments before dynamic segments', inject([AsyncTestCompleter], (async) => {
|
2015-05-15 05:05:57 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first/second/:third', 'component': DummyCompB});
|
|
|
|
registry.config(rootHostComponent, {'path': '/first/:second/third', 'component': DummyCompA});
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
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-04-29 18:47:12 -04:00
|
|
|
registry.config(rootHostComponent, {'path': '/first', 'component': DummyParentComp});
|
2015-04-17 12:59:56 -04:00
|
|
|
|
2015-05-21 16:59:14 -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) => {
|
|
|
|
registry.config(rootHostComponent, {'path': '/first', 'component': DummyAsyncComp});
|
|
|
|
|
|
|
|
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, {'path': '/first', 'component': {'loader': AsyncParentLoader, 'type': 'loader'} });
|
|
|
|
|
|
|
|
registry.recognize('/first/second', rootHostComponent).then((instruction) => {
|
|
|
|
expect(instruction.component).toBe(DummyParentComp);
|
|
|
|
expect(instruction.child.component).toBe(DummyCompB);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
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');
|
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', () => {
|
|
|
|
expect(() => registry.config(rootHostComponent, {'path': '/some/path', 'component': { 'type': 'intentionallyWrongComponentType' } }))
|
|
|
|
.toThrowError('Invalid component type \'intentionallyWrongComponentType\'');
|
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RouteConfig([
|
|
|
|
{ 'path': '/second', 'component': { 'loader': AsyncChildLoader, 'type': 'loader' } }
|
|
|
|
])
|
|
|
|
class DummyAsyncComp {}
|
|
|
|
|
2015-04-29 18:47:12 -04:00
|
|
|
class DummyCompA {}
|
|
|
|
class DummyCompB {}
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
@RouteConfig([{'path': '/second', 'component': DummyCompB}])
|
|
|
|
class DummyParentComp {
|
|
|
|
}
|