feat(aio): add LocationService.path() method (#16139)

This enables things like embedded components easy access to a clean version
of the current location path.
This commit is contained in:
Peter Bacon Darwin 2017-04-20 16:12:09 +01:00 committed by Miško Hevery
parent 5461182e25
commit 6ed812ff75
3 changed files with 43 additions and 0 deletions

View File

@ -124,6 +124,37 @@ describe('LocationService', () => {
});
});
describe('path', () => {
it('should ask Location for the current path without the hash', () => {
const location: MockLocationStrategy = injector.get(LocationStrategy);
const service: LocationService = injector.get(LocationService);
// We cannot test this directly in the following test because the `MockLocationStrategy`
// does not remove the hash from the path, even if we do pass `false`.
spyOn(location, 'path').and.callThrough();
service.path();
expect(location.path).toHaveBeenCalledWith(false);
});
it('should return the current location.path, with the query and trailing slash stripped off', () => {
const location: MockLocationStrategy = injector.get(LocationStrategy);
const service: LocationService = injector.get(LocationService);
location.simulatePopState('a/b/c?foo=bar&moo=car');
expect(service.path()).toEqual('a/b/c');
location.simulatePopState('c/d/e');
expect(service.path()).toEqual('c/d/e');
location.simulatePopState('a/b/c/?foo=bar&moo=car');
expect(service.path()).toEqual('a/b/c');
location.simulatePopState('c/d/e/');
expect(service.path()).toEqual('c/d/e');
});
});
describe('search', () => {
it('should read the query from the current location.path', () => {
const location: MockLocationStrategy = injector.get(LocationStrategy);

View File

@ -42,6 +42,16 @@ export class LocationService {
return url.replace(/^\/+/, '');
}
/**
* Get the current path, without trailing slash, hash fragment or query params
*/
path(): string {
let path = this.location.path(false);
path = path.match(/[^?]*/)[0]; // strip off query
path = path.replace(/\/$/, ''); // strip off trailing slash
return path;
}
search(): { [index: string]: string; } {
const search = {};
const path = this.location.path();

View File

@ -7,6 +7,8 @@ export class MockLocationService {
setSearch = jasmine.createSpy('setSearch');
go = jasmine.createSpy('Location.go').and
.callFake((url: string) => this.urlSubject.next(url));
path = jasmine.createSpy('Location.path').and
.callFake(() => this.urlSubject.getValue().split('?')[0]);
handleAnchorClick = jasmine.createSpy('Location.handleAnchorClick')
.and.returnValue(false); // prevent click from causing a browser navigation