Skip to content

Commit d60a2c5

Browse files
committed
test: Update sequencing
1 parent 8d98a7d commit d60a2c5

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

packages/nuqs/src/useQueryState.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,40 @@ describe('useQueryState: clearOnDefault', () => {
160160
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?a=default')
161161
})
162162
})
163+
164+
describe('useQueryState: update sequencing', () => {
165+
it('should combine updates for a single key made in the same event loop tick', async () => {
166+
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
167+
const { result } = renderHook(() => useQueryState('test'), {
168+
wrapper: withNuqsTestingAdapter({
169+
onUrlUpdate
170+
})
171+
})
172+
await act(() => {
173+
result.current[1]('a')
174+
return result.current[1]('b')
175+
})
176+
expect(onUrlUpdate).toHaveBeenCalledOnce()
177+
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?test=b')
178+
})
179+
it('should combine updtes for multiple keys made in the same event loop tick', async () => {
180+
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
181+
const { result } = renderHook(
182+
() => ({
183+
a: useQueryState('a', parseAsString),
184+
b: useQueryState('b', parseAsString)
185+
}),
186+
{
187+
wrapper: withNuqsTestingAdapter({
188+
onUrlUpdate
189+
})
190+
}
191+
)
192+
await act(() => {
193+
result.current.a[1]('a')
194+
return result.current.b[1]('b')
195+
})
196+
expect(onUrlUpdate).toHaveBeenCalledOnce()
197+
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?a=a&b=b')
198+
})
199+
})

packages/nuqs/src/useQueryStates.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,62 @@ describe('useQueryStates: dynamic keys', () => {
382382
expect(result.current[0].z).toBeUndefined()
383383
})
384384
})
385+
386+
describe('useQueryStates: update sequencing', () => {
387+
it('should combine updates for a single key made in the same event loop tick', async () => {
388+
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
389+
const { result } = renderHook(
390+
() => useQueryStates({ test: parseAsString }),
391+
{
392+
wrapper: withNuqsTestingAdapter({
393+
onUrlUpdate
394+
})
395+
}
396+
)
397+
await act(() => {
398+
result.current[1]({ test: 'a' })
399+
return result.current[1]({ test: 'b' })
400+
})
401+
expect(onUrlUpdate).toHaveBeenCalledOnce()
402+
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?test=b')
403+
})
404+
405+
it('should combine updates for multiple keys in the same hook made in the same event loop tick', async () => {
406+
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
407+
const { result } = renderHook(
408+
() => useQueryStates({ a: parseAsString, b: parseAsString }),
409+
{
410+
wrapper: withNuqsTestingAdapter({
411+
onUrlUpdate
412+
})
413+
}
414+
)
415+
await act(() => {
416+
result.current[1]({ a: 'a' })
417+
return result.current[1](() => ({ b: 'b' }))
418+
})
419+
expect(onUrlUpdate).toHaveBeenCalledOnce()
420+
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?a=a&b=b')
421+
})
422+
423+
it('should combine updates for multiple keys in different hook made in the same event loop tick', async () => {
424+
const onUrlUpdate = vi.fn<OnUrlUpdateFunction>()
425+
const { result } = renderHook(
426+
() => ({
427+
a: useQueryStates({ a: parseAsString }),
428+
b: useQueryStates({ b: parseAsString })
429+
}),
430+
{
431+
wrapper: withNuqsTestingAdapter({
432+
onUrlUpdate
433+
})
434+
}
435+
)
436+
await act(() => {
437+
result.current.a[1]({ a: 'a' })
438+
return result.current.b[1](() => ({ b: 'b' }))
439+
})
440+
expect(onUrlUpdate).toHaveBeenCalledOnce()
441+
expect(onUrlUpdate.mock.calls[0]![0].queryString).toEqual('?a=a&b=b')
442+
})
443+
})

0 commit comments

Comments
 (0)