Skip to content

Commit

Permalink
feat(router): support custom parseQuery and stringifyQuery (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
BboyAkers committed Mar 17, 2020
1 parent c6c2b6f commit 5dce7bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
22 changes: 19 additions & 3 deletions __tests__/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
RouteLocation,
START_LOCATION_NORMALIZED,
} from '../src/types'
import { RouterHistory } from '../src/history/common'

const routes: RouteRecord[] = [
{ path: '/', component: components.Home, name: 'home' },
Expand Down Expand Up @@ -56,9 +55,12 @@ const routes: RouteRecord[] = [
},
]

async function newRouter({ history }: { history?: RouterHistory } = {}) {
async function newRouter({
history,
...args
}: Partial<Parameters<typeof createRouter>[0]> = {}) {
history = history || createMemoryHistory()
const router = createRouter({ history, routes })
const router = createRouter({ history, routes, ...args })
await router.push('/')

return { history, router }
Expand Down Expand Up @@ -90,6 +92,20 @@ describe('Router', () => {
)
})

it('can allows the end user to override parseQuery', async () => {
const parseQuery = jest.fn()
const { router } = await newRouter({ parseQuery: parseQuery })
router.resolve('/foo?bar=baz')
expect(parseQuery).toHaveBeenCalled()
})

it('can allows the end user to stringify the query', async () => {
const stringifyQuery = jest.fn()
const { router } = await newRouter({ stringifyQuery: stringifyQuery })
router.resolve({ query: { foo: 'bar' } })
expect(stringifyQuery).toHaveBeenCalled()
})

it('can do initial navigation to /', async () => {
const router = createRouter({
history: createMemoryHistory(),
Expand Down
10 changes: 9 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import {
} from './utils'
import { useCallbacks } from './utils/callbacks'
import { encodeParam, decode } from './utils/encoding'
import { normalizeQuery, parseQuery, stringifyQuery } from './utils/query'
import {
normalizeQuery,
parseQuery as originalParseQuery,
stringifyQuery as originalStringifyQuery,
} from './utils/query'
import { ref, Ref, markNonReactive, nextTick, App, warn } from 'vue'
import { RouteRecordNormalized } from './matcher/types'
import { Link } from './components/Link'
Expand All @@ -55,6 +59,8 @@ export interface RouterOptions {
history: RouterHistory
routes: RouteRecord[]
scrollBehavior?: ScrollBehavior
parseQuery?: typeof originalParseQuery
stringifyQuery?: typeof originalStringifyQuery
// TODO: allow customizing encoding functions
}

Expand Down Expand Up @@ -87,6 +93,8 @@ export function createRouter({
history,
routes,
scrollBehavior,
parseQuery = originalParseQuery,
stringifyQuery = originalStringifyQuery,
}: RouterOptions): Router {
const matcher = createRouterMatcher(routes, {})

Expand Down

0 comments on commit 5dce7bc

Please sign in to comment.