Files
Alaister YoungandAlaister Young 477e2c136f [FE-5109] (part 6) chore(studio): remove next/router shims, aliases, and test mocks (#54058)
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>
2026-12-01 15:04:27 +08:00

179 lines
5.4 KiB
TypeScript

import { act, screen, waitFor } from '@testing-library/react'
import { ResizablePanel, ResizablePanelGroup } from 'ui'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { LayoutSidebar } from './index'
import { LayoutSidebarProvider, SIDEBAR_KEYS } from './LayoutSidebarProvider'
import { MobileSheetProvider } from '@/components/layouts/Navigation/NavigationBar/MobileSheetContext'
import { sidebarManagerState } from '@/state/sidebar-manager-state'
import { render } from '@/tests/helpers'
vi.mock('@/components/ui/AIAssistantPanel/AIAssistant', () => ({
AIAssistant: () => <div data-testid="ai-assistant-sidebar">AI Assistant</div>,
}))
vi.mock('@/components/ui/EditorPanel/EditorPanel', () => ({
EditorPanel: () => <div data-testid="editor-panel-sidebar">Editor Panel</div>,
}))
vi.mock('@/components/ui/AdvisorPanel/AdvisorPanel', () => ({
AdvisorPanel: () => <div data-testid="advisor-panel-sidebar">Advisor Panel</div>,
}))
vi.mock('nuqs', async () => {
let queryValue = 'ai-assistant'
return {
useQueryState: () => [queryValue, (v: string) => (queryValue = v)],
parseAsString: () => {},
}
})
const mockProject = {
id: 1,
ref: 'default',
name: 'Project 1',
status: 'ACTIVE_HEALTHY' as const,
organization_id: 1,
cloud_provider: 'AWS',
region: 'us-east-1',
inserted_at: new Date().toISOString(),
subscription_id: 'subscription-1',
db_host: 'db.supabase.co',
is_branch_enabled: false,
is_physical_backups_enabled: false,
restUrl: 'https://project-1.supabase.co',
}
let mockProjectData: typeof mockProject | undefined = mockProject
vi.mock('@/hooks/misc/useSelectedProject', () => ({
useSelectedProjectQuery: () => {
// Access the variable at runtime when the function is called
return {
data: mockProjectData,
}
},
}))
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
useSelectedOrganizationQuery: () => ({
data: {
id: 1,
name: 'Organization 1',
slug: 'test-org',
plan: { id: 'free', name: 'Free' },
managed_by: 'supabase',
is_owner: true,
billing_email: 'billing@example.com',
billing_partner: null,
usage_billing_enabled: false,
stripe_customer_id: 'stripe-1',
subscription_id: 'subscription-1',
organization_requires_mfa: false,
opt_in_tags: [],
restriction_status: null,
restriction_data: null,
organization_missing_address: false,
requires_indirect_tax_declaration: false,
},
}),
}))
vi.mock('@/lib/telemetry/track', () => ({
useTrack: () => vi.fn(),
}))
const resetSidebarManagerState = () => {
Object.keys(sidebarManagerState.sidebars).forEach((id) => {
sidebarManagerState.unregisterSidebar(id)
})
sidebarManagerState.closeActive()
}
describe('LayoutSidebar', () => {
afterEach(() => {
resetSidebarManagerState()
localStorage.clear()
})
const renderSidebar = () =>
render(
<ResizablePanelGroup orientation="horizontal">
<ResizablePanel>
<div />
</ResizablePanel>
<LayoutSidebarProvider>
<MobileSheetProvider>
<LayoutSidebar />
</MobileSheetProvider>
</LayoutSidebarProvider>
</ResizablePanelGroup>
)
it('does not render when there is no active sidebar', () => {
renderSidebar()
expect(screen.queryByTestId('ai-assistant-sidebar')).toBeNull()
})
it('renders the active sidebar content when toggled on', async () => {
renderSidebar()
await waitFor(() => {
expect(sidebarManagerState.sidebars[SIDEBAR_KEYS.AI_ASSISTANT]).toBeDefined()
})
act(() => {
sidebarManagerState.toggleSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
})
const sidebar = await screen.findByTestId('ai-assistant-sidebar')
expect(sidebar).toBeTruthy()
})
describe('at organization level', () => {
beforeEach(() => {
// Set project to undefined to simulate org-level (no project)
mockProjectData = undefined
})
afterEach(() => {
// Reset to project data for other tests
mockProjectData = mockProject
})
it('registers only the advisor panel, so project-related sidebars cannot be toggled', async () => {
renderSidebar()
// Advisor panel doesn't require a project, so it is still registered
await waitFor(() => {
expect(sidebarManagerState.sidebars[SIDEBAR_KEYS.ADVISOR_PANEL]).toBeDefined()
})
expect(sidebarManagerState.sidebars[SIDEBAR_KEYS.AI_ASSISTANT]).toBeUndefined()
expect(sidebarManagerState.sidebars[SIDEBAR_KEYS.EDITOR_PANEL]).toBeUndefined()
// Try to toggle AI_ASSISTANT - should not work since it's not registered
act(() => {
sidebarManagerState.toggleSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
})
// Should not render since it's not registered
expect(screen.queryByTestId('ai-assistant-sidebar')).toBeNull()
expect(screen.queryByTestId('editor-panel-sidebar')).toBeNull()
// Advisor panel should work
act(() => {
sidebarManagerState.toggleSidebar(SIDEBAR_KEYS.ADVISOR_PANEL)
})
expect(await screen.findByTestId('advisor-panel-sidebar')).toBeTruthy()
})
})
// [Joshen] JFYI temporarily commented this one out - I'm struggling to figure out the mocking to get this to work
// it('auto-opens when sidebar query param matches a registered sidebar', async () => {
// renderSidebar()
// await screen.findByTestId('ai-assistant-sidebar')
// })
})