Skip to content

Commit

Permalink
fix(redirect): Do not update URL after redirect with { location: false }
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen committed Mar 22, 2017
1 parent afdeec2 commit 652a760
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/transition/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface TransitionOptions {
*
* - If `true`, it will update the url in the location bar.
* - If `false`, it will not update the url in the location bar.
* - If it is the string "`replace`", it will update the url and also replace the last history record.
* - If it is the string `"replace"`, it will update the url and also replace the last history record.
*
* @default `true`
*/
Expand Down
8 changes: 5 additions & 3 deletions src/transition/transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { services } from '../common/coreservices';
import {
map, find, extend, mergeR, tail, omit, toJson, arrayTuples, unnestR, identity, anyTrueR
} from '../common/common';
import { isObject } from '../common/predicates';
import { isObject, isUndefined } from '../common/predicates';
import { prop, propEq, val, not, is } from '../common/hof';
import { StateDeclaration, StateOrName } from '../state/interface';
import {
Expand Down Expand Up @@ -517,8 +517,10 @@ export class Transition implements IHookRegistry {

let redirectOpts: TransitionOptions = { redirectedFrom: this, source: "redirect" };
// If the original transition was caused by URL sync, then use { location: 'replace' }
// on the new transition (unless the target state explicitly specifies location)
if (this.options().source === 'url') {
// on the new transition (unless the target state explicitly specifies location: false).
// This causes the original url to be replaced with the url for the redirect target
// so the original url disappears from the browser history.
if (this.options().source === 'url' && targetState.options().location !== false) {
redirectOpts.location = 'replace';
}

Expand Down
29 changes: 28 additions & 1 deletion test/transitionSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,10 @@ describe('transition', function () {
});

describe('redirected transition', () => {
let urlRedirect;
let urlRedirect, requiresAuth;
beforeEach(() => {
urlRedirect = router.stateRegistry.register({ name: 'urlRedirect', url: '/urlRedirect', redirectTo: 'redirectTarget' });
requiresAuth = router.stateRegistry.register({ name: 'requiresAuth', url: '/requiresAuth' });
router.stateRegistry.register({ name: 'redirectTarget', url: '/redirectTarget' });
});

Expand All @@ -703,6 +704,9 @@ describe('transition', function () {
router.transitionService.onSuccess({}, () => {
expect(transitionTo).toHaveBeenCalledWith(urlRedirect, {}, { inherit: true, source: 'url' });

expect(router.stateService.current.name).toBe('redirectTarget');
expect(router.urlService.path()).toBe('/redirectTarget');

expect(url.calls.count()).toEqual(2);
expect(url.calls.argsFor(0)).toEqual(["/urlRedirect"]);
expect(url.calls.argsFor(1)).toEqual(["/redirectTarget", true]);
Expand All @@ -713,6 +717,29 @@ describe('transition', function () {
router.urlService.url('/urlRedirect');
});

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 })
});

let url = spyOn(router.urlService, "url").and.callThrough();
let transitionTo = spyOn(router.stateService, "transitionTo").and.callThrough();

router.transitionService.onSuccess({}, () => {
expect(transitionTo).toHaveBeenCalledWith(requiresAuth, {}, { inherit: true, source: 'url' });

expect(router.globals.current.name).toBe("redirectTarget");
expect(router.urlService.path()).toBe('/requiresAuth');

expect(url.calls.count()).toEqual(1);
expect(url.calls.argsFor(0)).toEqual(["/requiresAuth"]);

done();
});

router.urlService.url('/requiresAuth');
});

});
});

Expand Down

0 comments on commit 652a760

Please sign in to comment.