feat(router): use componentFactoryResolver

This commit is contained in:
vsavkin 2016-06-27 16:45:21 -07:00
parent e12b1277df
commit dc64e90ab9
4 changed files with 96 additions and 98 deletions

View File

@ -6,7 +6,8 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Attribute, ComponentFactory, ComponentRef, Directive, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core'; import {Attribute, ComponentFactory, ComponentFactoryResolver, ComponentRef, Directive, NoComponentFactoryError, ReflectiveInjector, ResolvedReflectiveProvider, ViewContainerRef} from '@angular/core';
import {RouterOutletMap} from '../router_outlet_map'; import {RouterOutletMap} from '../router_outlet_map';
import {ActivatedRoute} from '../router_state'; import {ActivatedRoute} from '../router_state';
import {PRIMARY_OUTLET} from '../shared'; import {PRIMARY_OUTLET} from '../shared';
@ -22,7 +23,7 @@ export class RouterOutlet {
*/ */
constructor( constructor(
parentOutletMap: RouterOutletMap, private location: ViewContainerRef, parentOutletMap: RouterOutletMap, private location: ViewContainerRef,
@Attribute('name') name: string) { private componentFactoryResolver: ComponentFactoryResolver, @Attribute('name') name: string) {
parentOutletMap.registerOutlet(name ? name : PRIMARY_OUTLET, this); parentOutletMap.registerOutlet(name ? name : PRIMARY_OUTLET, this);
} }
@ -44,10 +45,27 @@ export class RouterOutlet {
} }
activate( activate(
factory: ComponentFactory<any>, activatedRoute: ActivatedRoute, activatedRoute: ActivatedRoute, providers: ResolvedReflectiveProvider[],
providers: ResolvedReflectiveProvider[], outletMap: RouterOutletMap): void { outletMap: RouterOutletMap): void {
this.outletMap = outletMap; this.outletMap = outletMap;
this._activatedRoute = activatedRoute; this._activatedRoute = activatedRoute;
const snapshot = activatedRoute._futureSnapshot;
const component: any = <any>snapshot._routeConfig.component;
let factory;
try {
factory = typeof component === 'string' ?
snapshot._resolvedComponentFactory :
this.componentFactoryResolver.resolveComponentFactory(component);
} catch (e) {
if (!(e instanceof NoComponentFactoryError)) throw e;
const componentName = component ? component.name : null;
console.warn(
`No component factory found for '${componentName}'. Add '${componentName}' to the 'precompile' list of your application component. This will be required in a future release of the router.`);
factory = snapshot._resolvedComponentFactory;
}
const inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector); const inj = ReflectiveInjector.fromResolvedProviders(providers, this.location.parentInjector);
this.activated = this.location.createComponent(factory, this.location.length, inj, []); this.activated = this.location.createComponent(factory, this.location.length, inj, []);
} }

View File

@ -41,6 +41,7 @@ function resolveNode(
function resolveComponent( function resolveComponent(
resolver: ComponentResolver, snapshot: ActivatedRouteSnapshot): Promise<any> { resolver: ComponentResolver, snapshot: ActivatedRouteSnapshot): Promise<any> {
// TODO: vsavkin change to typeof snapshot.component === 'string' in beta2
if (snapshot.component && snapshot._routeConfig) { if (snapshot.component && snapshot._routeConfig) {
return resolver.resolveComponent(<any>snapshot.component); return resolver.resolveComponent(<any>snapshot.component);
} else { } else {

View File

@ -619,7 +619,7 @@ class ActivateRoutes {
{provide: ActivatedRoute, useValue: future}, {provide: ActivatedRoute, useValue: future},
{provide: RouterOutletMap, useValue: outletMap} {provide: RouterOutletMap, useValue: outletMap}
]); ]);
outlet.activate(future._futureSnapshot._resolvedComponentFactory, future, resolved, outletMap); outlet.activate(future, resolved, outletMap);
} }
private deactivateOutletAndItChildren(outlet: RouterOutlet): void { private deactivateOutletAndItChildren(outlet: RouterOutlet): void {

View File

@ -28,10 +28,8 @@ describe('Integration', () => {
provide: Router, provide: Router,
useFactory: (resolver: ComponentResolver, urlSerializer: UrlSerializer, useFactory: (resolver: ComponentResolver, urlSerializer: UrlSerializer,
outletMap: RouterOutletMap, location: Location, injector: Injector) => { outletMap: RouterOutletMap, location: Location, injector: Injector) => {
const r = return new Router(
new Router(RootCmp, resolver, urlSerializer, outletMap, location, injector, config); RootCmp, resolver, urlSerializer, outletMap, location, injector, config);
r.initialNavigation();
return r;
}, },
deps: [ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector] deps: [ComponentResolver, UrlSerializer, RouterOutletMap, Location, Injector]
}, },
@ -39,12 +37,11 @@ describe('Integration', () => {
]; ];
}); });
it('should navigate with a provided config', fit('should navigate with a provided config',
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.navigateByUrl('/simple'); router.navigateByUrl('/simple');
advance(fixture); advance(fixture);
@ -57,8 +54,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{path: 'team/:id', component: TeamCmp}]); router.resetConfig([{path: 'team/:id', component: TeamCmp}]);
@ -76,8 +72,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -108,8 +103,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -130,8 +124,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{path: '**', component: CollectParamsCmp}]); router.resetConfig([{path: '**', component: CollectParamsCmp}]);
@ -153,8 +146,7 @@ describe('Integration', () => {
it('should support secondary routes', it('should support secondary routes',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -175,8 +167,7 @@ describe('Integration', () => {
it('should deactivate outlets', it('should deactivate outlets',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -200,8 +191,7 @@ describe('Integration', () => {
it('should deactivate nested outlets', it('should deactivate nested outlets',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([ router.resetConfig([
{ {
@ -227,8 +217,7 @@ describe('Integration', () => {
it('should set query params and fragment', it('should set query params and fragment',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{path: 'query', component: QueryParamsAndFragmentCmp}]); router.resetConfig([{path: 'query', component: QueryParamsAndFragmentCmp}]);
@ -244,8 +233,7 @@ describe('Integration', () => {
it('should push params only when they change', it('should push params only when they change',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -271,8 +259,7 @@ describe('Integration', () => {
it('should work when navigating to /', it('should work when navigating to /',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([ router.resetConfig([
{path: '', terminal: true, component: SimpleCmp}, {path: '', terminal: true, component: SimpleCmp},
@ -293,8 +280,7 @@ describe('Integration', () => {
it('should cancel in-flight navigations', it('should cancel in-flight navigations',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{path: 'user/:name', component: UserCmp}]); router.resetConfig([{path: 'user/:name', component: UserCmp}]);
@ -331,8 +317,7 @@ describe('Integration', () => {
it('should handle failed navigations gracefully', it('should handle failed navigations gracefully',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{path: 'user/:name', component: UserCmp}]); router.resetConfig([{path: 'user/:name', component: UserCmp}]);
@ -361,8 +346,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -390,8 +374,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmpWithTwoOutlets); const fixture = createRoot(tcb, router, RootCmpWithTwoOutlets);
advance(fixture);
router.resetConfig([ router.resetConfig([
{ {
@ -448,8 +431,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmpWithTwoOutlets); const fixture = createRoot(tcb, router, RootCmpWithTwoOutlets);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'parent/:id', path: 'parent/:id',
@ -499,8 +481,7 @@ describe('Integration', () => {
it('should support string router links', it('should support string router links',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -526,8 +507,7 @@ describe('Integration', () => {
it('should support absolute router links', it('should support absolute router links',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -553,8 +533,7 @@ describe('Integration', () => {
it('should support relative router links', it('should support relative router links',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -580,7 +559,7 @@ describe('Integration', () => {
it('should support top-level link', it('should support top-level link',
fakeAsync( fakeAsync(
inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => { inject([Router, TestComponentBuilder], (router: Router, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RelativeLinkInIfCmp); const fixture = createRoot(tcb, router, RelativeLinkInIfCmp);
advance(fixture); advance(fixture);
router.resetConfig( router.resetConfig(
@ -608,8 +587,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, Location, TestComponentBuilder], [Router, Location, TestComponentBuilder],
(router: Router, location: Location, tcb: TestComponentBuilder) => { (router: Router, location: Location, tcb: TestComponentBuilder) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -638,8 +616,7 @@ describe('Integration', () => {
it('should work', fakeAsync(inject( it('should work', fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([ router.resetConfig([
{path: 'old/team/:id', redirectTo: 'team/:id'}, {path: 'old/team/:id', redirectTo: 'team/:id'},
@ -662,8 +639,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysFalse']}]); [{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysFalse']}]);
@ -684,8 +660,7 @@ describe('Integration', () => {
it('works', fakeAsync(inject( it('works', fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'parent', path: 'parent',
@ -710,8 +685,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysTrue']}]); [{path: 'team/:id', component: TeamCmp, canActivate: ['alwaysTrue']}]);
@ -735,8 +709,7 @@ describe('Integration', () => {
it('works', fakeAsync(inject( it('works', fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canActivate: [AlwaysTrue]}]); [{path: 'team/:id', component: TeamCmp, canActivate: [AlwaysTrue]}]);
@ -760,8 +733,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canActivate: ['CanActivate']}]); [{path: 'team/:id', component: TeamCmp, canActivate: ['CanActivate']}]);
@ -800,8 +772,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([ router.resetConfig([
{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivateTeam']} {path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivateTeam']}
@ -828,8 +799,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'parent/:id', path: 'parent/:id',
@ -854,8 +824,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -902,8 +871,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canDeactivate: [AlwaysTrue]}]); [{path: 'team/:id', component: TeamCmp, canDeactivate: [AlwaysTrue]}]);
@ -930,8 +898,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig( router.resetConfig(
[{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivate']}]); [{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivate']}]);
@ -952,8 +919,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -983,8 +949,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -1014,8 +979,7 @@ describe('Integration', () => {
fakeAsync(inject( fakeAsync(inject(
[Router, TestComponentBuilder, Location], [Router, TestComponentBuilder, Location],
(router: Router, tcb: TestComponentBuilder, location: Location) => { (router: Router, tcb: TestComponentBuilder, location: Location) => {
const fixture = tcb.createFakeAsync(RootCmp); const fixture = createRoot(tcb, router, RootCmp);
advance(fixture);
router.resetConfig([{ router.resetConfig([{
path: 'team/:id', path: 'team/:id',
@ -1097,16 +1061,6 @@ class DummyLinkWithParentCmp {
class RelativeLinkCmp { class RelativeLinkCmp {
} }
@Component({
selector: 'link-cmp',
template:
`<div *ngIf="show"><a [routerLink]="['./simple']">link</a></div> <router-outlet></router-outlet>`,
directives: ROUTER_DIRECTIVES
})
class RelativeLinkInIfCmp {
show: boolean = false;
}
@Component({ @Component({
selector: 'link-cmp', selector: 'link-cmp',
template: `<a [routerLink]="['../simple']" [queryParams]="{q: '1'}" fragment="f">link</a>`, template: `<a [routerLink]="['../simple']" [queryParams]="{q: '1'}" fragment="f">link</a>`,
@ -1187,10 +1141,26 @@ class RouteCmp {
constructor(public route: ActivatedRoute) {} constructor(public route: ActivatedRoute) {}
} }
@Component({
selector: 'link-cmp',
template:
`<div *ngIf="show"><a [routerLink]="['./simple']">link</a></div> <router-outlet></router-outlet>`,
directives: ROUTER_DIRECTIVES,
precompile: [BlankCmp, SimpleCmp]
})
class RelativeLinkInIfCmp {
show: boolean = false;
}
@Component({ @Component({
selector: 'root-cmp', selector: 'root-cmp',
template: `<router-outlet></router-outlet>`, template: `<router-outlet></router-outlet>`,
directives: [ROUTER_DIRECTIVES] directives: [ROUTER_DIRECTIVES],
precompile: [
BlankCmp, SimpleCmp, TeamCmp, UserCmp, StringLinkCmp, DummyLinkCmp, AbsoluteLinkCmp,
RelativeLinkCmp, DummyLinkWithParentCmp, LinkWithQueryParamsAndFragment, CollectParamsCmp,
QueryParamsAndFragmentCmp
]
}) })
class RootCmp { class RootCmp {
} }
@ -1199,7 +1169,8 @@ class RootCmp {
selector: 'root-cmp', selector: 'root-cmp',
template: template:
`primary {<router-outlet></router-outlet>} right {<router-outlet name="right"></router-outlet>}`, `primary {<router-outlet></router-outlet>} right {<router-outlet name="right"></router-outlet>}`,
directives: [ROUTER_DIRECTIVES] directives: [ROUTER_DIRECTIVES],
precompile: [BlankCmp, SimpleCmp, RouteCmp, UserCmp]
}) })
class RootCmpWithTwoOutlets { class RootCmpWithTwoOutlets {
} }
@ -1208,3 +1179,11 @@ function advance(fixture: ComponentFixture<any>): void {
tick(); tick();
fixture.detectChanges(); fixture.detectChanges();
} }
function createRoot(tcb: TestComponentBuilder, router: Router, type: any): ComponentFixture<any> {
const f = tcb.createFakeAsync(type);
advance(f);
router.initialNavigation();
advance(f);
return f;
}