mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
In Studio's vitest setup, `useParams` from `common` is mocked globally
to return `{ ref: 'default' }`, and `next/router`, `next/compat/router`
and `next/navigation` are mocked with `next-router-mock`. `renderRoute`
mounts a real TanStack memory router, but nothing connected it to those
mocks. So components under a route test never saw the route's path or
search params, and `router.push` from legacy code never moved the
TanStack router. Route tests worked around this by hand-mocking
`useParams` to repeat their URL.
Inside `renderRoute`, these hooks now defer to the app's own
`compat/next` shims, which read the memory router, so they match
runtime: kebab-case keys are camelCased, repeated keys collapse to the
first value, and the hooks follow in-test navigation. All other tests
keep the static mocks.
**Changed:**
- `tests/vitestSetup.ts`: the `common` `useParams`, `next/router`,
`next/compat/router` and `next/navigation` mocks are route-aware.
- `tests/lib/route-render.tsx`: the root route provides a
`RenderedRouteContext` marker. The switch keys off this marker rather
than any TanStack router context, because some component tests mount
their own `RouterContextProvider` or mock `@tanstack/react-router`.
- Route tests that relied on the old behavior:
- The EdgeFunctionOverview self-hosted test, `new/$slug` and
`logs/index.platform` now assert on `router.state.location` instead of
`next-router-mock`.
- The `logs` test's `useSignOut` mock is now stable, like the real hook;
an unstable mock made the redirect effect push in a loop.
- The EdgeFunctionOverview periods test now seeds the function detail
under its real slug instead of `undefined`.
- Removed the hand-written `useParams` mocks from the `authorize`,
Stripe `login`, `sign-in-mfa`, Vercel `new-project` and storage
`vectors` route tests; their URLs carry the params now.
**Added:**
- `tests/lib/route-aware-hooks.ts`: `routeAwareHook` and
`mockNextRouter`.
- `tests/lib/route-render.test.tsx`: renders a route at
`/project/abc/probe?query-tab=logs&id=1&id=2`, asserts what
`useParams()`, `useRouter().query` and `usePathname()` return, then
navigates and asserts again.
Addresses
[FE-5150](https://linear.app/supabase/issue/FE-5150/useparams-in-routes-tests-doesnt-return-any-querystring-params)
## To test
- [ ] `pnpm --filter studio exec vitest --run
tests/lib/route-render.test.tsx` passes.
- [ ] With `tests/vitestSetup.ts`, `tests/lib/route-render.tsx` and
`tests/lib/route-aware-hooks.ts` reverted to `master`, the same test
fails with `expected { ref: 'default' } to deeply equal { ref: 'abc',
queryTab: 'logs', id: '1' }`.
- [ ] Regression checks: `pnpm --filter studio exec vitest --run
tests/routes components/interfaces/Functions
components/interfaces/Database/Replication
components/interfaces/Account/Preferences` passes. These include route
tests and the component tests that mount their own TanStack router or
mock `next/router`.
---------
Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
127 lines
3.9 KiB
TypeScript
127 lines
3.9 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import type { components } from '@/data/api'
|
|
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { Route } from '@/routes/project/$ref/branches/index'
|
|
import {
|
|
BRANCH_TEST_SCOPE,
|
|
createTestBranch,
|
|
mockBranchProject,
|
|
mockParentProjectPermissions,
|
|
} from '@/tests/lib/branch-test-utils'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
import { mockPermissions, mockPermissionsError } from '@/tests/lib/msw-permissions'
|
|
import { NO_PERMISSIONS } from '@/tests/lib/permissions-fixtures'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
import { renderRoute } from '@/tests/lib/route-render'
|
|
|
|
vi.mock('common', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('common')>()
|
|
return {
|
|
...actual,
|
|
useIsLoggedIn: () => true,
|
|
}
|
|
})
|
|
|
|
vi.mock('@/lib/constants', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('@/lib/constants')>()
|
|
return { ...actual, IS_PLATFORM: true }
|
|
})
|
|
|
|
const MAIN_BRANCH = createTestBranch({
|
|
id: 'main-branch-id',
|
|
name: 'main',
|
|
is_default: true,
|
|
parent_project_ref: BRANCH_TEST_SCOPE.parentRef,
|
|
project_ref: BRANCH_TEST_SCOPE.parentRef,
|
|
})
|
|
const PREVIEW_BRANCH = createTestBranch({
|
|
name: 'feature-x',
|
|
parent_project_ref: BRANCH_TEST_SCOPE.parentRef,
|
|
project_ref: BRANCH_TEST_SCOPE.branchRef,
|
|
})
|
|
|
|
const PREVIEW_BRANCH_DETAIL: components['schemas']['BranchDetailResponse_Output'] = {
|
|
ref: BRANCH_TEST_SCOPE.branchRef,
|
|
postgres_version: '15.1.0',
|
|
postgres_engine: '15',
|
|
release_channel: 'ga',
|
|
status: 'ACTIVE_HEALTHY',
|
|
db_host: `db.${BRANCH_TEST_SCOPE.branchRef}.supabase.co`,
|
|
db_port: 5432,
|
|
}
|
|
|
|
function mockEndpoints() {
|
|
mockBranchProject()
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/projects/:ref/branches',
|
|
response: [MAIN_BRANCH, PREVIEW_BRANCH],
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/integrations/github/connections',
|
|
response: { connections: [] },
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/integrations/github/authorization',
|
|
response: () => HttpResponse.json<APIErrorBody>({ message: 'Not found' }, { status: 404 }),
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/branches/:branch_id_or_ref',
|
|
response: PREVIEW_BRANCH_DETAIL,
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/organizations/:slug/entitlements',
|
|
response: { entitlements: [] },
|
|
})
|
|
}
|
|
|
|
const renderPage = () =>
|
|
renderRoute(Route, {
|
|
routeId: '/project/$ref/branches/',
|
|
url: `/project/${BRANCH_TEST_SCOPE.branchRef}/branches`,
|
|
profileContext: createMockProfileContext(),
|
|
})
|
|
|
|
describe('BranchesPage', () => {
|
|
test('lists the branches when the user can read branches on the parent project', async () => {
|
|
mockEndpoints()
|
|
mockParentProjectPermissions([FGA_PERMISSIONS.PROJECT.BRANCHING_DEVELOPMENT_READ])
|
|
|
|
await renderPage()
|
|
|
|
expect(await screen.findByText('feature-x')).toBeInTheDocument()
|
|
expect(screen.queryByText(/You need additional permissions/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('shows a no-permission state when the user cannot read branches', async () => {
|
|
mockEndpoints()
|
|
mockPermissions(NO_PERMISSIONS)
|
|
|
|
await renderPage()
|
|
|
|
expect(
|
|
await screen.findByText(/You need additional permissions to view this project's branches/)
|
|
).toBeInTheDocument()
|
|
expect(screen.queryByText('feature-x')).not.toBeInTheDocument()
|
|
})
|
|
|
|
test('shows the error when permissions fail to load', async () => {
|
|
mockEndpoints()
|
|
mockPermissionsError({ message: 'Permissions service unavailable' })
|
|
|
|
await renderPage()
|
|
|
|
expect(
|
|
await screen.findByText('Failed to check your permissions to view branches')
|
|
).toBeInTheDocument()
|
|
expect(screen.queryByText('feature-x')).not.toBeInTheDocument()
|
|
})
|
|
})
|