Files
supabase/apps/studio/components/interfaces/QueryInsights/QueryInsightsTable/QueryInsightsTable.utils.test.ts
T
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

137 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest'
import {
formatDuration,
formatQueryDisplay,
getColumnName,
getQueryType,
getTableName,
} from './QueryInsightsTable.utils'
describe('formatDuration', () => {
it('returns ms for values under 1000', () => {
expect(formatDuration(0)).toBe('0ms')
expect(formatDuration(500)).toBe('500ms')
expect(formatDuration(999)).toBe('999ms')
})
it('delegates to formatDurationLong for values >= 1000', () => {
expect(formatDuration(1000)).toBe('1.00s')
expect(formatDuration(60000)).toBe('1m')
})
})
describe('getQueryType', () => {
it('returns null for empty/null/undefined input', () => {
expect(getQueryType(null)).toBeNull()
expect(getQueryType(undefined)).toBeNull()
expect(getQueryType('')).toBeNull()
})
it('returns the SQL keyword for standard statement types', () => {
expect(getQueryType('SELECT * FROM users')).toBe('SELECT')
expect(getQueryType('INSERT INTO orders VALUES (1)')).toBe('INSERT')
expect(getQueryType('UPDATE users SET name = $1')).toBe('UPDATE')
expect(getQueryType('DELETE FROM logs')).toBe('DELETE')
expect(getQueryType('CREATE TABLE foo (id int)')).toBe('CREATE')
expect(getQueryType('DROP TABLE foo')).toBe('DROP')
expect(getQueryType('ALTER TABLE foo ADD COLUMN bar text')).toBe('ALTER')
expect(getQueryType('TRUNCATE foo')).toBe('TRUNCATE')
})
it('returns WITH for simple CTEs', () => {
expect(getQueryType('WITH cte AS (SELECT 1) SELECT * FROM cte')).toBe('WITH')
})
it('is case-insensitive', () => {
expect(getQueryType('select * from users')).toBe('SELECT')
expect(getQueryType('insert into foo values (1)')).toBe('INSERT')
})
})
describe('getTableName', () => {
it('returns null for empty/null/undefined input', () => {
expect(getTableName(null)).toBeNull()
expect(getTableName(undefined)).toBeNull()
expect(getTableName('')).toBeNull()
})
it('extracts table from SELECT FROM', () => {
expect(getTableName('SELECT * FROM users')).toBe('users')
expect(getTableName('SELECT id FROM public.orders WHERE id = 1')).toBe('orders')
})
it('extracts table from INSERT INTO', () => {
expect(getTableName('INSERT INTO orders (id) VALUES (1)')).toBe('orders')
})
it('extracts table from UPDATE', () => {
expect(getTableName('UPDATE users SET name = $1 WHERE id = 1')).toBe('users')
})
it('extracts table from DELETE FROM', () => {
expect(getTableName('DELETE FROM logs WHERE id = 1')).toBe('logs')
})
it('extracts table from CREATE TABLE', () => {
expect(getTableName('CREATE TABLE foo (id int)')).toBe('foo')
expect(getTableName('CREATE TABLE IF NOT EXISTS bar (id int)')).toBe('bar')
})
it('extracts table from ALTER TABLE', () => {
expect(getTableName('ALTER TABLE users ADD COLUMN email text')).toBe('users')
})
it('extracts table from DROP TABLE', () => {
expect(getTableName('DROP TABLE IF EXISTS old_table')).toBe('old_table')
})
it('extracts table from TRUNCATE', () => {
expect(getTableName('TRUNCATE TABLE logs')).toBe('logs')
expect(getTableName('TRUNCATE logs')).toBe('logs')
})
it('strips quotes', () => {
expect(getTableName('SELECT * FROM "my_table"')).toBe('my_table')
})
})
describe('getColumnName', () => {
it('returns null for empty/null/undefined input', () => {
expect(getColumnName(null)).toBeNull()
expect(getColumnName(undefined)).toBeNull()
expect(getColumnName('')).toBeNull()
})
it('extracts column from WHERE clause', () => {
expect(getColumnName('SELECT * FROM users WHERE id = 1')).toBe('id')
})
it('extracts column from ORDER BY when no WHERE clause', () => {
expect(getColumnName('SELECT * FROM users ORDER BY created_at')).toBe('created_at')
})
it('extracts column from GROUP BY', () => {
expect(getColumnName('SELECT status, count(*) FROM orders GROUP BY status')).toBe('status')
})
it('extracts column from UPDATE SET when no WHERE clause', () => {
expect(getColumnName('UPDATE users SET email = $1')).toBe('email')
})
it('extracts first column from INSERT INTO', () => {
expect(getColumnName('INSERT INTO users (id, name) VALUES ($1, $2)')).toBe('id')
})
})
describe('formatQueryDisplay', () => {
it('formats all three parts', () => {
expect(formatQueryDisplay('SELECT', 'users', 'id')).toBe('SELECT in users, id')
})
it('uses dash placeholders for null values', () => {
expect(formatQueryDisplay(null, null, null)).toBe('– in –, –')
expect(formatQueryDisplay('SELECT', null, null)).toBe('SELECT in –, –')
})
})