mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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>
145 lines
4.7 KiB
TypeScript
145 lines
4.7 KiB
TypeScript
import { fireEvent, screen, waitFor } 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 z from 'zod'
|
|
|
|
import { CreateRolePanel } from './CreateRolePanel'
|
|
import { ProjectContextProvider } from '@/components/layouts/ProjectLayout/ProjectContext'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock } from '@/tests/lib/msw'
|
|
|
|
mockAnimationsApi()
|
|
|
|
const FORM_ID = 'create-new-role'
|
|
|
|
const QueryBodySchema = z.object({ query: z.string() })
|
|
|
|
describe('CreateRolePanel', () => {
|
|
let queries: string[]
|
|
|
|
beforeEach(() => {
|
|
queries = []
|
|
// useSelectedProjectQuery
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
// @ts-expect-error partial project is enough for the connection string lookup
|
|
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',
|
|
},
|
|
})
|
|
// useDatabaseRoleCreateMutation executes SQL, and the invalidated roles query lists roles
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/platform/pg-meta/:ref/query',
|
|
response: async ({ request }) => {
|
|
const body = QueryBodySchema.parse(await request.json())
|
|
queries.push(body.query)
|
|
return HttpResponse.json<Record<string, unknown>[]>([])
|
|
},
|
|
})
|
|
})
|
|
|
|
const renderPanel = () => {
|
|
const onClose = vi.fn()
|
|
customRender(
|
|
<ProjectContextProvider projectRef="default">
|
|
<CreateRolePanel visible onClose={onClose} />
|
|
</ProjectContextProvider>
|
|
)
|
|
return { onClose }
|
|
}
|
|
|
|
const fillName = async (name: string) => {
|
|
const user = userEvent.setup()
|
|
await user.click(await screen.findByLabelText('Name'))
|
|
await user.paste(name)
|
|
return user
|
|
}
|
|
|
|
const getLoginSwitch = () => screen.getByLabelText('User can login')
|
|
|
|
const getCreateQuery = () => queries.find((query) => query.includes('create role'))
|
|
|
|
test('hides the password field until login is enabled', async () => {
|
|
renderPanel()
|
|
|
|
await screen.findByLabelText('Name')
|
|
expect(screen.queryByLabelText('Password')).not.toBeInTheDocument()
|
|
|
|
await userEvent.click(getLoginSwitch())
|
|
|
|
expect(await screen.findByLabelText('Password')).toBeInTheDocument()
|
|
})
|
|
|
|
test('fills the password field and announces it when a password is generated', async () => {
|
|
renderPanel()
|
|
const user = userEvent.setup()
|
|
await screen.findByLabelText('Name')
|
|
|
|
await user.click(getLoginSwitch())
|
|
const passwordInput = await screen.findByLabelText('Password')
|
|
expect(passwordInput).toHaveValue('')
|
|
|
|
await user.click(screen.getByRole('button', { name: 'Generate a password' }))
|
|
|
|
expect(passwordInput).not.toHaveValue('')
|
|
expect(screen.getByText('Password generated')).toBeInTheDocument()
|
|
|
|
// Editing the generated password clears the announcement
|
|
await user.type(passwordInput, 'x')
|
|
expect(screen.queryByText('Password generated')).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('creates the role with the password when login is enabled', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
await user.click(await screen.findByLabelText('Password'))
|
|
await user.paste('s3cret')
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
// The password is sent as a SCRAM-SHA-256 verifier, not as plaintext
|
|
expect(getCreateQuery()).toContain(`password 'SCRAM-SHA-256$`)
|
|
expect(getCreateQuery()).not.toContain('s3cret')
|
|
})
|
|
|
|
test('omits the password when login is enabled but the field is empty', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
expect(getCreateQuery()).toContain('login')
|
|
expect(getCreateQuery()).not.toContain('password')
|
|
})
|
|
|
|
test('omits a typed password when login is turned off again', async () => {
|
|
renderPanel()
|
|
const user = await fillName('app_user')
|
|
|
|
await user.click(getLoginSwitch())
|
|
await user.click(await screen.findByLabelText('Password'))
|
|
await user.paste('s3cret')
|
|
await user.click(getLoginSwitch())
|
|
fireEvent.submit(document.getElementById(FORM_ID)!)
|
|
|
|
await waitFor(() => expect(getCreateQuery()).toBeDefined())
|
|
expect(getCreateQuery()).not.toContain('password')
|
|
expect(getCreateQuery()).not.toContain('s3cret')
|
|
})
|
|
})
|