Skip to content

Commit

Permalink
fix(useRouteParams, useRouteQuery): effect triggers twice with object…
Browse files Browse the repository at this point in the history
… getter as watch source (vueuse#4283)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
3 people authored Nov 19, 2024
1 parent e77cab1 commit 44772a4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
21 changes: 21 additions & 0 deletions packages/router/useRouteParams/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,27 @@ describe('useRouteParams', () => {
expect(onUpdate).toHaveBeenCalledTimes(1)
})

it('should trigger effects only once with getter object as watch source', async () => {
const route = getRoute({ page: '1' })
const router = { replace: (r: any) => {
Object.keys(r.params).forEach(paramsKey => r.params[paramsKey] = String(r.params[paramsKey]))
return Object.assign(route, r)
} } as any
const onUpdate = vi.fn()

const page = useRouteParams('page', 1, { transform: Number, route, router })

watch(() => ({ page: page.value }), onUpdate)

page.value = 2
await nextTick()
await nextTick()

expect(page.value).toBe(2)
expect(route.params.page).toBe('2')
expect(onUpdate).toHaveBeenCalledTimes(1)
})

it('should keep current query and hash', async () => {
let route = getRoute()
const router = { replace: (r: any) => route = r } as any
Expand Down
2 changes: 1 addition & 1 deletion packages/router/useRouteParams/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function useRouteParams<
watch(
() => route.params[name],
(v) => {
if (param === v)
if (param === transform(v as T))
return

param = v
Expand Down
21 changes: 21 additions & 0 deletions packages/router/useRouteQuery/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ describe('useRouteQuery', () => {
expect(onUpdate).toHaveBeenCalledTimes(1)
})

it('should trigger effects only once with getter object as watch source', async () => {
const route = getRoute({ page: '1' })
const router = { replace: (r: any) => {
Object.keys(r.query).forEach(queryKey => r.query[queryKey] = String(r.query[queryKey]))
return Object.assign(route, r)
} } as any
const onUpdate = vi.fn()

const page = useRouteQuery('page', 1, { transform: Number, route, router })

watch(() => ({ page: page.value }), onUpdate)

page.value = 2
await nextTick()
await nextTick()

expect(page.value).toBe(2)
expect(route.query.page).toBe('2')
expect(onUpdate).toHaveBeenCalledTimes(1)
})

it('should keep current query and hash', async () => {
let route = getRoute()
const router = { replace: (r: any) => route = r } as any
Expand Down
2 changes: 1 addition & 1 deletion packages/router/useRouteQuery/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function useRouteQuery<
watch(
() => route.query[name],
(v) => {
if (query === v)
if (query === transform(v as T))
return

query = v
Expand Down

0 comments on commit 44772a4

Please sign in to comment.