From 1cdb67a72dd4759dcc75866412117e8dbe377aa3 Mon Sep 17 00:00:00 2001 From: Mike Ryan Date: Tue, 19 Apr 2016 11:12:53 -0500 Subject: [PATCH] fix(UndefinedRoutes): Route view correctly ignores undefined routes (#60) --- lib/route-view.ts | 10 +++++----- lib/route.ts | 4 ++++ spec/route.spec.ts | 7 +++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/route-view.ts b/lib/route-view.ts index 945153b..82a324f 100644 --- a/lib/route-view.ts +++ b/lib/route-view.ts @@ -36,8 +36,8 @@ import { ComponentRenderer } from './component-renderer'; export class RouteView implements OnDestroy, OnInit { private _prev: ComponentRef; private _sub: any; - private _routeSetProvider = provide(RouterInstruction, { - useValue: this._routeSet$.map(set => { + private _routerInstructionProvider = provide(RouterInstruction, { + useValue: this._routerInstruction$.map(set => { return { locationChange: set.locationChange, routeConfigs: [ ...set.routeConfigs ].slice(1), @@ -49,7 +49,7 @@ export class RouteView implements OnDestroy, OnInit { constructor( @Attribute('name') private _name: string, - protected _routeSet$: RouterInstruction, + protected _routerInstruction$: RouterInstruction, protected _injector: Injector, protected _renderer: ComponentRenderer, protected _dcl: DynamicComponentLoader, @@ -57,7 +57,7 @@ export class RouteView implements OnDestroy, OnInit { ) { } ngOnInit() { - this._sub = this._routeSet$ + this._sub = this._routerInstruction$ .map(set => { const route = set.routeConfigs[0]; const components = getNamedComponents(route, this._name); @@ -71,7 +71,7 @@ export class RouteView implements OnDestroy, OnInit { .do(ins => this._cleanPreviousRef()) .filter(({ components }) => !!components.component || !!components.loadComponent) .switchMap(({ route, components }) => this._renderer.render( - route, components, this._injector, this._ref, this._dcl, [ this._routeSetProvider ] + route, components, this._injector, this._ref, this._dcl, [ this._routerInstructionProvider ] )) .subscribe((ref: ComponentRef) => this._prev = ref); } diff --git a/lib/route.ts b/lib/route.ts index a4733ab..3a956c9 100644 --- a/lib/route.ts +++ b/lib/route.ts @@ -31,6 +31,10 @@ export interface Route extends IndexRoute { export const ROUTES = new OpaqueToken('@ngrx/router Init Routes'); export function getNamedComponents(route: IndexRoute, name?: string): BaseRoute { + if (!route) { + return { component: null, loadComponent: null }; + } + if (!name) { return { component: route.component, loadComponent: route.loadComponent }; } diff --git a/spec/route.spec.ts b/spec/route.spec.ts index e76c918..d79e932 100644 --- a/spec/route.spec.ts +++ b/spec/route.spec.ts @@ -32,5 +32,12 @@ describe('Route', function() { expect(resolved.component).toBe(FixtureB); expect(resolved.loadComponent).toBe(FixctureD); }); + + it('should return null for undefined routes', function() { + const resolved = getNamedComponents(undefined, 'main'); + + expect(resolved.component).toBe(null); + expect(resolved.loadComponent).toBe(null); + }); }); });