Skip to content

Commit

Permalink
fix(state): Update URL in response to ignored transition due to redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Aug 28, 2018
1 parent 8ed691b commit c64c252
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/state/stateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class StateService {
*/
const rejectedTransitionHandler = (trans: Transition) => (error: any): Promise<any> => {
if (error instanceof Rejection) {
const isLatest = router.globals.lastStartedTransitionId === trans.$id;
const isLatest = router.globals.lastStartedTransitionId <= trans.$id;

if (error.type === RejectType.IGNORED) {
isLatest && router.urlRouter.update();
Expand Down
35 changes: 35 additions & 0 deletions test/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,41 @@ describe('transition', function() {
router.urlService.url('/urlRedirect');
});

it('should replace the current url when redirecting a url sync transition, even if the redirect is ignored', async done => {
router.stateRegistry.register({
name: 'queryparam',
url: '/?param',
});

await router.stateService.go('queryparam', { param: undefined });
expect(router.globals.params.param).toEqual(undefined);

router.transitionService.onStart({}, trans => {
// redirect transition removing ?param=foo
if (trans.params().param === 'foo') {
return trans.targetState().withParams({ param: undefined });
}
});

router.urlService.url('/?param=foo');
const url = spyOn(router.locationService, 'url').and.callThrough();
const update = spyOn(router.urlRouter, 'update').and.callThrough();

router.transitionService.onError({}, trans => {
if (trans.error().type === RejectType.IGNORED) {
setTimeout(() => {
expect(update.calls.count()).toBe(1);
expect(url.calls.count()).toBe(2);
expect(url.calls.argsFor(0)).toEqual([]);
expect(url.calls.argsFor(1)).toEqual(['/', true]);
expect(router.urlService.url()).toBe('/');
expect(router.globals.params.param).toEqual(undefined);
done();
});
}
});
});

it('should not replace the current url when redirecting a url sync with { location: false }', done => {
router.transitionService.onBefore({ to: 'requiresAuth' }, trans => {
return router.stateService.target('redirectTarget', null, { location: false });
Expand Down

0 comments on commit c64c252

Please sign in to comment.