import { createMemoryHistory, createRootRoute, createRoute, createRouter, Outlet, RouterContextProvider, RouterProvider, type AnyRouter, } from '@tanstack/react-router' import { act, render, renderHook, screen, waitFor } from '@testing-library/react' import { parseAsBoolean, useQueryState } from 'nuqs' import type { ReactNode } from 'react' import { describe, expect, it } from 'vitest' import { buildSearchUpdateArgs, NuqsAdapter } from './nuqs-tanstack-adapter' import { parseSearch, stringifySearch } from './router-search-params' import { createTestRouter } from '@/tests/lib/custom-render' describe('buildSearchUpdateArgs (custom nuqs adapter)', () => { it('targets the current pathname with the search state as a record', () => { // The regression we fixed: the stock tanstack-router adapter navigated to // a relative `?provider=...`, which TanStack resolved by appending — // landing on `/auth/providers/?provider=...` (trailing slash injected). expect(buildSearchUpdateArgs('/auth/providers', new URLSearchParams('provider=apple'))).toEqual( { to: '/auth/providers', search: { provider: 'apple' } } ) }) it('returns an empty search record when every param is cleared', () => { // `search: {}` on TanStack navigate clears every param. expect(buildSearchUpdateArgs('/auth/providers', new URLSearchParams())).toEqual({ to: '/auth/providers', search: {}, }) }) it('keeps values verbatim, including newlines and other control chars', () => { // The second regression we fixed: rendering the query string into the // navigate `to` sent it through TanStack's path interpolation, which // percent-decodes and strips control characters — Logs Explorer's // multi-line SQL in `s` lost every newline (`desc\nlimit 5` became // `desclimit 5`). Passing a search *record* keeps values untouched. const sql = "select *\nfrom logs\nwhere source = 'edge'\norder by timestamp desc\nlimit 5" const params = new URLSearchParams() params.set('s', sql) expect(buildSearchUpdateArgs('/project/abc/logs/explorer', params).search).toEqual({ s: sql }) }) it('preserves repeated keys as arrays', () => { expect( buildSearchUpdateArgs('/advisors/security', new URLSearchParams('f=a&f=b&page=2')).search ).toEqual({ f: ['a', 'b'], page: '2' }) }) it('strips a trailing slash from the pathname, but keeps the root "/"', () => { const empty = new URLSearchParams() expect(buildSearchUpdateArgs('/auth/providers/', empty).to).toBe('/auth/providers') expect(buildSearchUpdateArgs('/', empty).to).toBe('/') expect(buildSearchUpdateArgs('', empty).to).toBe('/') }) }) describe('NuqsAdapter', () => { it('writes to the current route when called through a setter from an earlier render', async () => { const rootRoute = createRootRoute() const routes = ['/project/$ref/settings/general', '/project/$ref/database/settings'].map( (path) => createRoute({ getParentRoute: () => rootRoute, path }) ) const router: AnyRouter = createTestRouter( '/project/abc/settings/general', rootRoute.addChildren(routes) ) await router.load() const wrapper = ({ children }: { children: ReactNode }) => ( {children} ) const { result } = renderHook(() => useQueryState('showConnect', parseAsBoolean), { wrapper }) const [, retainedSetter] = result.current await act(() => router.navigate({ to: '/project/abc/database/settings' })) await act(() => retainedSetter(true)) await waitFor(() => { expect(router.state.location.pathname).toBe('/project/abc/database/settings') expect(router.state.location.search).toEqual({ showConnect: 'true' }) }) }) it('does not re-render a useQueryState consumer when an unwatched search param changes', async () => { let renderCount = 0 function WatchedProbe() { renderCount++ const [watched] = useQueryState('watched') return {watched ?? 'empty'} } const rootRoute = createRootRoute({ component: () => ( ), }) const pageRoute = createRoute({ getParentRoute: () => rootRoute, path: '/items', component: WatchedProbe, }) const router: AnyRouter = createRouter({ routeTree: rootRoute.addChildren([pageRoute]), history: createMemoryHistory({ initialEntries: ['/items?watched=a&other=1'] }), parseSearch, stringifySearch, }) render() expect(await screen.findByTestId('watched')).toHaveTextContent('a') const rendersAfterMount = renderCount await act(async () => { await router.navigate({ to: '/items', search: { watched: 'a', other: '2' }, }) }) expect(renderCount).toBe(rendersAfterMount) await act(async () => { await router.navigate({ to: '/items', search: { watched: 'b', other: '2' }, }) }) await waitFor(() => expect(screen.getByTestId('watched')).toHaveTextContent('b')) }) })