Skip to content

Commit 261cd95

Browse files
committed
fix: Make the testing adapter clear the URL queue
This caused issues in sequential tests where a test would update a query state, the URL update would be queued, but not flushed at the moment when the test finished, and the next test would then pick up the queued value in its initial state.
1 parent 06d6e39 commit 261cd95

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

packages/nuqs/src/adapters/testing.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createElement, type ReactNode } from 'react'
2+
import { resetQueue } from '../update-queue'
23
import { renderQueryString } from '../url-encoding'
34
import type { AdapterInterface, AdapterOptions } from './defs'
45
import { context } from './internal.context'
@@ -15,10 +16,17 @@ type TestingAdapterProps = {
1516
searchParams?: string | Record<string, string> | URLSearchParams
1617
onUrlUpdate?: OnUrlUpdateFunction
1718
rateLimitFactor?: number
19+
resetUrlUpdateQueueOnMount?: boolean
1820
children: ReactNode
1921
}
2022

21-
export function NuqsTestingAdapter(props: TestingAdapterProps) {
23+
export function NuqsTestingAdapter({
24+
resetUrlUpdateQueueOnMount = true,
25+
...props
26+
}: TestingAdapterProps) {
27+
if (resetUrlUpdateQueueOnMount) {
28+
resetQueue()
29+
}
2230
const useAdapter = (): AdapterInterface => ({
2331
searchParams: new URLSearchParams(props.searchParams),
2432
updateUrl(search, options) {

packages/nuqs/src/update-queue.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ export function getQueuedValue(key: string) {
2525
return updateQueue.get(key)
2626
}
2727

28+
export function resetQueue() {
29+
updateQueue.clear()
30+
transitionsQueue.clear()
31+
queueOptions.history = 'replace'
32+
queueOptions.scroll = false
33+
queueOptions.shallow = true
34+
queueOptions.throttleMs = FLUSH_RATE_LIMIT_MS
35+
}
36+
2837
export function enqueueQueryStringUpdate<Value>(
2938
key: string,
3039
value: Value | null,
@@ -132,12 +141,7 @@ function flushUpdateQueue(
132141
const options = { ...queueOptions }
133142
const transitions = Array.from(transitionsQueue)
134143
// Restore defaults
135-
updateQueue.clear()
136-
transitionsQueue.clear()
137-
queueOptions.history = 'replace'
138-
queueOptions.scroll = false
139-
queueOptions.shallow = true
140-
queueOptions.throttleMs = FLUSH_RATE_LIMIT_MS
144+
resetQueue()
141145
debug('[nuqs queue] Flushing queue %O with options %O', items, options)
142146
for (const [key, value] of items) {
143147
if (value === null) {

0 commit comments

Comments
 (0)