Skip to content

Commit 0192877

Browse files
christopherthielenmergify[bot]
authored andcommitted
fix(LocationServices): Apply the hash correctly when a query string is present
Closes #747
1 parent 1cb4310 commit 0192877

File tree

2 files changed

+89
-3
lines changed

2 files changed

+89
-3
lines changed

src/location/locationService.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ export class Ng2LocationServices extends BaseLocationServices {
1919

2020
_set(state: any, title: string, url: string, replace: boolean): any {
2121
const { path, search, hash } = parseUrl(url);
22-
const urlWithHash = path + (hash ? '#' + hash : '');
22+
const urlPart = search ? path : path + (hash ? '#' + hash : '');
23+
const searchPart = search + (hash ? '#' + hash : '');
2324

2425
if (replace) {
25-
this._locationStrategy.replaceState(state, title, urlWithHash, search);
26+
this._locationStrategy.replaceState(state, title, urlPart, searchPart);
2627
} else {
27-
this._locationStrategy.pushState(state, title, urlWithHash, search);
28+
this._locationStrategy.pushState(state, title, urlPart, searchPart);
2829
}
2930
}
3031

test/location/locationService.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Component } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { APP_BASE_HREF, HashLocationStrategy, LocationStrategy, PathLocationStrategy } from '@angular/common';
4+
import { UIRouter } from '@uirouter/core';
5+
import { Ng2LocationServices } from '../../src/location/locationService';
6+
7+
@Component({ selector: 'test', template: '' })
8+
class TestComponent {
9+
constructor(public locationStrategy: LocationStrategy) {}
10+
}
11+
12+
type Strategy = typeof HashLocationStrategy | typeof PathLocationStrategy;
13+
describe('locationService', () => {
14+
const setup = (LocationStrategyClass: Strategy) => {
15+
const router = new UIRouter();
16+
const fixture = TestBed.configureTestingModule({
17+
declarations: [TestComponent],
18+
imports: [],
19+
providers: [
20+
{ provide: LocationStrategy, useClass: LocationStrategyClass },
21+
{ provide: APP_BASE_HREF, useValue: '/' },
22+
],
23+
}).createComponent(TestComponent);
24+
fixture.detectChanges();
25+
const locationStrategy = fixture.componentInstance.locationStrategy;
26+
27+
return { router, fixture, locationStrategy };
28+
};
29+
30+
const expectUrlReadAfterWrite = (strategy: Strategy, url: string) => {
31+
const { router, locationStrategy } = setup(HashLocationStrategy);
32+
const locationServices = new Ng2LocationServices(router, locationStrategy, false);
33+
34+
// Set the URL
35+
locationServices._set(null, null, url, false);
36+
// Read it back
37+
expect(locationServices._get()).toBe(url);
38+
// Read it directly from Angular LocationStrategy
39+
expect(locationStrategy.path()).toBe(url);
40+
};
41+
42+
describe('+ HashLocationStrategy', () => {
43+
it('should read/write the url path', () => {
44+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo');
45+
});
46+
47+
it('should read/write query params', () => {
48+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1');
49+
});
50+
51+
it('should read/write multiple query params', () => {
52+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query2=value2');
53+
});
54+
55+
it('should read/write multiple query params of the same name', () => {
56+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2');
57+
});
58+
59+
it('should read/write multiple path + query params + hash', () => {
60+
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue');
61+
});
62+
});
63+
64+
describe('+ HashLocationStrategy', () => {
65+
it('should read/write the url path', () => {
66+
expectUrlReadAfterWrite(PathLocationStrategy, '/foo');
67+
});
68+
69+
it('should read/write query params', () => {
70+
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1');
71+
});
72+
73+
it('should read/write multiple query params', () => {
74+
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query2=value2');
75+
});
76+
77+
it('should read/write multiple query params of the same name', () => {
78+
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2');
79+
});
80+
81+
it('should read/write multiple path + query params + hash', () => {
82+
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue');
83+
});
84+
});
85+
});

0 commit comments

Comments
 (0)