Skip to content

Commit

Permalink
fix(RouterStore): Fix cancelled navigation with async guard (fixes ng…
Browse files Browse the repository at this point in the history
  • Loading branch information
kondi committed Sep 6, 2017
1 parent 28e1983 commit f0e9cea
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
35 changes: 35 additions & 0 deletions modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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<any> = 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(
Expand Down
2 changes: 2 additions & 0 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ export class StoreRouterConnectingModule {
private setUpStoreStateListener(): void {
this.store.subscribe(s => {
this.storeState = s;
});
this.store.select('routerReducer').subscribe(() => {
this.navigateIfNeeded();
});
}
Expand Down

0 comments on commit f0e9cea

Please sign in to comment.