Files
supabase/apps/studio/components/layouts/editors/EditorBaseLayout.test.tsx
Alaister YoungandAlaister Young 953e508a3e [FE-5110] refactor(studio): replace next/navigation with TanStack (#54041)
Moves the remaining `next/navigation` imports in Studio onto TanStack
Router hooks (and `useParams` from `common`), finishes migrating the
remaining `next/link` and `next/router` usages in the touched
components, and removes the `compat/next/navigation.ts` shim and its
Vite alias. Shared packages (`packages/**`) no longer import
`next/navigation`, so the alias can be dropped now.

**Removed:**

- `apps/studio/compat/next/navigation.ts` and the `'next/navigation'`
entry in `apps/studio/vite.config.ts`
- Global `next/navigation` mock in `apps/studio/tests/vitestSetup.ts`
and the obsolete `apps/studio/tests/lib/route-mock.test.tsx`

**Changed:**

- Replaced `usePathname` (12 files) and `useSearchParams` (3 files) with
`useLocation({ select })` from `@tanstack/react-router`
- Replaced `useRouter` in Storage bucket tables, SQL Editor commands,
and `SignInMfaForm` with `useNavigate` from `@tanstack/react-router`,
and updated `createNavigationHandler` in `lib/navigation.ts` to accept
either `NextRouter` or TanStack's `useNavigate`
- Replaced remaining `next/link` imports in touched components
(`SignInMfaForm`, `Restriction`, `DownloadLogsButton`,
`DownloadResultsButton`, `AuthEmailsLayout`, `AuthProvidersLayout`,
`JWTKeysLayout`, `StorageBucketsLayout`) with TanStack Router's `Link`
- Replaced `useParams` in `CreatePublishableAPIKeyDialog` with
`useParams` from `common` (matching `CreateSecretAPIKeyDialog`)
- Wrapped `CustomWrapper` (`tests/lib/custom-render.tsx`) in a default
TanStack `RouterContextProvider` (with optional `router` / `initialUrl`
overrides) and updated affected component tests to drop their
`next/navigation` mocks

Addresses
[FE-5110](https://linear.app/supabase/issue/FE-5110/replace-the-nextnavigation-shim-with-tanstack-router-hooks)

## To test

1. Check active tab highlighting and navigation across Auth Emails, Auth
Providers, JWT Keys, Storage Buckets, and Editor layouts
2. Verify clicking, Cmd/Ctrl-clicking, middle-clicking, and pressing
Enter on Storage bucket rows (Files, Analytics, Vector) navigates as
before
3. Run "Query a table..." and open SQL snippets from the Command Menu
and confirm `/project/$ref/sql/$id` opens with multi-line SQL preserved
in `?content=`
4. In the MFA sign-in flow, verify the recovery code link and
`forgot-password` completion preserve query params (`returnTo`,
`context=forgot-password`), and Cancel signs out and replaces history
with `/sign-in`
5. Confirm Table Editor sort/filter URL sync persists filters per table
without leaking across table switches

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-26 17:18:49 +08:00

126 lines
2.9 KiB
TypeScript

import {
createMemoryHistory,
createRootRoute,
createRouter,
RouterContextProvider,
} from '@tanstack/react-router'
import { render, screen } from '@testing-library/react'
import type { ReactNode } from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { EditorBaseLayout } from './EditorBaseLayout'
const { mockTabsState, mockEditorType } = vi.hoisted(() => ({
mockTabsState: {
activeTab: 'r-1',
openTabs: ['r-1'],
tabsMap: {
'r-1': {
id: 'r-1',
type: 'r',
label: 'routines',
metadata: {
name: 'tasks',
schema: 'public',
tableId: 1,
},
},
},
} as any,
mockEditorType: vi.fn(),
}))
vi.mock('common', () => ({
useParams: () => ({ ref: 'default' }),
}))
vi.mock('@/state/tabs', () => ({
useTabsStateSnapshot: () => mockTabsState,
}))
vi.mock('ui', () => ({
cn: (...classes: Array<string | false | null | undefined>) => classes.filter(Boolean).join(' '),
}))
vi.mock('../ProjectLayout', () => ({
ProjectLayoutWithAuth: ({
browserTitle,
children,
}: {
browserTitle?: Record<string, string>
children: ReactNode
}) => (
<div>
<div data-testid="browser-title">{JSON.stringify(browserTitle ?? {})}</div>
{children}
</div>
),
}))
vi.mock('../Tabs/CollapseButton', () => ({
CollapseButton: () => null,
}))
vi.mock('../Tabs/Tabs', () => ({
EditorTabs: () => null,
}))
vi.mock('./EditorsLayout.hooks', () => ({
useEditorType: () => mockEditorType(),
}))
describe('EditorBaseLayout browser title', () => {
beforeEach(() => {
mockEditorType.mockReturnValue('table')
mockTabsState.activeTab = 'r-1'
mockTabsState.openTabs = ['r-1']
mockTabsState.tabsMap = {
'r-1': {
id: 'r-1',
type: 'r',
label: 'routines',
metadata: {
name: 'tasks',
schema: 'public',
tableId: 1,
},
},
}
})
const renderEditorLayout = (initialUrl = '/project/default/editor/1') => {
const router = createRouter({
routeTree: createRootRoute(),
history: createMemoryHistory({ initialEntries: [initialUrl] }),
})
return render(
<RouterContextProvider router={router}>
<EditorBaseLayout title="Tables" product="Table Editor">
<div>Page content</div>
</EditorBaseLayout>
</RouterContextProvider>
)
}
it('prefers the live tab label over stale metadata when composing the title entity', () => {
renderEditorLayout()
expect(JSON.parse(screen.getByTestId('browser-title').textContent ?? '{}')).toEqual({
section: 'Tables',
entity: 'routines',
})
})
it('falls back to metadata when a tab does not have a label yet', () => {
mockTabsState.tabsMap['r-1'].label = undefined
renderEditorLayout()
expect(JSON.parse(screen.getByTestId('browser-title').textContent ?? '{}')).toEqual({
section: 'Tables',
entity: 'tasks',
})
})
})