-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
ref: NuqsTestingAdapter #102387
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
Merged
Merged
ref: NuqsTestingAdapter #102387
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d6fb148
ref: NuqsTestingAdapter
TkDodo cfa72ff
fix: switch to renderQueryString for consistency
TkDodo 0a6b777
Merge branch 'master' into tkdodo/fix/nuqs-testing-adapter
TkDodo a43994c
fix: renderQueryString already includes the `?`
TkDodo 55621d5
Merge branch 'master' into tkdodo/fix/nuqs-testing-adapter
TkDodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import {parseAsString, useQueryState} from 'nuqs'; | ||
|
|
||
| import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary'; | ||
|
|
||
| describe('SentryNuqsTestingAdapter', () => { | ||
| it('reads search params from router location', async () => { | ||
| function TestComponent() { | ||
| const [search] = useQueryState('query', parseAsString); | ||
| return <div>Search: {search ?? 'empty'}</div>; | ||
| } | ||
|
|
||
| const {router} = render(<TestComponent />, { | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/test', | ||
| query: {query: 'hello'}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.getByText('Search: hello')).toBeInTheDocument(); | ||
|
|
||
| // Navigate to a new location with different search params | ||
| router.navigate('/test?query=world'); | ||
|
|
||
| expect(await screen.findByText('Search: world')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('updates router location when nuqs state changes', async () => { | ||
| function TestComponent() { | ||
| const [search, setSearch] = useQueryState('query', parseAsString); | ||
| return ( | ||
| <div> | ||
| <div>Search: {search ?? 'empty'}</div> | ||
| <button onClick={() => setSearch('updated')}>Update</button> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| const {router} = render(<TestComponent />, { | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/test', | ||
| query: {query: 'initial'}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.getByText('Search: initial')).toBeInTheDocument(); | ||
|
|
||
| // Click button to update search param via nuqs | ||
| await userEvent.click(screen.getByRole('button', {name: 'Update'})); | ||
|
|
||
| // Wait for navigation to complete | ||
| await screen.findByText('Search: updated'); | ||
|
|
||
| // Verify the router location was updated | ||
| await waitFor(() => { | ||
| expect(router.location.search).toContain('query=updated'); | ||
| }); | ||
| }); | ||
|
|
||
| it('handles multiple query params', () => { | ||
| function TestComponent() { | ||
| const [foo] = useQueryState('foo', parseAsString); | ||
| const [bar] = useQueryState('bar', parseAsString); | ||
| return ( | ||
| <div> | ||
| <div>Foo: {foo ?? 'empty'}</div> | ||
| <div>Bar: {bar ?? 'empty'}</div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| render(<TestComponent />, { | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/test', | ||
| query: {foo: 'value1', bar: 'value2'}, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.getByText('Foo: value1')).toBeInTheDocument(); | ||
| expect(screen.getByText('Bar: value2')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('handles missing query params', () => { | ||
| function TestComponent() { | ||
| const [search] = useQueryState('query', parseAsString); | ||
| return <div>Search: {search ?? 'empty'}</div>; | ||
| } | ||
|
|
||
| render(<TestComponent />, { | ||
| initialRouterConfig: { | ||
| location: { | ||
| pathname: '/test', | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| expect(screen.getByText('Search: empty')).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import {useCallback, useMemo, type ReactElement, type ReactNode} from 'react'; | ||
| import { | ||
| unstable_createAdapterProvider as createAdapterProvider, | ||
| renderQueryString, | ||
| } from 'nuqs/adapters/custom'; | ||
| import type {unstable_AdapterInterface as AdapterInterface} from 'nuqs/adapters/custom'; | ||
| import type {OnUrlUpdateFunction} from 'nuqs/adapters/testing'; | ||
|
|
||
| import {useLocation} from 'sentry/utils/useLocation'; | ||
| import {useNavigate} from 'sentry/utils/useNavigate'; | ||
|
|
||
| type SentryNuqsTestingAdapterProps = { | ||
| children: ReactNode; | ||
| /** | ||
| * Default options to pass to nuqs | ||
| */ | ||
| defaultOptions?: { | ||
| clearOnDefault?: boolean; | ||
| scroll?: boolean; | ||
| shallow?: boolean; | ||
| }; | ||
| /** | ||
| * A function that will be called whenever the URL is updated. | ||
| * Connect that to a spy in your tests to assert the URL updates. | ||
| */ | ||
| onUrlUpdate?: OnUrlUpdateFunction; | ||
| }; | ||
|
|
||
| /** | ||
| * Custom nuqs adapter component for Sentry that reads location from our | ||
| * useLocation hook instead of maintaining its own internal state. | ||
| * | ||
| * This ensures nuqs uses the same location source as the rest of the | ||
| * application during tests. | ||
| */ | ||
| export function SentryNuqsTestingAdapter({ | ||
| children, | ||
| defaultOptions, | ||
| onUrlUpdate, | ||
| }: SentryNuqsTestingAdapterProps): ReactElement { | ||
| // Create a hook that nuqs will call to get the adapter interface | ||
| // This hook needs to be defined inside a component that has access to location/navigate | ||
| const useSentryAdapter = useCallback( | ||
| (_watchKeys: string[]): AdapterInterface => { | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| const location = useLocation(); | ||
| // eslint-disable-next-line react-hooks/rules-of-hooks | ||
| const navigate = useNavigate(); | ||
|
|
||
| // Get search params from the current location | ||
| const searchParams = new URLSearchParams(location.search || ''); | ||
|
|
||
| const updateUrl: AdapterInterface['updateUrl'] = (search, options) => { | ||
| const newSearchParams = new URLSearchParams(search); | ||
| const queryString = renderQueryString(newSearchParams); | ||
|
|
||
| // Call the onUrlUpdate callback if provided | ||
| onUrlUpdate?.({ | ||
| searchParams: new URLSearchParams(search), // make a copy | ||
| queryString, | ||
| options, | ||
| }); | ||
|
|
||
| // 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; | ||
|
|
||
| // The navigate function from TestRouter already wraps this in act() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is surprisingly true |
||
| navigate(newPath, {replace: options.history === 'replace'}); | ||
| }; | ||
|
|
||
| const getSearchParamsSnapshot = () => { | ||
| // Always read from the current location | ||
| return new URLSearchParams(location.search || ''); | ||
| }; | ||
|
|
||
| return { | ||
| searchParams, | ||
| updateUrl, | ||
| getSearchParamsSnapshot, | ||
| rateLimitFactor: 0, // No throttling in tests | ||
| autoResetQueueOnUpdate: true, // Reset update queue after each update | ||
| }; | ||
| }, | ||
| [onUrlUpdate] | ||
| ); | ||
|
|
||
| // Create the adapter provider (memoized to prevent remounting) | ||
| const AdapterProvider = useMemo( | ||
| () => createAdapterProvider(useSentryAdapter), | ||
| [useSentryAdapter] | ||
| ); | ||
|
|
||
| return <AdapterProvider defaultOptions={defaultOptions}>{children}</AdapterProvider>; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this area was most surprising to me after looking at the result of what was vibe coded
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I tried moving it out but it didn’t work :/
I think it’s good enough like this, I’d appreciate a ✅ so we can merge it, as we already have PRs blocked by this 🙏