Skip to content

Commit

Permalink
fix(useRouteParams,useRouteQuery): set route param/query to undefined…
Browse files Browse the repository at this point in the history
… when null or defaultValue (#3583)

Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
mtdvlpr and antfu committed Feb 20, 2024
1 parent b33ab7b commit c44fea4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/router/useRouteParams/index.test.ts
Expand Up @@ -202,7 +202,7 @@ describe('useRouteParams', () => {
await nextTick()

expect(page.value).toBe(1)
expect(route.params.page).toBe(1)
expect(route.params.page).toBeUndefined()
expect(onUpdate).not.toHaveBeenCalled()
})

Expand Down
4 changes: 2 additions & 2 deletions packages/router/useRouteParams/index.ts
Expand Up @@ -62,8 +62,8 @@ export function useRouteParams<
if (param === v)
return

param = v
_paramsQueue.set(name, v)
param = (v === defaultValue || v === null) ? undefined : v
_paramsQueue.set(name, (v === defaultValue || v === null) ? undefined : v)

trigger()

Expand Down
20 changes: 19 additions & 1 deletion packages/router/useRouteQuery/index.test.ts
Expand Up @@ -222,7 +222,7 @@ describe('useRouteQuery', () => {
await nextTick()

expect(page.value).toBe(1)
expect(route.query.page).toBe(1)
expect(route.query.page).toBeUndefined()
expect(onUpdate).not.toHaveBeenCalled()
})

Expand Down Expand Up @@ -263,4 +263,22 @@ describe('useRouteQuery', () => {
expect(page.value).toBe(2)
expect(lang.value).toBe('en-US')
})

it.each([{ value: 'default' }, { value: null }, { value: undefined }])('should reset value when $value value', async ({ value }) => {
let route = getRoute({
search: 'vue3',
})
const router = { replace: (r: any) => route = r } as any

const search: Ref<any> = useRouteQuery('search', 'default', { route, router })

expect(search.value).toBe('vue3')
expect(route.query.search).toBe('vue3')

search.value = value

await nextTick()

expect(route.query.search).toBeUndefined()
})
})
4 changes: 2 additions & 2 deletions packages/router/useRouteQuery/index.ts
Expand Up @@ -62,8 +62,8 @@ export function useRouteQuery<
if (query === v)
return

query = v
_queriesQueue.set(name, v)
query = (v === defaultValue || v === null) ? undefined : v
_queriesQueue.set(name, (v === defaultValue || v === null) ? undefined : v)

trigger()

Expand Down

0 comments on commit c44fea4

Please sign in to comment.