From 4fbaa9f7880276e661227442ef5923131a589210 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 6 Aug 2020 16:58:52 +0200 Subject: [PATCH] fix: keep repeated params in query/hash relative locations --- src/util/location.js | 2 +- test/unit/specs/location.spec.js | 134 ++++++++++++++++++++++--------- 2 files changed, 97 insertions(+), 39 deletions(-) diff --git a/src/util/location.js b/src/util/location.js index fb98d4537..f8df59b46 100644 --- a/src/util/location.js +++ b/src/util/location.js @@ -27,7 +27,7 @@ export function normalizeLocation ( } // relative params - if (!next.path && next.params && current) { + if (!next.path && (next.params || next.query || next.hash) && current) { next = extend({}, next) next._normalized = true const params: any = extend(extend({}, current.params), next.params) diff --git a/test/unit/specs/location.spec.js b/test/unit/specs/location.spec.js index 543e06b9b..66fa82b80 100644 --- a/test/unit/specs/location.spec.js +++ b/test/unit/specs/location.spec.js @@ -7,10 +7,12 @@ describe('Location utils', () => { expect(loc._normalized).toBe(true) expect(loc.path).toBe('/abc') expect(loc.hash).toBe('#hello') - expect(JSON.stringify(loc.query)).toBe(JSON.stringify({ - foo: 'bar', - baz: 'qux' - })) + expect(JSON.stringify(loc.query)).toBe( + JSON.stringify({ + foo: 'bar', + baz: 'qux' + }) + ) }) it('empty string', function () { @@ -36,23 +38,31 @@ describe('Location utils', () => { expect(loc._normalized).toBe(true) expect(loc.path).toBe('/root/abc') expect(loc.hash).toBe('#hello') - expect(JSON.stringify(loc.query)).toBe(JSON.stringify({ - foo: 'bar', - baz: 'qux' - })) + expect(JSON.stringify(loc.query)).toBe( + JSON.stringify({ + foo: 'bar', + baz: 'qux' + }) + ) }) it('relative append', () => { - const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', { - path: '/root/next' - }, true) + const loc = normalizeLocation( + 'abc?foo=bar&baz=qux#hello', + { + path: '/root/next' + }, + true + ) expect(loc._normalized).toBe(true) expect(loc.path).toBe('/root/next/abc') expect(loc.hash).toBe('#hello') - expect(JSON.stringify(loc.query)).toBe(JSON.stringify({ - foo: 'bar', - baz: 'qux' - })) + expect(JSON.stringify(loc.query)).toBe( + JSON.stringify({ + foo: 'bar', + baz: 'qux' + }) + ) }) it('relative query & hash', () => { @@ -62,38 +72,81 @@ describe('Location utils', () => { expect(loc._normalized).toBe(true) expect(loc.path).toBe('/root/next') expect(loc.hash).toBe('#hello') - expect(JSON.stringify(loc.query)).toBe(JSON.stringify({ - foo: 'bar', - baz: 'qux' - })) + expect(JSON.stringify(loc.query)).toBe( + JSON.stringify({ + foo: 'bar', + baz: 'qux' + }) + ) }) it('relative params (named)', () => { - const loc = normalizeLocation({ params: { lang: 'fr' }}, { - name: 'hello', - params: { lang: 'en', id: 'foo' } - }) + const loc = normalizeLocation( + { params: { lang: 'fr' }}, + { + name: 'hello', + params: { lang: 'en', id: 'foo' } + } + ) expect(loc._normalized).toBe(true) expect(loc.name).toBe('hello') expect(loc.params).toEqual({ lang: 'fr', id: 'foo' }) }) it('relative params (non-named)', () => { - const loc = normalizeLocation({ params: { lang: 'fr' }}, { - path: '/en/foo', - params: { lang: 'en', id: 'foo' }, - matched: [{ path: '/:lang(en|fr)/:id' }] - }) + const loc = normalizeLocation( + { params: { lang: 'fr' }}, + { + path: '/en/foo', + params: { lang: 'en', id: 'foo' }, + matched: [{ path: '/:lang(en|fr)/:id' }] + } + ) expect(loc._normalized).toBe(true) expect(loc.path).toBe('/fr/foo') }) + it('relative query named', () => { + const loc = normalizeLocation( + { query: { lang: 'fr' }}, + { + name: 'hello', + hash: '#foo', + params: { id: 'foo' } + } + ) + expect(loc._normalized).toBe(true) + expect(loc.name).toBe('hello') + expect(loc.params).toEqual({ id: 'foo' }) + expect(loc.query).toEqual({ lang: 'fr' }) + expect(loc.hash).toBe(undefined) + }) + + it('relative hash named', () => { + const loc = normalizeLocation( + { hash: '#foo' }, + { + name: 'hello', + query: { lang: 'fr' }, + params: { id: 'foo' } + } + ) + expect(loc._normalized).toBe(true) + expect(loc.name).toBe('hello') + expect(loc.params).toEqual({ id: 'foo' }) + expect(loc.query).toBe(undefined) + expect(loc.hash).toBe('#foo') + }) + it('custom regex can be case insensitive', () => { - const loc = normalizeLocation({ params: { lang: 'FR' }}, { - path: '/en/foo', - params: { lang: 'en', id: 'foo' }, - matched: [{ path: '/:lang(en|fr)/:id' }] - }) + const loc = normalizeLocation( + { params: { lang: 'FR' }}, + { + path: '/en/foo', + params: { lang: 'en', id: 'foo' }, + matched: [{ path: '/:lang(en|fr)/:id' }] + } + ) expect(loc._normalized).toBe(true) expect(loc.path).toBe('/FR/foo') }) @@ -101,7 +154,10 @@ describe('Location utils', () => { it('relative append', () => { const loc = normalizeLocation({ path: 'a' }, { path: '/b' }, true) expect(loc.path).toBe('/b/a') - const loc2 = normalizeLocation({ path: 'a', append: true }, { path: '/b' }) + const loc2 = normalizeLocation( + { path: 'a', append: true }, + { path: '/b' } + ) expect(loc2.path).toBe('/b/a') }) @@ -114,10 +170,12 @@ describe('Location utils', () => { expect(loc._normalized).toBe(true) expect(loc.path).toBe('/abc') expect(loc.hash).toBe('#lol') - expect(JSON.stringify(loc.query)).toBe(JSON.stringify({ - foo: 'bar', - baz: 'qux' - })) + expect(JSON.stringify(loc.query)).toBe( + JSON.stringify({ + foo: 'bar', + baz: 'qux' + }) + ) }) it('skip normalized', () => {