From 31dce72b7b5d7dcc32c1d9a96ff619527505d67a Mon Sep 17 00:00:00 2001 From: Victor Savkin Date: Wed, 21 Sep 2016 11:37:43 -0700 Subject: [PATCH] fix(router): update the router not to reset router state when updating root component (#11799) --- modules/@angular/router/src/router.ts | 4 +++- modules/@angular/router/test/router.spec.ts | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/modules/@angular/router/src/router.ts b/modules/@angular/router/src/router.ts index bcd3b53c3e..f5da74f613 100644 --- a/modules/@angular/router/src/router.ts +++ b/modules/@angular/router/src/router.ts @@ -323,7 +323,9 @@ export class Router { */ resetRootComponentType(rootComponentType: Type): void { this.rootComponentType = rootComponentType; - this.currentRouterState = createEmptyState(this.currentUrlTree, this.rootComponentType); + // TODO: vsavkin router 4.0 should make the root component set to null + // this will simplify the lifecycle of the router. + this.currentRouterState.root.component = this.rootComponentType; } /** diff --git a/modules/@angular/router/test/router.spec.ts b/modules/@angular/router/test/router.spec.ts index 42ea05b27b..007e6c9b7a 100644 --- a/modules/@angular/router/test/router.spec.ts +++ b/modules/@angular/router/test/router.spec.ts @@ -6,13 +6,31 @@ * found in the LICENSE file at https://angular.io/license */ -import {PreActivation} from '../src/router'; +import {TestBed} from '@angular/core/testing'; + +import {PreActivation, Router} from '../src/router'; import {RouterOutletMap} from '../src/router_outlet_map'; import {ActivatedRouteSnapshot, InheritedResolve, RouterStateSnapshot, createEmptyStateSnapshot} from '../src/router_state'; import {DefaultUrlSerializer} from '../src/url_tree'; import {TreeNode} from '../src/utils/tree'; +import {RouterTestingModule} from '../testing/router_testing_module'; describe('Router', () => { + describe('resetRootComponentType', () => { + class NewRootComponent {} + + beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); }); + + it('should not change root route when updating the root component', () => { + const r: Router = TestBed.get(Router); + const root = r.routerState.root; + + r.resetRootComponentType(NewRootComponent); + + expect(r.routerState.root).toBe(root); + }); + }); + describe('PreActivation', () => { const serializer = new DefaultUrlSerializer(); const inj = {get: (token: any) => () => `${token}_value`};