Skip to content

Commit 60f38ea

Browse files
committed
feat(router): add back() support to MockLocationStrategy
1 parent 209aefe commit 60f38ea

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

modules/angular2/src/mock/mock_location_strategy.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ export class MockLocationStrategy extends LocationStrategy {
3131
onPopState(fn: (value: any) => void): void { ObservableWrapper.subscribe(this._subject, fn); }
3232

3333
getBaseHref(): string { return this.internalBaseHref; }
34+
35+
back(): void {
36+
if (this.urlChanges.length > 0) {
37+
this.urlChanges.pop();
38+
var nextUrl = this.urlChanges.length > 0 ? this.urlChanges[this.urlChanges.length - 1] : '';
39+
this.simulatePopState(nextUrl);
40+
}
41+
}
3442
}

modules/angular2/test/router/location_spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,27 @@ export function main() {
8383
.toThrowError(
8484
`No base href set. Either provide a binding to "appBaseHrefToken" or add a base element.`);
8585
});
86+
87+
it('should revert to the previous path when a back() operation is executed', () => {
88+
var locationStrategy = new MockLocationStrategy();
89+
var location = new Location(locationStrategy);
90+
91+
location.go('/ready');
92+
assertUrl('/ready');
93+
94+
location.go('/ready/set');
95+
assertUrl('/ready/set');
96+
97+
location.go('/ready/set/go');
98+
assertUrl('/ready/set/go');
99+
100+
location.back();
101+
assertUrl('/ready/set');
102+
103+
location.back();
104+
assertUrl('/ready');
105+
106+
function assertUrl(path) { expect(location.path()).toEqual(path); }
107+
});
86108
});
87109
}

0 commit comments

Comments
 (0)