Files
supabase/apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.test.tsx
Alaister YoungandAlaister Young 3b57afc40c fix(studio): wait for project endpoint before showing function url (#53255)
`EdgeFunctionsListItem` built its URL as ``
`${endpoint}/functions/v1/${slug}` `` from `useProjectApiUrl`, whose
`data` is `undefined` while the project settings query is still pending.
On a cold load, each row showed `undefined/functions/v1/<slug>`, and the
copy button copied that string. `EdgeFunctionDetails` and
`TerminalInstructions` interpolated the endpoint the same way.

**Changed:**
-
`apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.tsx`:
only builds the URL once `endpoint` is defined. While it's pending, the
row shows a `ShimmeringLoader` in place of the URL and no copy button.
The copy button now has an accessible name ("Copy function URL").
-
`apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx`:
the "Invoke function" snippets wait for both the endpoint and the
function. Until then they show a skeleton, so the cURL tab no longer
renders or copies `undefined/functions/v1/...`. If either query fails,
the card shows an `AlertError` instead.
-
`apps/studio/components/interfaces/Functions/TerminalInstructions.tsx`:
the "Invoke your function" curl command is only added once the endpoint
resolves, with a loader in its place until then.

Also checked: `EdgeFunctionOverviewHeader` already guards on `endpoint
&& slug`, so it's unchanged. `EdgeFunctionTesterSheet` doesn't use
`useProjectApiUrl`: it builds the URL from project settings at submit
time and never renders it, so it's also unchanged. `useProjectApiUrl`
itself is unchanged.

**Added:**
-
`apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.test.tsx`:
renders a row while the settings request is in flight and asserts
there's no `undefined/functions/v1` text, a loader is shown and there's
no copy button. Once settings resolve, it asserts the full URL and that
the copy button copies it. It fails on the old component.
-
`apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.platform.test.tsx`:
a case where project settings fail to load, asserting the invoke card
shows the error and no cURL snippet.

## To test
- [ ] Open a project's Edge Functions page (`/project/<ref>/functions`)
with a hard reload and network throttling (for example "Slow 4G"). The
URL column never shows `undefined`: it shows a loader, then
`https://<ref>.supabase.co/functions/v1/<slug>`.
- [ ] Click a row's copy icon: the clipboard has the full function URL
and the page doesn't navigate.
- [ ] Click a row: it still navigates to that function's page.
- [ ] Hard reload a function's Details page
(`/project/<ref>/functions/<slug>/details`) with throttling. The cURL
snippet under "Invoke function" shows a skeleton, then the real URL.
- [ ] Open `/project/<ref>/functions?create=cli` with throttling. The
"Invoke your function" curl command shows a loader, then the real URL.

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-11 15:57:40 +09:00

95 lines
3.5 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 { Table, TableBody } from 'ui'
import { expect, it } from 'vitest'
import { EdgeFunctionsListItem } from './EdgeFunctionsListItem'
import { customDomainKeys } from '@/data/custom-domains/keys'
import { edgeFunctionsKeys } from '@/data/edge-functions/keys'
import { projectKeys } from '@/data/projects/keys'
import { subscriptionKeys } from '@/data/subscriptions/keys'
import { createMockProject } from '@/tests/helpers'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
import { routerMock } from '@/tests/lib/route-mock'
type FunctionResponse = components['schemas']['FunctionResponse_Output']
type ProjectSettingsResponse = components['schemas']['ProjectSettingsResponse_Output']
const edgeFunction: FunctionResponse = {
id: 'function-id',
name: 'Send email',
slug: 'send-email',
status: 'ACTIVE',
version: 2,
created_at: new Date('2026-03-20T10:00:00Z').valueOf(),
updated_at: new Date('2026-03-20T11:00:00Z').valueOf(),
}
const createQueryClient = () => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
})
queryClient.setQueryData(projectKeys.detail('default'), createMockProject({ ref: 'default' }))
queryClient.setQueryData(subscriptionKeys.addons('default'), { selected_addons: [] })
queryClient.setQueryData(customDomainKeys.list('default'), {})
queryClient.setQueryData(edgeFunctionsKeys.list('default'), [edgeFunction])
return queryClient
}
const mockProjectSettings = () => {
addAPIMock({
method: 'get',
path: '/platform/projects/:ref/settings',
response: () =>
HttpResponse.json<ProjectSettingsResponse>({
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',
}),
})
}
it('shows a loader instead of an undefined function URL until the project endpoint loads', async () => {
const user = userEvent.setup()
routerMock.setCurrentUrl('/projects/default')
mockProjectSettings()
const { container } = customRender(
<Table>
<TableBody>
<EdgeFunctionsListItem function={edgeFunction} />
</TableBody>
</Table>,
{ queryClient: createQueryClient() }
)
expect(screen.queryByText(/undefined\/functions\/v1/)).not.toBeInTheDocument()
expect(container.querySelector('.shimmering-loader')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Copy function URL' })).not.toBeInTheDocument()
const functionUrl = 'https://default.supabase.co/functions/v1/send-email'
expect(await screen.findByText(functionUrl)).toBeInTheDocument()
expect(container.querySelector('.shimmering-loader')).not.toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Copy function URL' }))
expect(await navigator.clipboard.readText()).toBe(functionUrl)
})