Files
supabase/apps/studio/data/granted-query-options.test.ts
Alaister YoungandAlaister Young 8f75187422 fix(studio): stop dev No queryFn error from grantedQueryOptions (#54004)
Fixes TanStack Query spamming `No queryFn was passed as an option` in
dev whenever a grant-gated query renders without a grant or required
variable (such as `branchesQueryOptions` when `projectRef` is
`undefined`).

**Changed:**

- Replaced `queryFn: undefined` in `grantedQueryOptions` with a stable
`fallbackQueryFn` while keeping `enabled: false`. This satisfies
`useBaseQuery`'s dev-mode check and still delegates to a subscribed
granted observer on invalidation (avoiding the `skipToken` shared-cache
regression from #53088).
- Updated `granted-query-options.test.ts` and `api-keys-query.test.tsx`
to verify no `console.error` is logged when rendering without a grant or
`projectRef` and that invalidation still refetches via the granted
observer.
- Updated the `studio-queries` skill note to match.

## To test

- Run `pnpm dev:studio` and open a non-branch project's home page.
Confirm the browser console and dev terminal no longer spam
`[["projects",null,"branches"]]: No queryFn was passed as an option`.
- Open Project Settings -> API Keys, create or delete a key, and confirm
the list still refreshes cleanly.
- Run `pnpm --filter studio test data/granted-query-options.test.ts
data/api-keys/api-keys-query.test.tsx`.

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-26 01:01:33 +08:00

80 lines
3.0 KiB
TypeScript

import { QueryClient, skipToken } from '@tanstack/react-query'
import { describe, expect, expectTypeOf, test } from 'vitest'
import { grantedQueryOptions } from './granted-query-options'
const queryKey = ['things'] as const
const queryFn = async () => ({ count: 1 })
describe('grantedQueryOptions', () => {
test('without a queryFn, enabled is false even if the caller asks for true', () => {
expect(grantedQueryOptions({ queryKey, queryFn: undefined }, { enabled: true }).enabled).toBe(
false
)
expect(
grantedQueryOptions({ queryKey, queryFn: undefined }, { enabled: () => true }).enabled
).toBe(false)
expect(grantedQueryOptions({ queryKey, queryFn: undefined }).enabled).toBe(false)
})
test('without a queryFn, uses a stable fallback function rather than undefined or skipToken', async () => {
const first = grantedQueryOptions({ queryKey, queryFn: undefined })
const second = grantedQueryOptions({ queryKey, queryFn: undefined })
expect(first.queryFn).toBeTypeOf('function')
expect(first.queryFn).not.toBe(skipToken)
expect(first.queryFn).toBe(second.queryFn)
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
await expect(queryClient.fetchQuery(first)).rejects.toThrow(`Missing queryFn: '["things"]'`)
})
test("with a queryFn, the caller's enabled passes through", () => {
expect(grantedQueryOptions({ queryKey, queryFn }).enabled).toBe(true)
expect(grantedQueryOptions({ queryKey, queryFn }, { enabled: false }).enabled).toBe(false)
const enabled = () => false
expect(grantedQueryOptions({ queryKey, queryFn }, { enabled }).enabled).toBe(enabled)
})
test('passes other caller options through', () => {
const result = grantedQueryOptions(
{ queryKey, queryFn },
{ staleTime: 5, refetchOnMount: 'always' }
)
expect(result.staleTime).toBe(5)
expect(result.refetchOnMount).toBe('always')
})
test('the factory queryKey and queryFn win over caller values', () => {
const result = grantedQueryOptions(
{ queryKey, queryFn },
{
// @ts-expect-error queryKey and queryFn are owned by the factory
queryKey: ['other'],
queryFn: async () => ({ count: 2 }),
}
)
expect(result.queryKey).toBe(queryKey)
expect(result.queryFn).toBe(queryFn)
})
test('select infers TData', () => {
const result = grantedQueryOptions<{ count: number }, Error, string>(
{ queryKey, queryFn },
{
select: (data) => {
expectTypeOf(data).toEqualTypeOf<{ count: number }>()
return String(data.count)
},
}
)
expect(result.select).toBeTypeOf('function')
expectTypeOf(result.select).not.toBeAny()
})
test('carries the brand the type-aware lint rule looks for', () => {
// fga/no-spread-granted-query-options detects this exact property name (scripts/eslint-rules/fga.cjs).
const result = grantedQueryOptions({ queryKey, queryFn })
expectTypeOf(result).toHaveProperty('__grantedQueryOptions')
})
})