Skip to content
Merged
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
14 changes: 10 additions & 4 deletions tests/js/sentry-test/nuqsTestingAdapter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useCallback, useMemo, type ReactElement, type ReactNode} from 'react';
import {useCallback, useMemo, useRef, type ReactElement, type ReactNode} from 'react';
import {
unstable_createAdapterProvider as createAdapterProvider,
renderQueryString,
Expand Down Expand Up @@ -47,6 +47,12 @@ export function SentryNuqsTestingAdapter({
// eslint-disable-next-line react-hooks/rules-of-hooks
const navigate = useNavigate();

// nuqs flushes updates on a deferred tick, so read location through a ref
// to compose onto the live URL instead of a stale render snapshot.
// eslint-disable-next-line react-hooks/rules-of-hooks
const locationRef = useRef(location);
locationRef.current = location;

// Get search params from the current location
const searchParams = new URLSearchParams(location.search || '');

Expand All @@ -64,16 +70,16 @@ export function SentryNuqsTestingAdapter({
// Navigate to the new location using Sentry's navigate
// We need to construct the full path with the search string
const newPath = queryString
? `${location.pathname}${queryString}`
: location.pathname;
? `${locationRef.current.pathname}${queryString}`
: locationRef.current.pathname;

// The navigate function from TestRouter already wraps this in act()
navigate(newPath, {replace: options.history === 'replace'});
};

const getSearchParamsSnapshot = () => {
// Always read from the current location
return new URLSearchParams(location.search || '');
return new URLSearchParams(locationRef.current.search || '');
};

return {
Expand Down
Loading