2016-04-07 20:17:50 -04:00
|
|
|
import {AsyncTestCompleter, beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit,} from 'angular2/testing_internal';
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2015-11-23 13:18:04 -05:00
|
|
|
import {bootstrap} from 'angular2/platform/browser';
|
2016-03-08 16:36:48 -05:00
|
|
|
import {Component, Directive} from 'angular2/src/core/metadata';
|
2015-11-19 18:09:34 -05:00
|
|
|
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
|
2015-12-15 11:34:44 -05:00
|
|
|
import {Console} from 'angular2/src/core/console';
|
2015-10-11 01:11:13 -04:00
|
|
|
import {provide} from 'angular2/core';
|
2015-11-17 18:24:36 -05:00
|
|
|
import {DOCUMENT} from 'angular2/src/platform/dom/dom_tokens';
|
2015-12-08 16:27:56 -05:00
|
|
|
import {Type, IS_DART} from 'angular2/src/facade/lang';
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2016-04-07 20:17:50 -04:00
|
|
|
import {ROUTER_PROVIDERS, Router, RouteConfig, APP_BASE_HREF, ROUTER_DIRECTIVES} from 'angular2/router';
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2015-11-06 20:34:07 -05:00
|
|
|
import {ExceptionHandler} from 'angular2/src/facade/exceptions';
|
2016-02-09 14:12:41 -05:00
|
|
|
import {LocationStrategy} from 'angular2/src/router/location/location_strategy';
|
2015-07-13 19:12:48 -04:00
|
|
|
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
|
|
|
|
|
2015-08-07 14:41:38 -04:00
|
|
|
class _ArrayLogger {
|
|
|
|
res: any[] = [];
|
|
|
|
log(s: any): void { this.res.push(s); }
|
2015-08-24 14:35:27 -04:00
|
|
|
logError(s: any): void { this.res.push(s); }
|
2015-08-07 14:41:38 -04:00
|
|
|
logGroup(s: any): void { this.res.push(s); }
|
|
|
|
logGroupEnd(){};
|
|
|
|
}
|
|
|
|
|
2015-12-15 11:34:44 -05:00
|
|
|
class DummyConsole implements Console {
|
|
|
|
log(message) {}
|
|
|
|
}
|
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
export function main() {
|
|
|
|
describe('RouteConfig with POJO arguments', () => {
|
|
|
|
var fakeDoc, el, testBindings;
|
|
|
|
beforeEach(() => {
|
|
|
|
fakeDoc = DOM.createHtmlDocument();
|
|
|
|
el = DOM.createElement('app-cmp', fakeDoc);
|
|
|
|
DOM.appendChild(fakeDoc.body, el);
|
2015-08-07 14:41:38 -04:00
|
|
|
var logger = new _ArrayLogger();
|
2016-02-25 17:24:17 -05:00
|
|
|
var exceptionHandler = new ExceptionHandler(logger, false);
|
2015-07-13 19:12:48 -04:00
|
|
|
testBindings = [
|
2016-04-07 20:17:50 -04:00
|
|
|
ROUTER_PROVIDERS, provide(LocationStrategy, {useClass: MockLocationStrategy}),
|
2015-10-12 14:30:34 -04:00
|
|
|
provide(DOCUMENT, {useValue: fakeDoc}),
|
2015-12-15 11:34:44 -05:00
|
|
|
provide(ExceptionHandler, {useValue: exceptionHandler}),
|
|
|
|
provide(Console, {useClass: DummyConsole})
|
2015-07-13 19:12:48 -04:00
|
|
|
];
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should bootstrap an app with a hierarchy', inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(HierarchyAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { parent { hello } }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/parent/child');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/parent/child');
|
|
|
|
});
|
2015-09-09 10:41:11 -04:00
|
|
|
}));
|
2015-07-13 19:12:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should work in an app with redirects', inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(RedirectAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { hello }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/after');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/before');
|
|
|
|
});
|
2015-09-09 10:41:11 -04:00
|
|
|
}));
|
2015-07-13 19:12:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should work in an app with async components', inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(AsyncAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { hello }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/hello');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/hello');
|
|
|
|
});
|
2015-09-09 10:41:11 -04:00
|
|
|
}));
|
2015-07-13 19:12:48 -04:00
|
|
|
|
|
|
|
|
2015-10-30 20:05:30 -04:00
|
|
|
it('should work in an app with aux routes', inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(AuxAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { hello } aside { hello }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/hello(aside)');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/hello(aside)');
|
|
|
|
});
|
2015-10-30 20:05:30 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
2015-10-08 15:27:55 -04:00
|
|
|
it('should work in an app with async components defined with "loader"',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(ConciseAsyncAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { hello }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/hello');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/hello');
|
|
|
|
});
|
2015-10-08 15:27:55 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
2015-07-13 19:12:48 -04:00
|
|
|
it('should work in an app with a constructor component',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2016-04-07 20:17:50 -04:00
|
|
|
bootstrap(ExplicitConstructorAppCmp, testBindings).then((applicationRef) => {
|
|
|
|
var router = applicationRef.hostComponent.router;
|
|
|
|
router.subscribe((_) => {
|
|
|
|
expect(el).toHaveText('root { hello }');
|
|
|
|
expect(applicationRef.hostComponent.location.path()).toEqual('/hello');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
router.navigateByUrl('/hello');
|
|
|
|
});
|
2015-09-09 10:41:11 -04:00
|
|
|
}));
|
2015-07-13 19:12:48 -04:00
|
|
|
|
2015-07-17 16:36:53 -04:00
|
|
|
it('should throw if a config is missing a target',
|
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter],
|
2016-04-07 20:17:50 -04:00
|
|
|
(async) => {bootstrap(WrongConfigCmp, testBindings).catch((e) => {
|
|
|
|
expect(e.originalException)
|
|
|
|
.toContainError(
|
|
|
|
'Route config should contain exactly one "component", "loader", or "redirectTo" property.');
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
})}));
|
2015-07-17 16:36:53 -04:00
|
|
|
|
|
|
|
it('should throw if a config has an invalid component type',
|
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter],
|
2016-04-07 20:17:50 -04:00
|
|
|
(async) => {bootstrap(WrongComponentTypeCmp, testBindings).catch((e) => {
|
|
|
|
expect(e.originalException)
|
|
|
|
.toContainError(
|
|
|
|
'Invalid component type "intentionallyWrongComponentType". Valid types are "constructor" and "loader".');
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
})}));
|
2015-09-14 19:01:09 -04:00
|
|
|
|
|
|
|
it('should throw if a config has an invalid alias name',
|
2015-10-25 05:30:27 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter],
|
2016-04-07 20:17:50 -04:00
|
|
|
(async) => {bootstrap(BadAliasNameCmp, testBindings).catch((e) => {
|
|
|
|
expect(e.originalException)
|
|
|
|
.toContainError(
|
|
|
|
`Route "/child" with name "child" does not begin with an uppercase letter. Route names should be CamelCase like "Child".`);
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
})}));
|
2015-10-25 05:30:27 -04:00
|
|
|
|
|
|
|
it('should throw if a config has an invalid alias name with "as"',
|
2015-09-14 19:01:09 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter],
|
2016-04-07 20:17:50 -04:00
|
|
|
(async) => {bootstrap(BadAliasCmp, testBindings).catch((e) => {
|
|
|
|
expect(e.originalException)
|
|
|
|
.toContainError(
|
|
|
|
`Route "/child" with name "child" does not begin with an uppercase letter. Route names should be CamelCase like "Child".`);
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
})}));
|
2015-10-25 05:30:27 -04:00
|
|
|
|
|
|
|
it('should throw if a config has multiple alias properties "as" and "name"',
|
2016-04-07 20:17:50 -04:00
|
|
|
inject(
|
|
|
|
[AsyncTestCompleter],
|
|
|
|
(async) => {bootstrap(MultipleAliasCmp, testBindings).catch((e) => {
|
|
|
|
expect(e.originalException)
|
|
|
|
.toContainError(
|
|
|
|
`Route config should contain exactly one "as" or "name" property.`);
|
|
|
|
async.done();
|
|
|
|
return null;
|
|
|
|
})}));
|
2015-07-13 19:12:48 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({selector: 'hello-cmp', template: 'hello'})
|
2015-07-13 19:12:48 -04:00
|
|
|
class HelloCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
@RouteConfig([
|
2016-04-07 20:17:50 -04:00
|
|
|
{path: '/before', redirectTo: ['Hello']}, {path: '/after', component: HelloCmp, name: 'Hello'}
|
refactor(router): improve recognition and generation pipeline
This is a big change. @matsko also deserves much of the credit for the implementation.
Previously, `ComponentInstruction`s held all the state for async components.
Now, we introduce several subclasses for `Instruction` to describe each type of navigation.
BREAKING CHANGE:
Redirects now use the Link DSL syntax. Before:
```
@RouteConfig([
{ path: '/foo', redirectTo: '/bar' },
{ path: '/bar', component: BarCmp }
])
```
After:
```
@RouteConfig([
{ path: '/foo', redirectTo: ['Bar'] },
{ path: '/bar', component: BarCmp, name: 'Bar' }
])
```
BREAKING CHANGE:
This also introduces `useAsDefault` in the RouteConfig, which makes cases like lazy-loading
and encapsulating large routes with sub-routes easier.
Previously, you could use `redirectTo` like this to expand a URL like `/tab` to `/tab/posts`:
@RouteConfig([
{ path: '/tab', redirectTo: '/tab/users' }
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
Now the recommended way to handle this is case is to use `useAsDefault` like so:
```
@RouteConfig([
{ path: '/tab', component: TabsCmp, name: 'Tab' }
])
AppCmp { ... }
@RouteConfig([
{ path: '/posts', component: PostsCmp, useAsDefault: true, name: 'Posts' },
{ path: '/users', component: UsersCmp, name: 'Users' }
])
TabsCmp { ... }
```
In the above example, you can write just `['/Tab']` and the route `Users` is automatically selected as a child route.
Closes #4728
Closes #4228
Closes #4170
Closes #4490
Closes #4694
Closes #5200
Closes #5475
2015-11-23 21:07:37 -05:00
|
|
|
])
|
2015-07-13 19:12:48 -04:00
|
|
|
class RedirectAppCmp {
|
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
function HelloLoader(): Promise<any> {
|
|
|
|
return Promise.resolve(HelloCmp);
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([
|
|
|
|
{path: '/hello', component: {type: 'loader', loader: HelloLoader}},
|
|
|
|
])
|
|
|
|
class AsyncAppCmp {
|
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-10-08 15:27:55 -04:00
|
|
|
@RouteConfig([
|
|
|
|
{path: '/hello', loader: HelloLoader},
|
|
|
|
])
|
|
|
|
class ConciseAsyncAppCmp {
|
2015-10-30 20:05:30 -04:00
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> } aside { <router-outlet name="aside"></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-10-30 20:05:30 -04:00
|
|
|
@RouteConfig([{path: '/hello', component: HelloCmp}, {aux: 'aside', component: HelloCmp}])
|
|
|
|
class AuxAppCmp {
|
2015-10-08 15:27:55 -04:00
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([
|
|
|
|
{path: '/hello', component: {type: 'constructor', constructor: HelloCmp}},
|
|
|
|
])
|
|
|
|
class ExplicitConstructorAppCmp {
|
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'parent-cmp',
|
|
|
|
template: `parent { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([{path: '/child', component: HelloCmp}])
|
|
|
|
class ParentCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-13 19:12:48 -04:00
|
|
|
@RouteConfig([{path: '/parent/...', component: ParentCmp}])
|
|
|
|
class HierarchyAppCmp {
|
|
|
|
constructor(public router: Router, public location: LocationStrategy) {}
|
|
|
|
}
|
2015-07-17 16:36:53 -04:00
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-17 16:36:53 -04:00
|
|
|
@RouteConfig([{path: '/hello'}])
|
|
|
|
class WrongConfigCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-10-25 05:30:27 -04:00
|
|
|
@RouteConfig([{path: '/child', component: HelloCmp, name: 'child'}])
|
|
|
|
class BadAliasNameCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-09-14 19:01:09 -04:00
|
|
|
@RouteConfig([{path: '/child', component: HelloCmp, as: 'child'}])
|
|
|
|
class BadAliasCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-10-25 05:30:27 -04:00
|
|
|
@RouteConfig([{path: '/child', component: HelloCmp, as: 'Child', name: 'Child'}])
|
|
|
|
class MultipleAliasCmp {
|
|
|
|
}
|
|
|
|
|
2016-03-08 16:36:48 -05:00
|
|
|
@Component({
|
|
|
|
selector: 'app-cmp',
|
|
|
|
template: `root { <router-outlet></router-outlet> }`,
|
|
|
|
directives: ROUTER_DIRECTIVES
|
|
|
|
})
|
2015-07-17 16:36:53 -04:00
|
|
|
@RouteConfig([
|
|
|
|
{path: '/hello', component: {type: 'intentionallyWrongComponentType', constructor: HelloCmp}},
|
|
|
|
])
|
|
|
|
class WrongComponentTypeCmp {
|
|
|
|
}
|