2015-05-29 17:58:41 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
describe,
|
|
|
|
proxy,
|
|
|
|
it,
|
|
|
|
iit,
|
|
|
|
ddescribe,
|
|
|
|
expect,
|
|
|
|
inject,
|
|
|
|
beforeEach,
|
2015-08-26 14:41:41 -04:00
|
|
|
beforeEachBindings
|
2015-05-29 17:58:41 -04:00
|
|
|
} from 'angular2/test_lib';
|
2015-08-26 14:41:41 -04:00
|
|
|
import {SpyRouterOutlet} from './spies';
|
|
|
|
import {Type} from 'angular2/src/core/facade/lang';
|
2015-08-20 17:28:25 -04:00
|
|
|
import {Promise, PromiseWrapper, ObservableWrapper} from 'angular2/src/core/facade/async';
|
|
|
|
import {ListWrapper} from 'angular2/src/core/facade/collection';
|
2015-08-14 15:31:56 -04:00
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
import {Router, RootRouter} from 'angular2/src/router/router';
|
|
|
|
import {Pipeline} from 'angular2/src/router/pipeline';
|
|
|
|
import {SpyLocation} from 'angular2/src/mock/location_mock';
|
|
|
|
import {Location} from 'angular2/src/router/location';
|
2015-07-17 16:36:53 -04:00
|
|
|
import {stringifyInstruction} from 'angular2/src/router/instruction';
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
import {RouteRegistry} from 'angular2/src/router/route_registry';
|
2015-08-14 15:31:56 -04:00
|
|
|
import {RouteConfig, AsyncRoute, Route} from 'angular2/src/router/route_config_decorator';
|
2015-05-29 17:58:41 -04:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
|
|
|
|
|
2015-09-04 01:01:36 -04:00
|
|
|
import {bind} from 'angular2/core';
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Router', () => {
|
|
|
|
var router, location;
|
|
|
|
|
|
|
|
beforeEachBindings(() => [
|
|
|
|
Pipeline,
|
2015-07-06 20:41:15 -04:00
|
|
|
RouteRegistry,
|
2015-05-29 17:58:41 -04:00
|
|
|
DirectiveResolver,
|
|
|
|
bind(Location).toClass(SpyLocation),
|
2015-06-03 16:42:57 -04:00
|
|
|
bind(Router)
|
|
|
|
.toFactory((registry, pipeline,
|
|
|
|
location) => { return new RootRouter(registry, pipeline, location, AppCmp); },
|
|
|
|
[RouteRegistry, Pipeline, Location])
|
2015-05-29 17:58:41 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
beforeEach(inject([Router, Location], (rtr, loc) => {
|
|
|
|
router = rtr;
|
|
|
|
location = loc;
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should navigate based on the initial URL state', inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
router.config([new Route({path: '/', component: DummyComponent})])
|
2015-08-31 00:25:46 -04:00
|
|
|
.then((_) => router.registerPrimaryOutlet(outlet))
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(location.urlChanges).toEqual([]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should activate viewports and update URL on navigate',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
2015-08-31 00:25:46 -04:00
|
|
|
router.registerPrimaryOutlet(outlet)
|
2015-07-13 19:12:48 -04:00
|
|
|
.then((_) => router.config([new Route({path: '/a', component: DummyComponent})]))
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => router.navigate('/a'))
|
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
2015-05-29 17:58:41 -04:00
|
|
|
expect(location.urlChanges).toEqual(['/a']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-07-28 01:46:09 -04:00
|
|
|
it('should not push a history change on when navigate is called with skipUrlChange',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
2015-08-31 00:25:46 -04:00
|
|
|
router.registerPrimaryOutlet(outlet)
|
2015-07-28 01:46:09 -04:00
|
|
|
.then((_) => router.config([new Route({path: '/b', component: DummyComponent})]))
|
|
|
|
.then((_) => router.navigate('/b', true))
|
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
2015-07-28 01:46:09 -04:00
|
|
|
expect(location.urlChanges).toEqual([]);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-07-06 20:41:15 -04:00
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
it('should navigate after being configured', inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
2015-08-31 00:25:46 -04:00
|
|
|
router.registerPrimaryOutlet(outlet)
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => router.navigate('/a'))
|
2015-06-03 16:42:57 -04:00
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).not.toHaveBeenCalled();
|
2015-07-13 19:12:48 -04:00
|
|
|
return router.config([new Route({path: '/a', component: DummyComponent})]);
|
2015-06-03 16:42:57 -04:00
|
|
|
})
|
2015-05-29 17:58:41 -04:00
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
2015-05-29 17:58:41 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-07-06 20:41:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should throw when linkParams does not start with a "/" or "./"', () => {
|
|
|
|
expect(() => router.generate(['firstCmp', 'secondCmp']))
|
|
|
|
.toThrowError(
|
|
|
|
`Link "${ListWrapper.toJSON(['firstCmp', 'secondCmp'])}" must start with "/", "./", or "../"`);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should throw when linkParams does not include a route name', () => {
|
|
|
|
expect(() => router.generate(['./']))
|
|
|
|
.toThrowError(`Link "${ListWrapper.toJSON(['./'])}" must include a route name.`);
|
|
|
|
expect(() => router.generate(['/']))
|
|
|
|
.toThrowError(`Link "${ListWrapper.toJSON(['/'])}" must include a route name.`);
|
|
|
|
});
|
|
|
|
|
2015-08-18 14:30:32 -04:00
|
|
|
it('should, when subscribed to, return a disposable subscription', () => {
|
|
|
|
expect(() => {
|
|
|
|
var subscription = router.subscribe((_) => {});
|
|
|
|
ObservableWrapper.dispose(subscription);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2015-07-06 20:41:15 -04:00
|
|
|
|
|
|
|
it('should generate URLs from the root component when the path starts with /', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
router.config([new Route({path: '/first/...', component: DummyParentComp, as: 'firstCmp'})]);
|
2015-07-06 20:41:15 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction = router.generate(['/firstCmp', 'secondCmp']);
|
|
|
|
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
|
|
|
|
|
|
|
instruction = router.generate(['/firstCmp/secondCmp']);
|
|
|
|
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
2015-07-06 20:41:15 -04:00
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-08-14 15:31:56 -04:00
|
|
|
it('should generate an instruction with terminal async routes',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
2015-08-31 00:25:46 -04:00
|
|
|
router.registerPrimaryOutlet(outlet);
|
2015-08-14 15:31:56 -04:00
|
|
|
router.config([new AsyncRoute({path: '/first', loader: loader, as: 'FirstCmp'})]);
|
|
|
|
|
|
|
|
var instruction = router.generate(['/FirstCmp']);
|
|
|
|
router.navigateInstruction(instruction)
|
|
|
|
.then((_) => {
|
2015-08-31 00:25:46 -04:00
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should return whether a given instruction is active with isRouteActive',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
|
|
|
router.registerPrimaryOutlet(outlet)
|
|
|
|
.then((_) => router.config([
|
|
|
|
new Route({path: '/a', component: DummyComponent, as: 'A'}),
|
|
|
|
new Route({path: '/b', component: DummyComponent, as: 'B'})
|
|
|
|
]))
|
|
|
|
.then((_) => router.navigate('/a'))
|
|
|
|
.then((_) => {
|
|
|
|
var instruction = router.generate(['/A']);
|
|
|
|
var otherInstruction = router.generate(['/B']);
|
|
|
|
|
|
|
|
expect(router.isRouteActive(instruction)).toEqual(true);
|
|
|
|
expect(router.isRouteActive(otherInstruction)).toEqual(false);
|
2015-08-14 15:31:56 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
describe('query string params', () => {
|
|
|
|
it('should use query string params for the root route', () => {
|
|
|
|
router.config(
|
|
|
|
[new Route({path: '/hi/how/are/you', component: DummyComponent, as: 'greeting-url'})]);
|
2015-07-21 04:26:43 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction = router.generate(['/greeting-url', {'name': 'brad'}]);
|
|
|
|
var path = stringifyInstruction(instruction);
|
|
|
|
expect(path).toEqual('hi/how/are/you?name=brad');
|
|
|
|
});
|
2015-07-21 04:26:43 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should serialize parameters that are not part of the route definition as query string params',
|
2015-07-21 04:26:43 -04:00
|
|
|
() => {
|
|
|
|
router.config(
|
|
|
|
[new Route({path: '/one/two/:three', component: DummyComponent, as: 'number-url'})]);
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction = router.generate(['/number-url', {'three': 'three', 'four': 'four'}]);
|
|
|
|
var path = stringifyInstruction(instruction);
|
|
|
|
expect(path).toEqual('one/two/three?four=four');
|
2015-07-21 04:26:43 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-07-08 13:57:38 -04:00
|
|
|
describe('matrix params', () => {
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should generate matrix params for each non-root component', () => {
|
2015-07-13 19:12:48 -04:00
|
|
|
router.config(
|
|
|
|
[new Route({path: '/first/...', component: DummyParentComp, as: 'firstCmp'})]);
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction =
|
2015-07-08 13:57:38 -04:00
|
|
|
router.generate(['/firstCmp', {'key': 'value'}, 'secondCmp', {'project': 'angular'}]);
|
2015-07-17 16:36:53 -04:00
|
|
|
var path = stringifyInstruction(instruction);
|
|
|
|
expect(path).toEqual('first/second;project=angular?key=value');
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should work with named params', () => {
|
|
|
|
router.config(
|
|
|
|
[new Route({path: '/first/:token/...', component: DummyParentComp, as: 'firstCmp'})]);
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction =
|
|
|
|
router.generate(['/firstCmp', {'token': 'min'}, 'secondCmp', {'author': 'max'}]);
|
|
|
|
var path = stringifyInstruction(instruction);
|
|
|
|
expect(path).toEqual('first/min/second;author=max');
|
|
|
|
});
|
2015-07-08 13:57:38 -04:00
|
|
|
});
|
2015-05-29 17:58:41 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-08-14 15:31:56 -04:00
|
|
|
function loader(): Promise<Type> {
|
|
|
|
return PromiseWrapper.resolve(DummyComponent);
|
|
|
|
}
|
|
|
|
|
2015-05-21 16:59:14 -04:00
|
|
|
class DummyComponent {}
|
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([new Route({path: '/second', component: DummyComponent, as: 'secondCmp'})])
|
2015-07-06 20:41:15 -04:00
|
|
|
class DummyParentComp {
|
|
|
|
}
|
|
|
|
|
2015-05-29 17:58:41 -04:00
|
|
|
function makeDummyOutlet() {
|
2015-08-26 14:41:41 -04:00
|
|
|
var ref = new SpyRouterOutlet();
|
2015-05-29 17:58:41 -04:00
|
|
|
ref.spy('canActivate').andCallFake((_) => PromiseWrapper.resolve(true));
|
2015-07-07 18:44:29 -04:00
|
|
|
ref.spy('canReuse').andCallFake((_) => PromiseWrapper.resolve(false));
|
2015-05-29 17:58:41 -04:00
|
|
|
ref.spy('canDeactivate').andCallFake((_) => PromiseWrapper.resolve(true));
|
2015-08-31 00:25:46 -04:00
|
|
|
ref.spy('activate').andCallFake((_) => PromiseWrapper.resolve(true));
|
2015-05-29 17:58:41 -04:00
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
class AppCmp {}
|