mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
Part 4 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 ← this PR** | **Database, Branching, Compute, Edge Functions, Explorer, SQL Editor, Storage, Table Editor** | | 5 | #54057 | Remaining `components/interfaces/**` and `QueryInsightsTable` router events | | 6 | #54058 | Remove `next/router` & `next/compat/router` shims, `_router-events.ts`, Vite aliases, and test mocks | Migrates the database, branching, compute, functions, explorer, SQL editor, storage, and table editor interfaces off `next/router` and `next/compat/router`. **Changed:** - Migrated the sole `next/compat/router` consumer (`components/interfaces/Database/Replication/Destinations.tsx`) to `useNavigate` - Replaced `useRouter` across `BranchManagement`, `Branching`, `Compute`, `Database` (`Backups`, `Functions`, `Replication`, `Schemas`, `Tables`, `Triggers`), `EdgeFunctions`, `Explorer`, `Functions`, `SQLEditor`, `Storage` (`AnalyticsBuckets`, `FilesBuckets`, `VectorBuckets`, `StorageMenuV2`, `Storage.utils`), and `TableGridEditor` with TanStack Router hooks (`useNavigate`, `useLocation`, `useRoutePattern`) and `useParams` from `common` - Updated unit and component tests across `Database/Replication`, `Explorer`, `Functions`, and `Storage/FilesBuckets/DeleteBucketModal.test.tsx` to use TanStack Router **Simplified:** - `useSnippetEditor` and `useRedirectLegacyReadReplicaDestination` read search values from `useParams` instead of casting `location.search` - `resolveSourceSwitch` returns `replace: boolean` instead of a `push`/`replace` method name - `ExplorerQueryTab` and `EdgeFunctionDetails` guard on a defined ref instead of navigating with `ref ?? ''` - `useFunctionsDetailShortcuts` and `StorageMenuV2` use a local `goTo` helper for their navigation shortcuts - Tests use the shared `createTestRouter` and `customRender` router, use `toMatchObject` for search assertions, and drop duplicate assertions Addresses [FE-5109](https://linear.app/supabase/issue/FE-5109/replace-the-nextrouter-shims-with-tanstack-router-hooks) ## To test - [ ] SQL Editor: creating, renaming, moving, opening templates/examples, and keyboard shortcuts navigate between snippets cleanly - [ ] Table Editor & Database: opening tables, schema table nodes, triggers, functions, backups/PITR, and replication destination flows navigate as expected - [ ] Storage: switching between Files, Analytics, and Vector buckets and deleting a bucket navigates cleanly - [ ] Edge Functions & Explorer: opening function details/logs, explorer chat/query tabs, and branch management actions navigate properly --------- Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
196 lines
6.0 KiB
TypeScript
196 lines
6.0 KiB
TypeScript
import { QueryClient } from '@tanstack/react-query'
|
|
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import type { components } from 'api-types'
|
|
import { HttpResponse } from 'msw'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { EdgeFunctionDetails } from './EdgeFunctionDetails'
|
|
import { apiKeysKeys } from '@/data/api-keys/keys'
|
|
import { FGA_PERMISSIONS } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
import { mockPermissionsError } from '@/tests/lib/msw-permissions'
|
|
import { ALL_FGA_PERMISSIONS, type PermissionsV2Response } from '@/tests/lib/permissions-fixtures'
|
|
import { createMockProfileContext } from '@/tests/lib/profile-helpers'
|
|
import { mockProjectGrantContext } from '@/tests/lib/project-grant-mocks'
|
|
|
|
vi.mock('common', async () => {
|
|
const actual = await vi.importActual<Record<string, unknown>>('common')
|
|
return {
|
|
...actual,
|
|
useIsLoggedIn: () => true,
|
|
useParams: () => ({ ref: 'default', functionSlug: 'hello' }),
|
|
}
|
|
})
|
|
|
|
type ApiKeyResponse = components['schemas']['ApiKeyResponse_Output']
|
|
type FunctionResponse = components['schemas']['FunctionSlugResponse_Output']
|
|
|
|
const profileContext = createMockProfileContext()
|
|
|
|
const withoutKeysRead = (): PermissionsV2Response => ({
|
|
organizations: [
|
|
{
|
|
slug: 'acme',
|
|
role: 'developer',
|
|
permissions: ALL_FGA_PERMISSIONS.filter(
|
|
(permission) => permission !== FGA_PERMISSIONS.PROJECT.API_GATEWAY_KEYS_READ
|
|
),
|
|
projects: [],
|
|
},
|
|
],
|
|
})
|
|
|
|
const apiKeys: ApiKeyResponse[] = [
|
|
{
|
|
type: 'publishable',
|
|
name: 'default',
|
|
api_key: 'sb_publishable_abc',
|
|
id: 'pub-id',
|
|
inserted_at: '2024-01-01',
|
|
},
|
|
]
|
|
|
|
const mockApiKeys = () => {
|
|
const requests = vi.fn()
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/projects/:ref/api-keys',
|
|
response: () => {
|
|
requests()
|
|
return HttpResponse.json<ApiKeyResponse[]>(apiKeys)
|
|
},
|
|
})
|
|
return requests
|
|
}
|
|
|
|
const mockFunctionDeps = () => {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/projects/:ref/functions/:function_slug',
|
|
response: {
|
|
id: 'fn-id',
|
|
slug: 'hello',
|
|
name: 'hello',
|
|
status: 'ACTIVE',
|
|
version: 1,
|
|
created_at: 1,
|
|
updated_at: 1,
|
|
verify_jwt: true,
|
|
} satisfies FunctionResponse,
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref/billing/addons',
|
|
response: { selected_addons: [], available_addons: [], ref: 'default' },
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/v1/projects/:ref/custom-hostname',
|
|
response: {
|
|
status: '1_not_started',
|
|
custom_hostname: '',
|
|
data: {
|
|
errors: [],
|
|
messages: [],
|
|
success: true,
|
|
result: { hostname: 'api.example.com', id: 'hostname-id' },
|
|
},
|
|
},
|
|
})
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref/settings',
|
|
response: {
|
|
app_config: {
|
|
endpoint: 'default.supabase.co',
|
|
db_schema: 'public',
|
|
storage_endpoint: 'default.supabase.co',
|
|
},
|
|
cloud_provider: 'AWS',
|
|
db_dns_name: 'db.default.supabase.co',
|
|
db_host: 'db.default.supabase.co',
|
|
db_name: 'postgres',
|
|
db_port: 5432,
|
|
db_user: 'postgres',
|
|
inserted_at: '2026-01-01T00:00:00.000Z',
|
|
name: 'Production',
|
|
ref: 'default',
|
|
region: 'us-east-1',
|
|
ssl_enforced: false,
|
|
status: 'ACTIVE_HEALTHY',
|
|
},
|
|
})
|
|
}
|
|
|
|
describe('EdgeFunctionDetails', () => {
|
|
it('requests the keys without a note when api-keys are readable', async () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
const requests = mockApiKeys()
|
|
mockFunctionDeps()
|
|
|
|
customRender(<EdgeFunctionDetails />, { profileContext })
|
|
|
|
await vi.waitFor(() => expect(requests).toHaveBeenCalled())
|
|
expect(screen.queryByText(/need additional permissions/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('explains the missing permission and skips the request when denied', async () => {
|
|
mockProjectGrantContext({ ref: 'default', permissions: withoutKeysRead() })
|
|
const requests = mockApiKeys()
|
|
mockFunctionDeps()
|
|
|
|
customRender(<EdgeFunctionDetails />, { profileContext })
|
|
|
|
expect(
|
|
await screen.findByText(/You need additional permissions to view API keys\./)
|
|
).toBeVisible()
|
|
expect(requests).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not use cached keys when denied', async () => {
|
|
mockProjectGrantContext({ ref: 'default', permissions: withoutKeysRead() })
|
|
mockApiKeys()
|
|
mockFunctionDeps()
|
|
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
|
queryClient.setQueryData(apiKeysKeys.list('default', false), apiKeys)
|
|
|
|
customRender(<EdgeFunctionDetails />, { profileContext, queryClient })
|
|
|
|
expect(
|
|
await screen.findByText(/You need additional permissions to view API keys\./)
|
|
).toBeVisible()
|
|
await userEvent.click(screen.getByRole('button', { name: 'Show anon key' }))
|
|
expect(await screen.findByText(/YOUR ANON KEY/)).toBeInTheDocument()
|
|
expect(screen.queryByText(/sb_publishable_abc/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows an error instead of the invoke snippet when the project endpoint fails to load', async () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
mockApiKeys()
|
|
mockFunctionDeps()
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref/settings',
|
|
response: () =>
|
|
HttpResponse.json<APIErrorBody>({ message: 'Settings unavailable' }, { status: 500 }),
|
|
})
|
|
|
|
customRender(<EdgeFunctionDetails />, { profileContext })
|
|
|
|
expect(await screen.findByText('Failed to retrieve function URL')).toBeVisible()
|
|
expect(screen.queryByText(/curl -L -X POST/)).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('shows an error when permissions fail to load', async () => {
|
|
mockProjectGrantContext({ ref: 'default' })
|
|
mockPermissionsError()
|
|
mockFunctionDeps()
|
|
|
|
customRender(<EdgeFunctionDetails />, { profileContext })
|
|
|
|
expect(await screen.findByText(/Failed to check API key permissions/)).toBeVisible()
|
|
})
|
|
})
|