Files
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## Context

_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_

We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.

## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail 
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files 
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
2026-10-23 18:51:27 +08:00

158 lines
5.5 KiB
TypeScript

// @vitest-environment jsdom
import { QueryClient } from '@tanstack/react-query'
import { waitFor } from '@testing-library/react'
import { HttpResponse } from 'msw'
import { describe, expect, it, vi } from 'vitest'
import { acquireSharedRegistration, useAddDefinitions } from './useAddDefinitions'
import { customRenderHook } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
import { setupSqlEditorMocks } from '@/tests/lib/sql-editor-test-utils'
vi.mock('@/components/ui/CodeEditor/Providers/PgSQLCompletionProvider', () => ({
getPgsqlCompletionProvider: vi.fn((_monaco: unknown, pgInfoRef: unknown) => ({
__pgInfoRef: pgInfoRef,
})),
}))
vi.mock('@/components/ui/CodeEditor/Providers/PgSQLSignatureHelpProvider', () => ({
getPgsqlSignatureHelpProvider: vi.fn((_monaco: unknown, pgInfoRef: unknown) => ({
__pgInfoRef: pgInfoRef,
})),
}))
describe('acquireSharedRegistration', () => {
it('only registers once for multiple concurrent callers sharing a key', () => {
const register = vi.fn(() => ({ dispose: vi.fn() }))
acquireSharedRegistration('test-key-1', register)
acquireSharedRegistration('test-key-1', register)
acquireSharedRegistration('test-key-1', register)
expect(register).toHaveBeenCalledTimes(1)
})
it('registers independently per key', () => {
const register = vi.fn(() => ({ dispose: vi.fn() }))
acquireSharedRegistration('test-key-2a', register)
acquireSharedRegistration('test-key-2b', register)
expect(register).toHaveBeenCalledTimes(2)
})
it('does not dispose while other callers are still holding the registration', () => {
const dispose = vi.fn()
const register = vi.fn(() => ({ dispose }))
const releaseA = acquireSharedRegistration('test-key-3', register)
acquireSharedRegistration('test-key-3', register)
releaseA()
expect(dispose).not.toHaveBeenCalled()
})
it('disposes once the last caller releases', () => {
const dispose = vi.fn()
const register = vi.fn(() => ({ dispose }))
const releaseA = acquireSharedRegistration('test-key-4', register)
const releaseB = acquireSharedRegistration('test-key-4', register)
releaseA()
releaseB()
expect(dispose).toHaveBeenCalledTimes(1)
})
it('registers again after a full release cycle', () => {
const register = vi.fn(() => ({ dispose: vi.fn() }))
const release = acquireSharedRegistration('test-key-5', register)
release()
acquireSharedRegistration('test-key-5', register)
expect(register).toHaveBeenCalledTimes(2)
})
it('is safe to release more times than acquired', () => {
const dispose = vi.fn()
const register = vi.fn(() => ({ dispose }))
const release = acquireSharedRegistration('test-key-6', register)
release()
expect(() => release()).not.toThrow()
expect(dispose).toHaveBeenCalledTimes(1)
})
})
describe('useAddDefinitions', () => {
const createMonaco = () =>
({
languages: {
registerCompletionItemProvider: vi.fn(() => ({ dispose: vi.fn() })),
registerSignatureHelpProvider: vi.fn(() => ({ dispose: vi.fn() })),
registerDocumentFormattingEditProvider: vi.fn(() => ({ dispose: vi.fn() })),
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}) as any
it('keeps the registered provider reading fresh data after the registering editor unmounts, as long as a sibling editor is still active', async () => {
const { getPgsqlCompletionProvider } =
(await import('@/components/ui/CodeEditor/Providers/PgSQLCompletionProvider')) as unknown as {
getPgsqlCompletionProvider: ReturnType<typeof vi.fn>
}
let keywordWords = ['select']
setupSqlEditorMocks()
addAPIMock({
method: 'post',
path: '/platform/pg-meta/:ref/query',
response: ({ request }) => {
const key = new URL(request.url).searchParams.get('key')
if (key === 'keywords') return HttpResponse.json(keywordWords.map((word) => ({ word })))
return HttpResponse.json([])
},
})
const monaco = createMonaco()
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
const renderOptions = { queryClient }
// Two sibling notebook cells' editors, both mounted at once, sharing the same cache.
const editor1 = customRenderHook(
() => useAddDefinitions('', monaco, { enabled: true }),
renderOptions
)
const editor2 = customRenderHook(
() => useAddDefinitions('', monaco, { enabled: true }),
renderOptions
)
await waitFor(() =>
expect(monaco.languages.registerCompletionItemProvider).toHaveBeenCalledTimes(1)
)
// The first (and only, thanks to ref-counted registration) call registered the provider —
// capture the `pgInfoRef` it reads from.
const pgInfoRef = getPgsqlCompletionProvider.mock.calls[0][1] as {
current: { keywords: string[] }
}
await waitFor(() => expect(pgInfoRef.current.keywords).toEqual(['select']))
// Editor 1 — the one whose render happened to trigger the registration — closes. Editor 2
// is still open, so the provider must stay registered and stay current.
editor1.unmount()
expect(monaco.languages.registerCompletionItemProvider).toHaveBeenCalledTimes(1)
keywordWords = ['select', 'insert']
queryClient.invalidateQueries({ queryKey: ['projects', 'default', 'keywords'] })
editor2.rerender()
await waitFor(() => expect(pgInfoRef.current.keywords).toEqual(['select', 'insert']))
editor2.unmount()
})
})