From f0e9cea7f2568551cda4221a5ccabb1248d11060 Mon Sep 17 00:00:00 2001 From: Pusztai Tibor Date: Wed, 6 Sep 2017 17:59:52 +0200 Subject: [PATCH] fix(RouterStore): Fix cancelled navigation with async guard (fixes #354) --- modules/router-store/spec/integration.spec.ts | 35 +++++++++++++++++++ .../router-store/src/router_store_module.ts | 2 ++ 2 files changed, 37 insertions(+) diff --git a/modules/router-store/spec/integration.spec.ts b/modules/router-store/spec/integration.spec.ts index 4af94dff08..9e0519a881 100644 --- a/modules/router-store/spec/integration.spec.ts +++ b/modules/router-store/spec/integration.spec.ts @@ -14,7 +14,10 @@ import { } from '../src/index'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/first'; +import 'rxjs/add/operator/mapTo'; +import 'rxjs/add/operator/take'; import 'rxjs/add/operator/toPromise'; +import { of } from 'rxjs/observable/of'; describe('integration spec', () => { it('should work', done => { @@ -370,6 +373,38 @@ describe('integration spec', () => { }); }); }); + + it('should support event during an async canActivate guard', done => { + createTestModule({ + reducers: { routerReducer }, + canActivate: () => { + store.dispatch({ type: 'USER_EVENT' }); + return store.take(1).mapTo(true); + }, + }); + + const router: Router = TestBed.get(Router); + const store: Store = TestBed.get(Store); + const log = logOfRouterAndStore(router, store); + + router + .navigateByUrl('/') + .then(() => { + log.splice(0); + return router.navigateByUrl('next'); + }) + .then(() => { + expect(log).toEqual([ + { type: 'router', event: 'NavigationStart', url: '/next' }, + { type: 'router', event: 'RoutesRecognized', url: '/next' }, + { type: 'store', state: undefined }, // after ROUTER_NAVIGATION + { type: 'store', state: undefined }, // after USER_EVENT + { type: 'router', event: 'NavigationEnd', url: '/next' }, + ]); + + done(); + }); + }); }); function createTestModule( diff --git a/modules/router-store/src/router_store_module.ts b/modules/router-store/src/router_store_module.ts index cbf111c2be..7343f5758d 100644 --- a/modules/router-store/src/router_store_module.ts +++ b/modules/router-store/src/router_store_module.ts @@ -186,6 +186,8 @@ export class StoreRouterConnectingModule { private setUpStoreStateListener(): void { this.store.subscribe(s => { this.storeState = s; + }); + this.store.select('routerReducer').subscribe(() => { this.navigateIfNeeded(); }); }