Skip to content

Commit

Permalink
fix(encoding): decode hash in string location
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Oct 2, 2020
1 parent 1a8ffc1 commit 11acb3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
21 changes: 18 additions & 3 deletions __tests__/urlEncoding.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,16 @@ describe('URL Encoding', () => {
it('calls decode with a path', async () => {
const router = createRouter()
await router.push('/p/foo')
expect(encoding.decode).toHaveBeenCalledTimes(1)
// one extra time for hash
expect(encoding.decode).toHaveBeenCalledTimes(2)
expect(encoding.decode).toHaveBeenNthCalledWith(1, 'foo')
})

it('calls decode with a path with repeatable params', async () => {
const router = createRouter()
await router.push('/p/foo/bar')
expect(encoding.decode).toHaveBeenCalledTimes(2)
// one extra time for hash
expect(encoding.decode).toHaveBeenCalledTimes(3)
expect(encoding.decode).toHaveBeenNthCalledWith(1, 'foo', 0, ['foo', 'bar'])
expect(encoding.decode).toHaveBeenNthCalledWith(2, 'bar', 1, ['foo', 'bar'])
})
Expand Down Expand Up @@ -102,7 +104,8 @@ describe('URL Encoding', () => {
it('calls decode with query', async () => {
const router = createRouter()
await router.push('/?p=foo')
expect(encoding.decode).toHaveBeenCalledTimes(2)
// one extra time for hash
expect(encoding.decode).toHaveBeenCalledTimes(3)
expect(encoding.decode).toHaveBeenNthCalledWith(1, 'p')
expect(encoding.decode).toHaveBeenNthCalledWith(2, 'foo')
})
Expand Down Expand Up @@ -141,4 +144,16 @@ describe('URL Encoding', () => {
hash: '#%',
})
})
it('decodes hash', async () => {
// @ts-ignore: override to make the difference
encoding.decode = () => '#d'
// @ts-ignore
encoding.encodeHash = () => '#e'
const router = createRouter()
await router.push('#%20')
expect(router.currentRoute.value).toMatchObject({
fullPath: '/#%20',
hash: '#d',
})
})
})
1 change: 1 addition & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export function createRouter(options: RouterOptions): Router {
// locationNormalized is always a new object
return assign(locationNormalized, matchedRoute, {
params: decodeParams(matchedRoute.params),
hash: decode(locationNormalized.hash),
redirectedFrom: undefined,
href,
})
Expand Down

0 comments on commit 11acb3d

Please sign in to comment.