Files
supabase/apps/studio/components/interfaces/Integrations/CronJobs/CronJobsTab.EnableCleanupButton.test.tsx
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

136 lines
4.8 KiB
TypeScript

import { screen, waitFor, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { HttpResponse } from 'msw'
import { beforeEach, describe, expect, test, vi } from 'vitest'
import { EnableCleanupButton } from './CronJobsTab.EnableCleanupButton'
import { ProjectContextProvider } from '@/components/layouts/ProjectLayout/ProjectContext'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
mockAnimationsApi()
const cleanupJobRow = {
jobid: 1,
jobname: 'delete-job-run-details',
schedule: '0 12 * * *',
command: `DELETE FROM cron.job_run_details WHERE end_time < now() - interval '7 days';`,
active: true,
}
// Mutable state for the pg-meta mock, reset per test. Scheduling flips
// cleanupJobExists so the subsequent existence refetch sees the new job,
// mirroring the real invalidation flow.
let cleanupJobExists = false
let lookupCount = 0
let scheduleQueries: string[] = []
const renderButton = (onScheduled = vi.fn()) => {
customRender(
<ProjectContextProvider projectRef="default">
<EnableCleanupButton onScheduled={onScheduled} />
</ProjectContextProvider>
)
return onScheduled
}
describe('EnableCleanupButton', () => {
beforeEach(() => {
cleanupJobExists = false
lookupCount = 0
scheduleQueries = []
// useSelectedProjectQuery
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
// @ts-expect-error partial project shape
response: {
cloud_provider: 'localhost',
id: 1,
inserted_at: '2021-08-02T06:40:40.646Z',
name: 'Default Project',
organization_id: 1,
ref: 'default',
region: 'local',
status: 'ACTIVE_HEALTHY',
},
})
// The existence lookup (useCronJobQuery by name) and the schedule mutation
// both go through the pg-meta query endpoint with different SQL
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: async ({ request }) => {
const { query } = (await request.json()) as { query: string }
if (query.includes('cron.schedule')) {
scheduleQueries.push(query)
cleanupJobExists = true
return HttpResponse.json([{ schedule: 1 }])
}
// jobname lookup
lookupCount += 1
return HttpResponse.json(cleanupJobExists ? [cleanupJobRow] : [])
},
})
})
test('shows the button once the cleanup job is confirmed missing', async () => {
renderButton()
expect(await screen.findByRole('button', { name: 'Enable cleanup' })).toBeInTheDocument()
})
test('hides the button when the cleanup job already exists', async () => {
cleanupJobExists = true
renderButton()
await waitFor(() => expect(lookupCount).toBeGreaterThan(0))
expect(screen.queryByRole('button', { name: 'Enable cleanup' })).not.toBeInTheDocument()
})
test('schedules the cleanup job with the selected retention interval', async () => {
const onScheduled = renderButton()
await userEvent.click(await screen.findByRole('button', { name: 'Enable cleanup' }))
const dialog = await screen.findByRole('dialog')
expect(within(dialog).getByText('Enable automatic cleanup')).toBeInTheDocument()
expect(within(dialog).getByRole('combobox')).toHaveTextContent('Older than 7 days')
await userEvent.click(within(dialog).getByRole('combobox'))
await userEvent.click(await screen.findByRole('option', { name: 'Older than 1 day' }))
await userEvent.click(within(dialog).getByRole('button', { name: 'Enable cleanup' }))
await waitFor(() => expect(scheduleQueries).toHaveLength(1))
expect(scheduleQueries[0]).toContain(`'delete-job-run-details'`)
expect(scheduleQueries[0]).toContain(`'0 12 * * *'`)
expect(scheduleQueries[0]).toContain(`interval ''1 day''`)
await waitFor(() => expect(onScheduled).toHaveBeenCalledTimes(1))
// The mutation invalidates the existence query, which now returns the job,
// so the whole component (dialog included) unmounts
await waitFor(() => {
expect(screen.queryByRole('button', { name: 'Enable cleanup' })).not.toBeInTheDocument()
})
expect(screen.queryByRole('dialog')).not.toBeInTheDocument()
})
test('cancel closes the dialog without scheduling', async () => {
renderButton()
await userEvent.click(await screen.findByRole('button', { name: 'Enable cleanup' }))
const dialog = await screen.findByRole('dialog')
await userEvent.click(within(dialog).getByRole('button', { name: 'Cancel' }))
await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument())
expect(scheduleQueries).toHaveLength(0)
expect(screen.getByRole('button', { name: 'Enable cleanup' })).toBeInTheDocument()
})
})