From 042a9500d5dd80b6ce5ce1d4a7e6b2c6756cbcde Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Wed, 20 Dec 2017 14:33:05 -0800 Subject: [PATCH] fix(pushStateLocation): When url is "" or "/", use baseHref for pushState --- src/vanilla/pushStateLocationService.ts | 2 +- test/vanilla.pushStateLocationServiceSpec.ts | 50 +++++++++++--------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/vanilla/pushStateLocationService.ts b/src/vanilla/pushStateLocationService.ts index 52bb24fc..b3ac6a09 100644 --- a/src/vanilla/pushStateLocationService.ts +++ b/src/vanilla/pushStateLocationService.ts @@ -57,7 +57,7 @@ export class PushStateLocationService extends BaseLocationServices { protected _set(state: any, title: string, url: string, replace: boolean) { const basePrefix = this._getBasePrefix(); const slash = url && url[0] !== '/' ? '/' : ''; - const fullUrl = url === '' ? this._config.baseHref() : basePrefix + slash + url; + const fullUrl = (url === '' || url === '/') ? this._config.baseHref() : basePrefix + slash + url; if (replace) { this._history.replaceState(state, title, fullUrl); diff --git a/test/vanilla.pushStateLocationServiceSpec.ts b/test/vanilla.pushStateLocationServiceSpec.ts index b0efc189..fe8387d5 100644 --- a/test/vanilla.pushStateLocationServiceSpec.ts +++ b/test/vanilla.pushStateLocationServiceSpec.ts @@ -40,15 +40,17 @@ describe('pushStateLocationService', () => { expect($url.search()).toEqual({}); }); - it('sets and returns an empty path', () => { + it('returns a slash when an empty path is set', () => { + const base = $url.config.baseHref(); $url.url(''); - expect(window.location.pathname).toBe(''); - expect($url.path()).toBe(''); + expect(window.location.pathname).toBe(base); + expect($url.path()).toBe('/'); }); it('sets and returns a path with a single slash', () => { + const base = $url.config.baseHref(); $url.url('/'); - expect(window.location.pathname).toBe('/'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -60,7 +62,7 @@ describe('pushStateLocationService', () => { expect($url.search()).toEqual({ queryParam: 'query' }); }); - fdescribe('with base tag', () => { + describe('with base tag', () => { let baseTag: HTMLBaseElement; const applyBaseTag = (href: string) => { baseTag = document.createElement('base'); @@ -71,7 +73,8 @@ describe('pushStateLocationService', () => { afterEach(() => baseTag.parentElement.removeChild(baseTag)); describe('/base/', () => { - beforeEach(() => applyBaseTag("/base/")); + const base = '/base/'; + beforeEach(() => applyBaseTag(base)); it('reports html5Mode to be true', () => { expect(router.urlService.config.html5Mode()).toBe(true); @@ -79,39 +82,39 @@ describe('pushStateLocationService', () => { it('handles bar correctly', () => { $url.url('bar'); - expect(window.location.pathname).toBe('/base/bar'); + expect(window.location.pathname).toBe(`${base}bar`); expect($url.path()).toBe('/bar'); }); it('handles /bar correctly', () => { $url.url('/bar'); - expect(window.location.pathname).toBe('/base/bar'); + expect(window.location.pathname).toBe(`${base}bar`); expect($url.path()).toBe('/bar'); }); it('handles /path/bar correctly', () => { $url.url('/path/bar'); - expect(window.location.pathname).toBe('/base/path/bar'); + expect(window.location.pathname).toBe(`${base}path/bar`); expect($url.path()).toBe('/path/bar'); }); it('handles / correctly', () => { $url.url('/'); - expect(window.location.pathname).toBe('/base/'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); it('handles "" correctly', () => { $url.url('foobar'); - expect(window.location.pathname).toBe('/base/foobar'); + expect(window.location.pathname).toBe(`${base}foobar`); $url.url(''); - expect(window.location.pathname).toBe('/base/'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); it('handles ?queryParam=query correctly', () => { $url.url('/path/bar?queryParam=query'); - expect(window.location.pathname).toBe('/base/path/bar'); + expect(window.location.pathname).toBe(`${base}path/bar`); expect(window.location.search).toBe('?queryParam=query'); expect($url.path()).toBe('/path/bar'); expect($url.search()).toEqual({ queryParam: 'query' }); @@ -119,7 +122,8 @@ describe('pushStateLocationService', () => { }); describe('/debug.html', () => { - beforeEach(() => applyBaseTag("/debug.html")); + const base = "/debug.html"; + beforeEach(() => applyBaseTag(base)); it('handles bar correctly', () => { $url.url('bar'); @@ -141,7 +145,7 @@ describe('pushStateLocationService', () => { it('handles / correctly', () => { $url.url('/'); - expect(window.location.pathname).toBe('/debug.html'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -149,7 +153,7 @@ describe('pushStateLocationService', () => { $url.url('foobar'); expect(window.location.pathname).toBe('/foobar'); $url.url(''); - expect(window.location.pathname).toBe('/debug.html'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -163,7 +167,8 @@ describe('pushStateLocationService', () => { }); describe(origin + '/debug.html', () => { - beforeEach(() => applyBaseTag(origin + '/debug.html')); + const base = '/debug.html'; + beforeEach(() => applyBaseTag(origin + base)); it('handles bar correctly', () => { $url.url('bar'); @@ -185,7 +190,7 @@ describe('pushStateLocationService', () => { it('handles / correctly', () => { $url.url('/'); - expect(window.location.pathname).toBe('/debug.html'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -193,7 +198,7 @@ describe('pushStateLocationService', () => { $url.url('foobar'); expect(window.location.pathname).toBe('/foobar'); $url.url(''); - expect(window.location.pathname).toBe('/debug.html'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -207,7 +212,8 @@ describe('pushStateLocationService', () => { }); describe(origin + '/base/debug.html', () => { - beforeEach(() => applyBaseTag(origin + '/base/debug.html')); + const base = '/base/debug.html'; + beforeEach(() => applyBaseTag(origin + base)); it('handles bar correctly', () => { $url.url('bar'); @@ -229,7 +235,7 @@ describe('pushStateLocationService', () => { it('handles / correctly', () => { $url.url('/'); - expect(window.location.pathname).toBe('/base/'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); }); @@ -237,7 +243,7 @@ describe('pushStateLocationService', () => { $url.url('foobar'); expect(window.location.pathname).toBe('/base/foobar'); $url.url(''); - expect(window.location.pathname).toBe('/base/debug.html'); + expect(window.location.pathname).toBe(base); expect($url.path()).toBe('/'); });