mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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>
127 lines
4.1 KiB
TypeScript
127 lines
4.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
import type { UseNavigateResult } from '@tanstack/react-router'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { createNavigationHandler, navigateToUrl } from './navigation'
|
|
|
|
type NavigationEvent = Parameters<ReturnType<typeof createNavigationHandler>>[0]
|
|
type Outcome = 'push' | 'new-tab' | 'none'
|
|
|
|
describe('createNavigationHandler', () => {
|
|
const URL = '/project/123/settings'
|
|
const navigate = vi.fn()
|
|
const windowOpen = vi.fn()
|
|
|
|
beforeEach(() => {
|
|
navigate.mockReset()
|
|
windowOpen.mockReset()
|
|
global.window.open = windowOpen
|
|
})
|
|
|
|
const run = (event: Record<string, unknown>, url = URL) => {
|
|
const preventDefault = vi.fn()
|
|
const handler = createNavigationHandler(url, navigate as unknown as UseNavigateResult<string>)
|
|
handler({
|
|
metaKey: false,
|
|
ctrlKey: false,
|
|
preventDefault,
|
|
...event,
|
|
} as unknown as NavigationEvent)
|
|
return { preventDefault }
|
|
}
|
|
|
|
const expectOutcome = (outcome: Outcome) => {
|
|
if (outcome === 'push') {
|
|
expect(navigate).toHaveBeenCalledWith({ to: URL, search: {}, hash: '' })
|
|
} else {
|
|
expect(navigate).not.toHaveBeenCalled()
|
|
}
|
|
if (outcome === 'new-tab') {
|
|
expect(windowOpen).toHaveBeenCalledWith(URL, '_blank')
|
|
} else {
|
|
expect(windowOpen).not.toHaveBeenCalled()
|
|
}
|
|
}
|
|
|
|
it.each<[string, Record<string, unknown>, Outcome]>([
|
|
['Enter', { key: 'Enter' }, 'push'],
|
|
['Space', { key: ' ' }, 'push'],
|
|
['Cmd + Enter', { key: 'Enter', metaKey: true }, 'new-tab'],
|
|
['Ctrl + Enter', { key: 'Enter', ctrlKey: true }, 'new-tab'],
|
|
['Cmd + Space', { key: ' ', metaKey: true }, 'new-tab'],
|
|
])('handles %s by preventing default and navigating', (_, event, outcome) => {
|
|
const { preventDefault } = run(event)
|
|
|
|
expect(preventDefault).toHaveBeenCalled()
|
|
expectOutcome(outcome)
|
|
})
|
|
|
|
it('ignores other keys', () => {
|
|
const { preventDefault } = run({ key: 'a' })
|
|
|
|
expect(preventDefault).not.toHaveBeenCalled()
|
|
expectOutcome('none')
|
|
})
|
|
|
|
it.each<[string, Record<string, unknown>, Outcome]>([
|
|
['a regular left click', { button: 0 }, 'push'],
|
|
['a middle click', { button: 1 }, 'new-tab'],
|
|
['Cmd + left click', { button: 0, metaKey: true }, 'new-tab'],
|
|
['Ctrl + left click', { button: 0, ctrlKey: true }, 'new-tab'],
|
|
// Right click opens the context menu, so it must not navigate.
|
|
['a right click', { button: 2 }, 'none'],
|
|
])('handles %s', (_, event, outcome) => {
|
|
const { preventDefault } = run(event)
|
|
|
|
expectOutcome(outcome)
|
|
// New-tab clicks must not also trigger the browser's default navigation
|
|
if (outcome === 'new-tab') expect(preventDefault).toHaveBeenCalled()
|
|
})
|
|
|
|
it('splits path, search params, and hash when navigating', () => {
|
|
run({ button: 0 }, '/project/123/settings?tab=general&filter=a&filter=b#limits')
|
|
|
|
expect(navigate).toHaveBeenCalledWith({
|
|
to: '/project/123/settings',
|
|
search: { tab: 'general', filter: ['a', 'b'] },
|
|
hash: 'limits',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('navigateToUrl', () => {
|
|
it('splits internal paths into to, search, and hash and preserves newlines in query values', () => {
|
|
const navigate = vi.fn()
|
|
navigateToUrl(
|
|
navigate as unknown as UseNavigateResult<string>,
|
|
'/project/123/logs/explorer?s=select%0A1&tag=a&tag=b#results',
|
|
{ replace: true }
|
|
)
|
|
|
|
expect(navigate).toHaveBeenCalledWith({
|
|
to: '/project/123/logs/explorer',
|
|
search: { s: 'select\n1', tag: ['a', 'b'] },
|
|
hash: 'results',
|
|
replace: true,
|
|
})
|
|
})
|
|
|
|
it('uses reloadDocument for cross-origin and protocol-relative URLs', () => {
|
|
const navigate = vi.fn()
|
|
navigateToUrl(navigate as unknown as UseNavigateResult<string>, 'https://supabase.com/docs')
|
|
expect(navigate).toHaveBeenCalledWith({
|
|
href: 'https://supabase.com/docs',
|
|
reloadDocument: true,
|
|
})
|
|
|
|
navigateToUrl(navigate as unknown as UseNavigateResult<string>, '//example.com/docs', {
|
|
replace: true,
|
|
})
|
|
expect(navigate).toHaveBeenLastCalledWith({
|
|
href: `${window.location.protocol}//example.com/docs`,
|
|
reloadDocument: true,
|
|
replace: true,
|
|
})
|
|
})
|
|
})
|