Skip to content

Commit

Permalink
fix(RouterStore): Add support for cancellation with CanLoad guard (#223)
Browse files Browse the repository at this point in the history
Closes #213
  • Loading branch information
brandonroberts authored and MikeRyanDev committed Aug 2, 2017
1 parent e2f1e57 commit 2c006e8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
41 changes: 40 additions & 1 deletion modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,40 @@ describe('integration spec', () => {
done();
});
});

it('should support cancellation of initial navigation using canLoad guard', done => {
const reducer = (state: any, action: RouterAction<any>) => {
const r = routerReducer(state, action);
return r && r.state
? { url: r.state.url, navigationId: r.navigationId }
: null;
};

createTestModule({
reducers: { routerReducer, reducer },
canLoad: () => false,
});

const router = TestBed.get(Router);
const store = TestBed.get(Store);
const log = logOfRouterAndStore(router, store);

router.navigateByUrl('/load').then((r: boolean) => {
expect(r).toBe(false);

expect(log).toEqual([
{ type: 'store', state: null },
{ type: 'router', event: 'NavigationStart', url: '/load' },
{ type: 'store', state: null },
{ type: 'router', event: 'NavigationCancel', url: '/load' },
]);
done();
});
});
});

function createTestModule(
opts: { reducers?: any; canActivate?: Function } = {}
opts: { reducers?: any; canActivate?: Function; canLoad?: Function } = {}
) {
@Component({
selector: 'test-app',
Expand All @@ -314,6 +344,11 @@ function createTestModule(
component: SimpleCmp,
canActivate: ['CanActivateNext'],
},
{
path: 'load',
loadChildren: 'test',
canLoad: ['CanLoadNext'],
},
]),
StoreRouterConnectingModule,
],
Expand All @@ -322,6 +357,10 @@ function createTestModule(
provide: 'CanActivateNext',
useValue: opts.canActivate || (() => true),
},
{
provide: 'CanLoadNext',
useValue: opts.canLoad || (() => true),
},
],
});

Expand Down
7 changes: 6 additions & 1 deletion modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,12 @@ export class StoreRouterConnectingModule {
}

private navigateIfNeeded(): void {
if (!this.storeState['routerReducer']) return;
if (
!this.storeState['routerReducer'] ||
!this.storeState['routerReducer'].state
) {
return;
}
if (this.dispatchTriggeredByRouter) return;

if (this.router.url !== this.storeState['routerReducer'].state.url) {
Expand Down

0 comments on commit 2c006e8

Please sign in to comment.