Skip to content

Commit

Permalink
fix(LocationServices): Apply the hash correctly when a query string i…
Browse files Browse the repository at this point in the history
…s present

Closes #747
  • Loading branch information
christopherthielen authored and mergify[bot] committed Jul 5, 2020
1 parent 1cb4310 commit 0192877
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/location/locationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export class Ng2LocationServices extends BaseLocationServices {

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

if (replace) {
this._locationStrategy.replaceState(state, title, urlWithHash, search);
this._locationStrategy.replaceState(state, title, urlPart, searchPart);
} else {
this._locationStrategy.pushState(state, title, urlWithHash, search);
this._locationStrategy.pushState(state, title, urlPart, searchPart);
}
}

Expand Down
85 changes: 85 additions & 0 deletions test/location/locationService.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { APP_BASE_HREF, HashLocationStrategy, LocationStrategy, PathLocationStrategy } from '@angular/common';
import { UIRouter } from '@uirouter/core';
import { Ng2LocationServices } from '../../src/location/locationService';

@Component({ selector: 'test', template: '' })
class TestComponent {
constructor(public locationStrategy: LocationStrategy) {}
}

type Strategy = typeof HashLocationStrategy | typeof PathLocationStrategy;
describe('locationService', () => {
const setup = (LocationStrategyClass: Strategy) => {
const router = new UIRouter();
const fixture = TestBed.configureTestingModule({
declarations: [TestComponent],
imports: [],
providers: [
{ provide: LocationStrategy, useClass: LocationStrategyClass },
{ provide: APP_BASE_HREF, useValue: '/' },
],
}).createComponent(TestComponent);
fixture.detectChanges();
const locationStrategy = fixture.componentInstance.locationStrategy;

return { router, fixture, locationStrategy };
};

const expectUrlReadAfterWrite = (strategy: Strategy, url: string) => {
const { router, locationStrategy } = setup(HashLocationStrategy);
const locationServices = new Ng2LocationServices(router, locationStrategy, false);

// Set the URL
locationServices._set(null, null, url, false);
// Read it back
expect(locationServices._get()).toBe(url);
// Read it directly from Angular LocationStrategy
expect(locationStrategy.path()).toBe(url);
};

describe('+ HashLocationStrategy', () => {
it('should read/write the url path', () => {
expectUrlReadAfterWrite(HashLocationStrategy, '/foo');
});

it('should read/write query params', () => {
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1');
});

it('should read/write multiple query params', () => {
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query2=value2');
});

it('should read/write multiple query params of the same name', () => {
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2');
});

it('should read/write multiple path + query params + hash', () => {
expectUrlReadAfterWrite(HashLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue');
});
});

describe('+ HashLocationStrategy', () => {
it('should read/write the url path', () => {
expectUrlReadAfterWrite(PathLocationStrategy, '/foo');
});

it('should read/write query params', () => {
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1');
});

it('should read/write multiple query params', () => {
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query2=value2');
});

it('should read/write multiple query params of the same name', () => {
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2');
});

it('should read/write multiple path + query params + hash', () => {
expectUrlReadAfterWrite(PathLocationStrategy, '/foo?query1=value1&query1=value2#hashvalue');
});
});
});

0 comments on commit 0192877

Please sign in to comment.