Skip to content

Commit

Permalink
fix: Make the testing adapter clear the URL queue (#764)
Browse files Browse the repository at this point in the history
  • Loading branch information
franky47 authored Nov 14, 2024
1 parent 06d6e39 commit 8200add
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
10 changes: 9 additions & 1 deletion packages/nuqs/src/adapters/testing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createElement, type ReactNode } from 'react'
import { resetQueue } from '../update-queue'
import { renderQueryString } from '../url-encoding'
import type { AdapterInterface, AdapterOptions } from './defs'
import { context } from './internal.context'
Expand All @@ -15,10 +16,17 @@ type TestingAdapterProps = {
searchParams?: string | Record<string, string> | URLSearchParams
onUrlUpdate?: OnUrlUpdateFunction
rateLimitFactor?: number
resetUrlUpdateQueueOnMount?: boolean
children: ReactNode
}

export function NuqsTestingAdapter(props: TestingAdapterProps) {
export function NuqsTestingAdapter({
resetUrlUpdateQueueOnMount = true,
...props
}: TestingAdapterProps) {
if (resetUrlUpdateQueueOnMount) {
resetQueue()
}
const useAdapter = (): AdapterInterface => ({
searchParams: new URLSearchParams(props.searchParams),
updateUrl(search, options) {
Expand Down
16 changes: 10 additions & 6 deletions packages/nuqs/src/update-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ export function getQueuedValue(key: string) {
return updateQueue.get(key)
}

export function resetQueue() {
updateQueue.clear()
transitionsQueue.clear()
queueOptions.history = 'replace'
queueOptions.scroll = false
queueOptions.shallow = true
queueOptions.throttleMs = FLUSH_RATE_LIMIT_MS
}

export function enqueueQueryStringUpdate<Value>(
key: string,
value: Value | null,
Expand Down Expand Up @@ -132,12 +141,7 @@ function flushUpdateQueue(
const options = { ...queueOptions }
const transitions = Array.from(transitionsQueue)
// Restore defaults
updateQueue.clear()
transitionsQueue.clear()
queueOptions.history = 'replace'
queueOptions.scroll = false
queueOptions.shallow = true
queueOptions.throttleMs = FLUSH_RATE_LIMIT_MS
resetQueue()
debug('[nuqs queue] Flushing queue %O with options %O', items, options)
for (const [key, value] of items) {
if (value === null) {
Expand Down

0 comments on commit 8200add

Please sign in to comment.