Files
supabase/apps/studio/components/layouts/ProjectNeedsSecuring/ProjectNeedsSecuring.test.tsx
Alaister YoungandAlaister Young 6dd7062496 [FE-5108] (part 2) refactor(studio): migrate layouts & shell off next/link (#54305)
Part 2 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 | `data/prefetchers/PrefetchableLink.tsx` and
`components/ui/**` |
| **2** | **#54305 ← this PR** | **`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 all `apps/studio/components/layouts/**` files and top-level
navigation shell components off `next/link`.

**Changed:**

- Replaced `next/link` across `components/layouts/**` (`AccountLayout`,
`AppLayout`, `AuthLayout`, `BranchLayout`, `DatabaseLayout`,
`EdgeFunctionsLayout`, `ExplorerLayout`, `IssuesLayout`, `LogsLayout`,
`Navigation`, `ObservabilityLayout`, `ProjectLayout`,
`ProjectNeedsSecuring`, `SQLEditorLayout`, `SignInLayout`,
`TableEditorLayout`, `Tabs`) and top-level shell components
(`Sidebar.tsx`, `UserDropdown.tsx`, `LocalDropdown.tsx`,
`HomePageActions.tsx`)
- Removed `vi.mock('next/link', ...)` from `LocalDropdown.test.tsx` and
`ProjectNeedsSecuring.test.tsx`
- Added accessible labels where external icon/logo links switched from
`<Link>` to plain `<a>` (`AdminStudioButton.tsx`,
`ForgotPasswordLayout.tsx`)

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

## To test

- [ ] Navigate across top header, left sidebar, user/local dropdown, and
project/org/branch switchers
- [ ] Verify mobile navigation menu items and recent-tab links navigate
with expected query params
- [ ] Verify sign-in and forgot-password layout links work cleanly

---------

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

254 lines
6.9 KiB
TypeScript

import { createRootRoute, createRoute } from '@tanstack/react-router'
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ProjectNeedsSecuring } from './ProjectNeedsSecuring'
import { createTestRouter, customRender } from '@/tests/lib/custom-render'
const {
mockUseFlag,
mockUseProjectLintsQuery,
mockUseSelectedProjectQuery,
mockUseTablesQuery,
mockUseProjectPostgrestConfigQuery,
mockUseTablePrivilegesQuery,
mockUseLocalStorageQuery,
} = vi.hoisted(() => ({
mockUseFlag: vi.fn(),
mockUseProjectLintsQuery: vi.fn(),
mockUseSelectedProjectQuery: vi.fn(),
mockUseTablesQuery: vi.fn(),
mockUseProjectPostgrestConfigQuery: vi.fn(),
mockUseTablePrivilegesQuery: vi.fn(),
mockUseLocalStorageQuery: vi.fn(),
}))
const createProjectRouter = async (initialUrl: string) => {
const rootRoute = createRootRoute()
const projectHomeRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/project/$ref',
})
const tablesRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/project/$ref/database/tables',
})
const policiesRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/project/$ref/database/policies',
})
const router = createTestRouter(
initialUrl,
rootRoute.addChildren([projectHomeRoute, tablesRoute, policiesRoute])
)
await router.load()
return router
}
vi.mock('common', async () => {
const actual = await vi.importActual<typeof import('common')>('common')
return {
...actual,
useFlag: mockUseFlag,
useParams: () => ({ ref: 'project-ref' }),
}
})
vi.mock('@/data/lint/lint-query', () => ({
useProjectLintsQuery: mockUseProjectLintsQuery,
}))
vi.mock('@/hooks/misc/useSelectedProject', () => ({
useSelectedProjectQuery: mockUseSelectedProjectQuery,
}))
vi.mock('@/data/tables/tables-query', () => ({
useTablesQuery: mockUseTablesQuery,
}))
vi.mock('@/data/config/project-postgrest-config-query', () => ({
parseDbSchemaString: vi.fn((value: string) => value.split(',').map((schema) => schema.trim())),
useProjectPostgrestConfigQuery: mockUseProjectPostgrestConfigQuery,
}))
vi.mock('@/data/privileges/table-privileges-query', () => ({
useTablePrivilegesQuery: mockUseTablePrivilegesQuery,
}))
vi.mock('@/hooks/misc/useLocalStorage', () => ({
useLocalStorageQuery: mockUseLocalStorageQuery,
}))
vi.mock('sonner', () => ({
toast: {
error: vi.fn(),
},
}))
const issueLint = {
cache_key: 'lint-1',
name: 'rls_disabled_in_public',
detail: 'RLS is disabled on public.invoices',
description: 'RLS disabled',
level: 'ERROR',
categories: ['SECURITY'],
metadata: {
schema: 'public',
name: 'invoices',
},
}
const tables = [
{
id: 1,
name: 'invoices',
schema: 'public',
rls_enabled: false,
},
{
id: 2,
name: 'profiles',
schema: 'public',
rls_enabled: false,
},
{
id: 3,
name: 'customers',
schema: 'public',
rls_enabled: true,
},
]
const tablePrivileges = [
{
schema: 'public',
name: 'invoices',
privileges: [
{
grantee: 'anon',
privilege_type: 'SELECT',
},
],
},
]
describe('ProjectNeedsSecuring', () => {
let initialUrl = '/project/project-ref'
beforeEach(() => {
mockAnimationsApi()
initialUrl = '/project/project-ref'
mockUseFlag.mockReturnValue(true)
mockUseSelectedProjectQuery.mockReturnValue({
data: { connectionString: 'postgresql://example' },
})
mockUseProjectLintsQuery.mockReturnValue({
data: [issueLint],
isPending: false,
isError: false,
})
mockUseTablesQuery.mockReturnValue({
data: tables,
isPending: false,
isError: false,
})
mockUseProjectPostgrestConfigQuery.mockReturnValue({
data: 'public',
isPending: false,
isError: false,
})
mockUseTablePrivilegesQuery.mockReturnValue({
data: tablePrivileges,
isPending: false,
isError: false,
})
mockUseLocalStorageQuery.mockReturnValue([null, vi.fn(), { isLoading: false }])
})
afterEach(() => {
window.localStorage.clear()
})
it('renders the security gate when an exposed table has RLS disabled and the project has not been dismissed', async () => {
const router = await createProjectRouter(initialUrl)
customRender(
<ProjectNeedsSecuring>
<div data-testid="project-children">Project content</div>
</ProjectNeedsSecuring>,
{ router }
)
expect(screen.getByText('Your project needs securing')).toBeInTheDocument()
expect(screen.getByText('Review and fix')).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Open Data API settings' })).toHaveAttribute(
'href',
'/project/project-ref/integrations/data_api/settings'
)
expect(screen.queryByRole('columnheader', { name: 'Action' })).not.toBeInTheDocument()
expect(screen.queryByRole('link', { name: 'View policies' })).not.toBeInTheDocument()
expect(screen.queryByText('profiles')).not.toBeInTheDocument()
expect(screen.queryByText('customers')).not.toBeInTheDocument()
expect(screen.getByText('Skip to home')).toBeInTheDocument()
expect(screen.queryByTestId('project-children')).not.toBeInTheDocument()
})
it('navigates to the table policies page when a table row is clicked', async () => {
const router = await createProjectRouter(initialUrl)
customRender(
<ProjectNeedsSecuring>
<div data-testid="project-children">Project content</div>
</ProjectNeedsSecuring>,
{ router }
)
fireEvent.click(screen.getByText('invoices'))
await waitFor(() => {
expect(router.state.location.href).toBe(
'/project/project-ref/database/policies?schema=public&search=invoices'
)
})
})
it.each([
[
'the security gate has been dismissed',
() =>
mockUseLocalStorageQuery.mockReturnValue([
'2026-04-21T00:00:00.000Z',
vi.fn(),
{ isLoading: false },
]),
],
[
'there are no RLS issues',
() =>
mockUseProjectLintsQuery.mockReturnValue({ data: [], isPending: false, isError: false }),
],
[
'on non-home project routes',
() => {
initialUrl = '/project/project-ref/database/tables'
},
],
['the feature flag is disabled', () => mockUseFlag.mockReturnValue(false)],
])('renders the project content when %s', async (_, arrange) => {
arrange()
const router = await createProjectRouter(initialUrl)
customRender(
<ProjectNeedsSecuring>
<div data-testid="project-children">Project content</div>
</ProjectNeedsSecuring>,
{ router }
)
expect(screen.queryByText('Your project needs securing')).not.toBeInTheDocument()
expect(screen.getByTestId('project-children')).toBeInTheDocument()
})
})