Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make the testing adapter clear the URL queue #764

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading