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-10-13 03:29:13 -04:00
|
|
|
} from 'angular2/testing_internal';
|
2015-08-26 14:41:41 -04:00
|
|
|
import {SpyRouterOutlet} from './spies';
|
2015-11-06 20:34:07 -05:00
|
|
|
import {Type} from 'angular2/src/facade/lang';
|
|
|
|
import {Promise, PromiseWrapper, ObservableWrapper} from 'angular2/src/facade/async';
|
|
|
|
import {ListWrapper} from 'angular2/src/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 {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-10-02 10:37:23 -04:00
|
|
|
import {DirectiveResolver} from 'angular2/src/core/linker/directive_resolver';
|
2015-05-29 17:58:41 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
import {provide} from 'angular2/core';
|
2015-05-29 17:58:41 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('Router', () => {
|
|
|
|
var router, location;
|
|
|
|
|
|
|
|
beforeEachBindings(() => [
|
2015-07-06 20:41:15 -04:00
|
|
|
RouteRegistry,
|
2015-05-29 17:58:41 -04:00
|
|
|
DirectiveResolver,
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(Location, {useClass: SpyLocation}),
|
2015-10-11 01:11:13 -04:00
|
|
|
provide(Router,
|
|
|
|
{
|
2015-10-12 14:30:34 -04:00
|
|
|
useFactory:
|
2015-10-11 01:11:13 -04:00
|
|
|
(registry, location) => { return new RootRouter(registry, location, AppCmp); },
|
|
|
|
deps: [RouteRegistry, 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-09-09 00:41:56 -04:00
|
|
|
.then((_) => router.navigateByUrl('/a'))
|
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(['/a']);
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-09-09 00:42:23 -04:00
|
|
|
it('should activate viewports and update URL when navigating via DSL',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var outlet = makeDummyOutlet();
|
|
|
|
|
|
|
|
router.registerPrimaryOutlet(outlet)
|
2015-10-25 05:30:27 -04:00
|
|
|
.then((_) => router.config(
|
|
|
|
[new Route({path: '/a', component: DummyComponent, name: 'A'})]))
|
2015-09-09 00:42:23 -04:00
|
|
|
.then((_) => router.navigate(['/A']))
|
|
|
|
.then((_) => {
|
|
|
|
expect(outlet.spy('activate')).toHaveBeenCalled();
|
|
|
|
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})]))
|
2015-09-09 00:41:56 -04:00
|
|
|
.then((_) => router.navigateByUrl('/b', true))
|
2015-07-28 01:46:09 -04:00
|
|
|
.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-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-09-09 00:41:56 -04:00
|
|
|
.then((_) => router.navigateByUrl('/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 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-10-25 05:30:27 -04:00
|
|
|
router.config(
|
|
|
|
[new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
2015-07-06 20:41:15 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
var instruction = router.generate(['/FirstCmp', 'SecondCmp']);
|
2015-07-17 16:36:53 -04:00
|
|
|
expect(stringifyInstruction(instruction)).toEqual('first/second');
|
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
instruction = router.generate(['/FirstCmp/SecondCmp']);
|
2015-07-17 16:36:53 -04:00
|
|
|
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-10-25 05:30:27 -04:00
|
|
|
router.config([new AsyncRoute({path: '/first', loader: loader, name: 'FirstCmp'})]);
|
2015-08-14 15:31:56 -04:00
|
|
|
|
|
|
|
var instruction = router.generate(['/FirstCmp']);
|
2015-09-09 00:41:56 -04:00
|
|
|
router.navigateByInstruction(instruction)
|
2015-08-14 15:31:56 -04:00
|
|
|
.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([
|
2015-10-25 05:30:27 -04:00
|
|
|
new Route({path: '/a', component: DummyComponent, name: 'A'}),
|
|
|
|
new Route({path: '/b', component: DummyComponent, name: 'B'})
|
2015-08-31 00:25:46 -04:00
|
|
|
]))
|
2015-09-09 00:41:56 -04:00
|
|
|
.then((_) => router.navigateByUrl('/a'))
|
2015-08-31 00:25:46 -04:00
|
|
|
.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(
|
2015-10-25 05:30:27 -04:00
|
|
|
[new Route({path: '/hi/how/are/you', component: DummyComponent, name: 'GreetingUrl'})]);
|
2015-07-21 04:26:43 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
var instruction = router.generate(['/GreetingUrl', {'name': 'brad'}]);
|
2015-07-17 16:36:53 -04:00
|
|
|
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
|
|
|
() => {
|
2015-10-25 05:30:27 -04:00
|
|
|
router.config([
|
|
|
|
new Route({path: '/one/two/:three', component: DummyComponent, name: 'NumberUrl'})
|
|
|
|
]);
|
2015-07-21 04:26:43 -04:00
|
|
|
|
2015-09-14 20:36:32 -04:00
|
|
|
var instruction = router.generate(['/NumberUrl', {'three': 'three', 'four': 'four'}]);
|
2015-07-17 16:36:53 -04:00
|
|
|
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(
|
2015-10-25 05:30:27 -04:00
|
|
|
[new Route({path: '/first/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction =
|
2015-09-14 20:36:32 -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(
|
2015-10-25 05:30:27 -04:00
|
|
|
[new Route({path: '/first/:token/...', component: DummyParentComp, name: 'FirstCmp'})]);
|
2015-07-08 13:57:38 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
var instruction =
|
2015-09-14 20:36:32 -04:00
|
|
|
router.generate(['/FirstCmp', {'token': 'min'}, 'SecondCmp', {'author': 'max'}]);
|
2015-07-17 16:36:53 -04:00
|
|
|
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-10-25 05:30:27 -04:00
|
|
|
@RouteConfig([new Route({path: '/second', component: DummyComponent, name: '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 {}
|