Skip to content

Commit

Permalink
fix: keep repeated params in query/hash relative locations
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Aug 6, 2020
1 parent 7ddcc2d commit 4fbaa9f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/util/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
134 changes: 96 additions & 38 deletions test/unit/specs/location.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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', () => {
Expand All @@ -62,46 +72,92 @@ 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')
})

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')
})

Expand All @@ -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', () => {
Expand Down

0 comments on commit 4fbaa9f

Please sign in to comment.