diff --git a/packages/router/src/create_router_state.ts b/packages/router/src/create_router_state.ts index 17e0f8b6ad..92ca55bdbf 100644 --- a/packages/router/src/create_router_state.ts +++ b/packages/router/src/create_router_state.ts @@ -65,7 +65,7 @@ function createOrReuseChildren( prevState: TreeNode) { return curr.children.map(child => { for (const p of prevState.children) { - if (routeReuseStrategy.shouldReuseRoute(p.value.snapshot, child.value)) { + if (routeReuseStrategy.shouldReuseRoute(child.value, p.value.snapshot)) { return createNode(routeReuseStrategy, child, p); } } diff --git a/packages/router/test/create_router_state.spec.ts b/packages/router/test/create_router_state.spec.ts index 3351eb2be9..7afb907eda 100644 --- a/packages/router/test/create_router_state.spec.ts +++ b/packages/router/test/create_router_state.spec.ts @@ -16,7 +16,10 @@ import {DefaultUrlSerializer, UrlSegmentGroup, UrlTree} from '../src/url_tree'; import {TreeNode} from '../src/utils/tree'; describe('create router state', () => { - const reuseStrategy = new DefaultRouteReuseStrategy(); + let reuseStrategy: DefaultRouteReuseStrategy; + beforeEach(() => { + reuseStrategy = new DefaultRouteReuseStrategy(); + }); const emptyState = () => createEmptyState(new (UrlTree as any)(new UrlSegmentGroup([], {}), {}, null!), RootComponent); @@ -109,6 +112,33 @@ describe('create router state', () => { // Verify the retrieve method has been called one more time expect(reuseStrategy.retrieve).toHaveBeenCalledTimes(3); }); + + it('should consistently represent future and current state', () => { + const config = [ + {path: '', pathMatch: 'full', component: ComponentA}, + {path: 'product/:id', component: ComponentB} + ]; + spyOn(reuseStrategy, 'shouldReuseRoute').and.callThrough(); + const previousState = createRouterState(reuseStrategy, createState(config, ''), emptyState()); + advanceState(previousState); + (reuseStrategy.shouldReuseRoute as jasmine.Spy).calls.reset(); + + createRouterState(reuseStrategy, createState(config, 'product/30'), previousState); + + // One call for the root and one call for each of the children + expect(reuseStrategy.shouldReuseRoute).toHaveBeenCalledTimes(2); + const reuseCalls = (reuseStrategy.shouldReuseRoute as jasmine.Spy).calls; + const future1 = reuseCalls.argsFor(0)[0]; + const current1 = reuseCalls.argsFor(0)[1]; + const future2 = reuseCalls.argsFor(1)[0]; + const current2 = reuseCalls.argsFor(1)[1]; + + // Routing from '' to 'product/30' + expect(current1._routerState.url).toEqual(''); + expect(future1._routerState.url).toEqual('product/30'); + expect(current2._routerState.url).toEqual(''); + expect(future2._routerState.url).toEqual('product/30'); + }); }); function advanceState(state: RouterState): void {