Skip to content

Commit 2c006e8

Browse files
brandonrobertsMikeRyanDev
authored andcommitted
fix(RouterStore): Add support for cancellation with CanLoad guard (#223)
Closes #213
1 parent e2f1e57 commit 2c006e8

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

modules/router-store/spec/integration.spec.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,40 @@ describe('integration spec', () => {
286286
done();
287287
});
288288
});
289+
290+
it('should support cancellation of initial navigation using canLoad guard', done => {
291+
const reducer = (state: any, action: RouterAction<any>) => {
292+
const r = routerReducer(state, action);
293+
return r && r.state
294+
? { url: r.state.url, navigationId: r.navigationId }
295+
: null;
296+
};
297+
298+
createTestModule({
299+
reducers: { routerReducer, reducer },
300+
canLoad: () => false,
301+
});
302+
303+
const router = TestBed.get(Router);
304+
const store = TestBed.get(Store);
305+
const log = logOfRouterAndStore(router, store);
306+
307+
router.navigateByUrl('/load').then((r: boolean) => {
308+
expect(r).toBe(false);
309+
310+
expect(log).toEqual([
311+
{ type: 'store', state: null },
312+
{ type: 'router', event: 'NavigationStart', url: '/load' },
313+
{ type: 'store', state: null },
314+
{ type: 'router', event: 'NavigationCancel', url: '/load' },
315+
]);
316+
done();
317+
});
318+
});
289319
});
290320

291321
function createTestModule(
292-
opts: { reducers?: any; canActivate?: Function } = {}
322+
opts: { reducers?: any; canActivate?: Function; canLoad?: Function } = {}
293323
) {
294324
@Component({
295325
selector: 'test-app',
@@ -314,6 +344,11 @@ function createTestModule(
314344
component: SimpleCmp,
315345
canActivate: ['CanActivateNext'],
316346
},
347+
{
348+
path: 'load',
349+
loadChildren: 'test',
350+
canLoad: ['CanLoadNext'],
351+
},
317352
]),
318353
StoreRouterConnectingModule,
319354
],
@@ -322,6 +357,10 @@ function createTestModule(
322357
provide: 'CanActivateNext',
323358
useValue: opts.canActivate || (() => true),
324359
},
360+
{
361+
provide: 'CanLoadNext',
362+
useValue: opts.canLoad || (() => true),
363+
},
325364
],
326365
});
327366

modules/router-store/src/router_store_module.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ export class StoreRouterConnectingModule {
185185
}
186186

187187
private navigateIfNeeded(): void {
188-
if (!this.storeState['routerReducer']) return;
188+
if (
189+
!this.storeState['routerReducer'] ||
190+
!this.storeState['routerReducer'].state
191+
) {
192+
return;
193+
}
189194
if (this.dispatchTriggeredByRouter) return;
190195

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

0 commit comments

Comments
 (0)