Files
supabase/apps/studio/components/interfaces/Database/Policies/PolicyTableRow/PolicyTableRow.utils.test.ts
Alaister YoungandAlaister Young 0a4cec0464 [FE-5108] (part 1) refactor(studio): migrate core UI & prefetchers off next/link (#54304)
Part 1 of a 6-PR stack migrating all `next/link` imports in
`apps/studio` onto TanStack Router's `Link` (or plain `<a>` for known
external URLs) and removing `compat/next/link.tsx`.

| Part | PR | Scope |
| --- | --- | --- |
| **1** | **#54304 ← this PR** |
**`data/prefetchers/PrefetchableLink.tsx` and `components/ui/**`** |
| 2 | #54305 | `components/layouts/**` and top-level shell components |
| 3 | #54306 | Database, TableGridEditor, Storage, SQLEditor,
BranchManagement, Branching, Functions, Realtime |
| 4 | #54307 | Integrations, Settings, Observability, LogDrains, Issues,
Linter, Reports, QueryPerformance, QuerySources, ConfigDrift,
DiskManagement, Compute |
| 5 | #54308 | Remaining `components/interfaces/**` (Org, Billing, Auth,
Support, Home, SignIn, ConnectSheet, etc.) |
| 6 | #54309 | Remove `compat/next/link.tsx` shim, Vite alias, and
update basePath tests/docs |

Migrates `data/prefetchers/PrefetchableLink.tsx` and all shared
`components/ui/**` components off `next/link`.

**Added:**

- `apps/studio/components/ui/DynamicLink.tsx` (with unit tests) — shared
`forwardRef` link wrapper for dynamic URL strings that runs
`splitInternalUrl` and renders a plain `<a>` for external URLs (or
same-origin URLs outside the app's `basePath`) vs TanStack `<Link>` for
internal paths

**Changed:**

- Replaced `next/link` in
`apps/studio/data/prefetchers/PrefetchableLink.tsx` and all 30 importing
components under `apps/studio/components/ui/**` (`InlineLink`,
`CardButton`, `CommandItemLink`, `ObservabilityLink`, `ProductMenuItem`,
`ResourceItem`, `UpgradePlanButton`, `ToggleSpendCapButton`, banners, AI
assistant panel links, etc.)
- Used typed `<Link to="..." params={...} search={...} hash="..." />`
for known internal routes, `<DynamicLink href={...} />` for
dynamic/untyped URL strings (whether internal with query/hash or
external), and plain `<a>` for known external links
- Updated `apps/studio/routes/index.tsx` `validateSearch` return type to
`{ next?: string }` so `<Link to="/">` compiles without requiring
`search={{ next: undefined }}`
- Updated
`hooks/misc/__tests__/useTableEditorFiltersSort.navigation.test.tsx`,
`PolicyTableRow.utils.test.ts`, `SupportAssistantSuccessCard.test.tsx`,
and `useOrganizationRestrictions.test.tsx` to render inside a TanStack
Router context (`customRender` / `RouterContextProvider`)

Addresses
[FE-5108](https://linear.app/supabase/issue/FE-5108/replace-the-nextlink-shim-with-tanstack-link)

## To test

- [ ] Click internal and external `InlineLink`, `CardButton`,
`ProductMenuItem`, and `PrefetchableLink` targets and verify client-side
navigation vs external anchors
- [ ] Verify banner links (upgrade, resource exhaustion, queue
operations) preserve query params and hashes
- [ ] Confirm Studio typecheck, lint, and unit tests pass

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-12-01 16:23:48 +08:00

159 lines
5.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// @vitest-environment jsdom
import type { ReactElement } from 'react'
import { describe, expect, it } from 'vitest'
import { getTableAdmonitionMessage, getTableDataApiStatus } from './PolicyTableRow.utils'
import type { TableDataApiStatus } from './PolicyTableRow.utils'
import type { TableApiAccessData } from '@/data/privileges/table-api-access-query'
import type { ApiPrivilegesByRole } from '@/lib/data-api-types'
import { customRender } from '@/tests/lib/custom-render'
const FULL_PRIVILEGES: ApiPrivilegesByRole = {
anon: ['SELECT', 'INSERT', 'UPDATE', 'DELETE'],
authenticated: ['SELECT', 'INSERT', 'UPDATE', 'DELETE'],
service_role: ['SELECT', 'INSERT', 'UPDATE', 'DELETE'],
}
const PARTIAL_PRIVILEGES: ApiPrivilegesByRole = {
anon: ['SELECT'],
authenticated: [],
service_role: ['SELECT', 'INSERT', 'UPDATE', 'DELETE'],
}
const grantedAccess: TableApiAccessData = {
apiAccessType: 'access',
grantStatus: 'granted',
privileges: FULL_PRIVILEGES,
}
const customAccess: TableApiAccessData = {
apiAccessType: 'access',
grantStatus: 'custom',
privileges: PARTIAL_PRIVILEGES,
}
const noGrants: TableApiAccessData = { apiAccessType: 'exposed-schema-no-grants' }
const schemaNotExposedData: TableApiAccessData = { apiAccessType: 'none' }
describe('getTableDataApiStatus', () => {
it('returns no-grants when schema is exposed but no API roles have privileges', () => {
const status = getTableDataApiStatus({
apiAccessData: noGrants,
isRLSEnabled: true,
policiesCount: 0,
})
expect(status).toBe('no-grants')
})
it('returns publicly-readable when fully granted and RLS is off', () => {
const status = getTableDataApiStatus({
apiAccessData: grantedAccess,
isRLSEnabled: false,
policiesCount: 3,
})
expect(status).toBe('publicly-readable')
})
it('returns locked-by-rls when fully granted + RLS on + no policies', () => {
const status = getTableDataApiStatus({
apiAccessData: grantedAccess,
isRLSEnabled: true,
policiesCount: 0,
})
expect(status).toBe('locked-by-rls')
})
it('returns secured when fully granted + RLS on + policies exist', () => {
const status = getTableDataApiStatus({
apiAccessData: grantedAccess,
isRLSEnabled: true,
policiesCount: 2,
})
expect(status).toBe('secured')
})
it('returns unknown when apiAccessData is still loading or errored', () => {
// apiAccessData is undefined during loading AND on query error (isPending flips false
// but data stays undefined). We must not fall through to 'schema-not-exposed' — that
// would tell the user to reconfigure API settings for a schema that is in fact exposed.
const status = getTableDataApiStatus({
apiAccessData: undefined,
isRLSEnabled: true,
policiesCount: 0,
})
expect(status).toBe('unknown')
})
it('returns unknown when apiAccessData reports apiAccessType=none on an exposed schema', () => {
// Defensive: the query shouldn't emit apiAccessType=none when schema is exposed,
// but if it does we still don't want the false "schema not exposed" admonition.
const status = getTableDataApiStatus({
apiAccessData: schemaNotExposedData,
isRLSEnabled: true,
policiesCount: 0,
})
expect(status).toBe('unknown')
})
it('custom-grants wins over RLS state — we never claim public-readable for partial grants', () => {
const rlsOff = getTableDataApiStatus({
apiAccessData: customAccess,
isRLSEnabled: false,
policiesCount: 0,
})
const rlsOnNoPolicies = getTableDataApiStatus({
apiAccessData: customAccess,
isRLSEnabled: true,
policiesCount: 0,
})
expect(rlsOff).toBe('custom-grants')
expect(rlsOnNoPolicies).toBe('custom-grants')
})
})
const renderAdmonitionText = (status: TableDataApiStatus) => {
const message = getTableAdmonitionMessage({ status })
return customRender(message as ReactElement).container.textContent
}
describe('getTableAdmonitionMessage', () => {
it('returns the custom-grants copy', () => {
expect(renderAdmonitionText('custom-grants')).toBe(
'This table has custom Data API permissions — access may be restricted for some roles or operations.'
)
})
it('returns the no-grants copy, linking to the Data API settings for the given project ref', () => {
const message = getTableAdmonitionMessage({ status: 'no-grants', ref: 'my-project' })
const { container, getByRole } = customRender(message as ReactElement)
expect(container.textContent).toBe(
'This table cannot be accessed via the Data API. Enable access in your project’s Data API settings.'
)
expect(getByRole('link', { name: 'Data API settings' })).toHaveAttribute(
'href',
'/project/my-project/integrations/data_api/settings'
)
})
it('returns the publicly-readable copy', () => {
expect(renderAdmonitionText('publicly-readable')).toBe(
'This table can be accessed by anyone via the Data API as RLS is disabled.'
)
})
it('returns the locked-by-rls copy', () => {
expect(renderAdmonitionText('locked-by-rls')).toBe(
'No data will be returned via the Data API as no RLS policies exist on this table.'
)
})
it('returns null for secured — no admonition needed', () => {
expect(getTableAdmonitionMessage({ status: 'secured' })).toBeNull()
})
it('returns null for unknown — caller should stay silent during loading/errored state', () => {
expect(getTableAdmonitionMessage({ status: 'unknown' })).toBeNull()
})
})