Files
supabase/apps/studio/components/interfaces/SQLEditor/useSnippetIdentity.test.tsx
Charis 08c4f64c42 test(sql-editor): add mock-free hook tests (Step 4) (#48214)
## What

Step 4 of the SQL editor testability plan: **mock-free hook tests** for
the extracted SQL editor hooks, built on the Step 3 renderHook harness
(`tests/lib/sql-editor-test-utils.tsx`) — in-memory editor port + real
valtio stores + MSW. **Zero `vi.mock`.**

| File | Tests | Covers |
|------|-------|--------|
| `useSqlEditorExecution.test.tsx` | 8 | destructive-query gating
(`potentialIssues` vs. forced run), auto-limit suffixing,
connection-string → `x-connection-encrypted` header,
`onSuccess`/`onError` session-store writes, error-line highlight,
diff-open short-circuit |
| `useSqlEditorAi.test.tsx` | 7 | one-shot diff-request drain (empty vs.
non-empty editor), drain-exactly-once across remounts, accept/discard
diff, `onDebug` opening the assistant chat + debug prompt |
| `usePrettifyQuery.test.tsx` | 2 | in-place format + write-back,
diff-open no-op |
| `useSnippetIdentity.test.tsx` | 2 | generated identity + store-driven
loading state |
| `useSnippetTitleGenerator.test.tsx` | 2 | untitled-snippet naming via
the title endpoint |

Every test exercises real dependencies at the seam where they're real:
network via MSW, stores used real and reset per test, Monaco via the
in-memory editor port.

## Test plan

- [x] `pnpm test:studio -- SQLEditor` → **286/286 passing** (21 new
tests included)
- [x] `pnpm --filter studio typecheck` clean
- [x] Confirmed zero `vi.mock` in the new files


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Tests**
* Added comprehensive automated coverage for SQL query formatting,
snippet identity, and AI-generated titles.
* Added coverage for AI-assisted SQL editing, including diff acceptance,
rejection, debugging, and request handling.
* Added coverage for query execution, result persistence, safety checks,
replica selection, error highlighting, and diff-state behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-07-22 14:53:46 -04:00

43 lines
1.5 KiB
TypeScript

import { waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it } from 'vitest'
import { useSnippetIdentity } from './useSnippetIdentity'
import {
renderSqlEditorHook,
resetSqlEditorStores,
seedSnippet,
} from '@/tests/lib/sql-editor-test-utils'
/**
* `useParams` is globally stubbed to `{ ref: 'default' }` (no snippet id), so
* these tests exercise the generated-id branch. The pure id/loading derivation
* itself is proven exhaustively against `deriveSnippetIdentity` in
* `SQLEditor.utils.test`; here we assert the hook wires the store in.
*/
describe('useSnippetIdentity', () => {
beforeEach(() => {
resetSqlEditorStores()
})
it('generates a fresh snippet id + name when there is no URL id', () => {
const { result } = renderSqlEditorHook(useSnippetIdentity)
expect(result.current.urlId).toBeUndefined()
expect(result.current.id).toEqual(expect.any(String))
expect(result.current.id.length).toBeGreaterThan(0)
expect(result.current.generatedNewSnippetName).toEqual(expect.any(String))
expect(result.current.generatedNewSnippetName.length).toBeGreaterThan(0)
})
it('reports loading until the snippet content is present in the store', async () => {
const { result } = renderSqlEditorHook(useSnippetIdentity)
// The generated snippet has no content in the store yet.
expect(result.current.isLoading).toBe(true)
seedSnippet({ id: result.current.id, name: 'My query', sql: 'select 1;' })
await waitFor(() => expect(result.current.isLoading).toBe(false))
})
})