mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
Part 6 (final) of a 6-PR stack migrating all `next/router` and `next/compat/router` imports in `apps/studio` onto TanStack Router hooks and removing the router shims. | Part | PR | Scope | | --- | --- | --- | | 1 | #54053 | Core navigation helpers, `hooks/**`, `lib/**`, `data/prefetchers/**` | | 2 | #54054 | `components/ui/**` | | 3 | #54055 | `components/layouts/**` and `state/tabs.tsx` | | 4 | #54056 | Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | **6** | **#54058 ← this PR** | **Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks** | Now that no files in `apps/studio` (or `packages/**`) import `next/router` or `next/compat/router`, deletes the router shims, `_router-events.ts`, their Vite aliases, and the obsolete router test mocks. `RouteValidationWrapper` is intentionally left for FE-3107. **Removed:** - `apps/studio/compat/next/router.ts`, `apps/studio/compat/next/compat/router.ts`, and `apps/studio/compat/next/_router-events.ts` - `'next/router'` and `'next/compat/router'` entries from `nextShims` in `apps/studio/vite.config.ts` - Global `vi.mock('next/router', ...)` and `vi.mock('next/compat/router', ...)` in `apps/studio/tests/vitestSetup.ts`, `mockNextRouter` in `apps/studio/tests/lib/route-aware-hooks.ts`, and the unused `vi.mock('next/router')` in `tests/features/logs/LogTable.test.tsx` - Obsolete shim tests and test helpers: `compat/next/router.test.ts`, `compat/next/router-navigation.test.tsx`, `tests/config/router.tsx`, and `tests/config/router.test.tsx` **Changed:** - Simplified `createNavigationHandler` in `apps/studio/lib/navigation.ts` (and `navigation.test.ts`) to accept TanStack's `UseNavigateResult<string>` only, dropping the `NextRouter` union branch - Updated `compat/next/link-basepath-navigation.test.tsx` (renamed from `router-basepath-navigation.test.tsx`), `lib/router-url-shape.test.tsx`, and `tests/lib/route-render.test.tsx` to exercise TanStack Router hooks directly instead of the removed router shim - Cleaned up stale `next/router` comments in `compat/next/link.tsx`, `lib/internal-url.ts`, `router.tsx`, and `tests/lib/route-mock.ts` **Simplified:** - Removed `routerMock` setup from about 19 tests that no longer read it. `next-router-mock` stays for now, because the test `FrameworkProvider` still feeds `useParams` from it. - `AGENTS.md` and the `vite.config.ts` error text now say only `next/link` and `next/script` are shimmed - Renamed `compat/next/router-basepath-navigation.test.tsx` to `link-basepath-navigation.test.tsx`, and removed stale router-shim references from comments Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] Confirm `git grep -E "next/(compat/)?router|_router-events" apps/studio` returns no code or config imports (only the prose guideline in `apps/studio/AGENTS.md`) - [ ] Confirm `pnpm --filter studio run typecheck`, `pnpm --filter studio run lint`, `pnpm --filter studio run build`, Knip, and the full Studio Vitest suite pass - [ ] Spot-check basePath navigation (`next/link` + `navigateToUrl`) and multi-line query param URLs in Studio --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
244 lines
7.2 KiB
TypeScript
244 lines
7.2 KiB
TypeScript
import { QueryClient } from '@tanstack/react-query'
|
|
import { act, screen, waitFor } from '@testing-library/react'
|
|
import { platformComponents as components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { parseAsString, useQueryState } from 'nuqs'
|
|
import type { UrlUpdateEvent } from 'nuqs/adapters/testing'
|
|
import { afterEach, beforeEach, describe, expect, test } from 'vitest'
|
|
|
|
import { LayoutSidebarProvider, SIDEBAR_KEYS } from './LayoutSidebarProvider'
|
|
import { projectKeys } from '@/data/projects/keys'
|
|
import { sidebarManagerState } from '@/state/sidebar-manager-state'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
type ProjectDetailResponse = components['schemas']['ProjectDetailResponse_Output']
|
|
|
|
const PROJECT_REF = 'default'
|
|
|
|
const PROJECT: ProjectDetailResponse = {
|
|
cloud_provider: 'AWS',
|
|
connectionString: `postgresql://postgres:password@db.${PROJECT_REF}.supabase.co:5432/postgres`,
|
|
db_host: `db.${PROJECT_REF}.supabase.co`,
|
|
high_availability: false,
|
|
id: 1,
|
|
infra_compute_size: 'micro',
|
|
inserted_at: '2026-01-01T00:00:00.000Z',
|
|
integration_source: null,
|
|
is_branch_enabled: false,
|
|
is_physical_backups_enabled: false,
|
|
name: 'Production',
|
|
organization_id: 1,
|
|
ref: PROJECT_REF,
|
|
region: 'us-east-1',
|
|
restUrl: `https://${PROJECT_REF}.supabase.co`,
|
|
status: 'ACTIVE_HEALTHY',
|
|
subscription_id: 'subscription-1',
|
|
updated_at: '2026-01-01T00:00:00.000Z',
|
|
}
|
|
|
|
const resetSidebarManagerState = () => {
|
|
Object.keys(sidebarManagerState.sidebars).forEach((id) => {
|
|
sidebarManagerState.unregisterSidebar(id)
|
|
})
|
|
sidebarManagerState.closeActive()
|
|
}
|
|
|
|
const SidebarParamProbe = () => {
|
|
const [sidebar] = useQueryState('sidebar', parseAsString)
|
|
return <div data-testid="sidebar-param">{sidebar ?? ''}</div>
|
|
}
|
|
|
|
const createQueryClient = () =>
|
|
new QueryClient({
|
|
defaultOptions: {
|
|
queries: { retry: false },
|
|
mutations: { retry: false },
|
|
},
|
|
})
|
|
|
|
const renderWithSidebarParam = (
|
|
sidebarId: string,
|
|
{
|
|
project,
|
|
projectResponse,
|
|
}: {
|
|
project?: ProjectDetailResponse
|
|
projectResponse?: () => Promise<Response> | Response
|
|
} = {}
|
|
) => {
|
|
const urlUpdates: UrlUpdateEvent[] = []
|
|
const queryClient = createQueryClient()
|
|
if (project) {
|
|
queryClient.setQueryData(projectKeys.detail(PROJECT_REF), project)
|
|
}
|
|
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
response: projectResponse ?? (() => HttpResponse.json<ProjectDetailResponse>(PROJECT)),
|
|
})
|
|
|
|
customRender(
|
|
<LayoutSidebarProvider>
|
|
<SidebarParamProbe />
|
|
</LayoutSidebarProvider>,
|
|
{
|
|
queryClient,
|
|
nuqs: {
|
|
hasMemory: true,
|
|
// the testing adapter otherwise resets the nuqs queue on every render, which drops the write under test.
|
|
resetUrlUpdateQueueOnMount: false,
|
|
searchParams: `?sidebar=${sidebarId}`,
|
|
onUrlUpdate: (event) => {
|
|
urlUpdates.push(event)
|
|
},
|
|
},
|
|
}
|
|
)
|
|
|
|
return { queryClient, urlUpdates }
|
|
}
|
|
|
|
describe('LayoutSidebarProvider sidebar deep link', () => {
|
|
let releaseProject = () => {}
|
|
|
|
beforeEach(() => {
|
|
resetSidebarManagerState()
|
|
localStorage.clear()
|
|
releaseProject = () => {}
|
|
})
|
|
|
|
afterEach(() => {
|
|
releaseProject()
|
|
localStorage.clear()
|
|
})
|
|
|
|
test.each([SIDEBAR_KEYS.ADVISOR_PANEL, SIDEBAR_KEYS.AI_ASSISTANT])(
|
|
'does not clear ?sidebar=%s on initial mount, and clears it after the sidebar closes',
|
|
async (sidebarId) => {
|
|
const { urlUpdates } = renderWithSidebarParam(sidebarId, { project: PROJECT })
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(sidebarId)
|
|
})
|
|
|
|
// nuqs flushes on a later tick even when throttling is disabled
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe(sidebarId)
|
|
expect(urlUpdates).toEqual([])
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe('')
|
|
expect(urlUpdates[0]?.searchParams.has('sidebar')).toBe(false)
|
|
}
|
|
)
|
|
|
|
test('does not clear ?sidebar= when the open sidebar unregisters', async () => {
|
|
const { urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.AI_ASSISTANT, { project: PROJECT })
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
})
|
|
|
|
act(() => {
|
|
sidebarManagerState.unregisterSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
})
|
|
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
|
|
expect(sidebarManagerState.activeSidebar).toBeUndefined()
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe(SIDEBAR_KEYS.AI_ASSISTANT)
|
|
expect(urlUpdates).toEqual([])
|
|
})
|
|
|
|
test('clears ?sidebar= when the sidebar closes before the project loads', async () => {
|
|
const projectReleased = new Promise<void>((resolve) => {
|
|
releaseProject = resolve
|
|
})
|
|
|
|
const { queryClient, urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.ADVISOR_PANEL, {
|
|
projectResponse: async () => {
|
|
await projectReleased
|
|
return HttpResponse.json<ProjectDetailResponse>(PROJECT)
|
|
},
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.ADVISOR_PANEL)
|
|
})
|
|
expect(queryClient.getQueryData(projectKeys.detail(PROJECT_REF))).toBeUndefined()
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
expect(screen.getByTestId('sidebar-param').textContent).toBe('')
|
|
})
|
|
|
|
// guards opened-sidebar tracking being recorded above the !project guard. a later close still has to clear ?sidebar=.
|
|
test('clears ?sidebar= on close when the panel opened before the project loaded', async () => {
|
|
const projectReleased = new Promise<void>((resolve) => {
|
|
releaseProject = resolve
|
|
})
|
|
|
|
const { queryClient, urlUpdates } = renderWithSidebarParam(SIDEBAR_KEYS.ADVISOR_PANEL, {
|
|
projectResponse: async () => {
|
|
await projectReleased
|
|
return HttpResponse.json<ProjectDetailResponse>(PROJECT)
|
|
},
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(sidebarManagerState.activeSidebar?.id).toBe(SIDEBAR_KEYS.ADVISOR_PANEL)
|
|
})
|
|
|
|
await act(async () => {
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
})
|
|
expect(urlUpdates).toEqual([])
|
|
|
|
releaseProject()
|
|
await waitFor(() => {
|
|
expect(queryClient.getQueryData(projectKeys.detail(PROJECT_REF))).toBeTruthy()
|
|
})
|
|
|
|
act(() => {
|
|
sidebarManagerState.closeActive()
|
|
})
|
|
|
|
await waitFor(() => {
|
|
expect(urlUpdates).toEqual([
|
|
expect.objectContaining({
|
|
queryString: '',
|
|
options: expect.objectContaining({ history: 'replace' }),
|
|
}),
|
|
])
|
|
})
|
|
})
|
|
})
|