diff --git a/modules/angular2/src/mock/mock_location_strategy.ts b/modules/angular2/src/mock/mock_location_strategy.ts index 6921dc6f00..602dd078e0 100644 --- a/modules/angular2/src/mock/mock_location_strategy.ts +++ b/modules/angular2/src/mock/mock_location_strategy.ts @@ -31,4 +31,12 @@ export class MockLocationStrategy extends LocationStrategy { onPopState(fn: (value: any) => void): void { ObservableWrapper.subscribe(this._subject, fn); } getBaseHref(): string { return this.internalBaseHref; } + + back(): void { + if (this.urlChanges.length > 0) { + this.urlChanges.pop(); + var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : ''; + this.simulatePopState(nextUrl); + } + } } diff --git a/modules/angular2/test/router/location_spec.ts b/modules/angular2/test/router/location_spec.ts index e603e7199e..2273386a24 100644 --- a/modules/angular2/test/router/location_spec.ts +++ b/modules/angular2/test/router/location_spec.ts @@ -83,5 +83,27 @@ export function main() { .toThrowError( `No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`); }); + + it('should revert to the previous path when a back() operation is executed', () => { + var locationStrategy = new MockLocationStrategy(); + var location = new Location(locationStrategy); + + location.go('/ready'); + assertUrl('/ready'); + + location.go('/ready/set'); + assertUrl('/ready/set'); + + location.go('/ready/set/go'); + assertUrl('/ready/set/go'); + + location.back(); + assertUrl('/ready/set'); + + location.back(); + assertUrl('/ready'); + + function assertUrl(path) { expect(location.path()).toEqual(path); } + }); }); }