Files
supabase/apps/studio/lib/__tests__/theme-overrides.test.ts
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

175 lines
6.0 KiB
TypeScript

// @vitest-environment jsdom
import { describe, expect, it } from 'vitest'
import {
applyThemeOverrides,
clearThemeOverridesForMode,
getThemeOverrideRange,
getThemeOverrideValue,
hasThemeOverrides,
mergeThemeOverride,
parseThemeOverridesByMode,
resolveThemeOverrideMode,
resolveThemeOverridesForFlags,
sliderValueToThemeOverride,
THEME_OVERRIDE_DEFAULTS,
THEME_OVERRIDE_KNOBS,
ThemeOverrideMode,
themeOverrideToSliderValue,
} from '@/lib/theme-overrides'
const knob = (key: string) => {
const found = THEME_OVERRIDE_KNOBS.find((candidate) => candidate.key === key)
if (found === undefined) throw new Error(`No knob found for ${key}`)
return found
}
describe.each<ThemeOverrideMode>(['dark', 'light'])('%s theme slider mappings', (mode) => {
it.each(THEME_OVERRIDE_KNOBS)(
'maps $label endpoints to its safe range and clamps outside it',
(setting) => {
const range = getThemeOverrideRange(setting, mode)
expect(sliderValueToThemeOverride(setting, mode, 0)).toBe(range.min)
expect(sliderValueToThemeOverride(setting, mode, 100)).toBe(range.max)
expect(themeOverrideToSliderValue(setting, mode, range.min)).toBe(0)
expect(themeOverrideToSliderValue(setting, mode, range.max)).toBe(100)
expect(themeOverrideToSliderValue(setting, mode, Number.NEGATIVE_INFINITY)).toBe(0)
expect(themeOverrideToSliderValue(setting, mode, Number.POSITIVE_INFINITY)).toBe(100)
expect(sliderValueToThemeOverride(setting, mode, -1)).toBe(range.min)
expect(sliderValueToThemeOverride(setting, mode, 101)).toBe(range.max)
}
)
it.each(THEME_OVERRIDE_KNOBS)('round trips $label positions', (setting) => {
for (const sliderValue of [0, 17, 50, 83, 100]) {
const rawValue = sliderValueToThemeOverride(setting, mode, sliderValue)
expect(themeOverrideToSliderValue(setting, mode, rawValue)).toBe(sliderValue)
}
})
})
describe('dark theme surface elevation', () => {
it('keeps the base surface independent while giving raised layers meaningful contrast', () => {
const surface = getThemeOverrideRange(knob('surface'), 'dark').min
const elevationStep = getThemeOverrideRange(knob('elevationStep'), 'dark').max
expect(surface).toBe(0.12)
expect(surface + elevationStep).toBeCloseTo(0.36)
expect(surface + elevationStep * 1.5).toBeCloseTo(0.48)
})
})
describe('theme override storage', () => {
it('keeps sibling settings and modes', () => {
const merged = mergeThemeOverride(
{ dark: { chroma: 0.02 }, light: { surface: 0.9 } },
'dark',
'contrast',
0.7
)
expect(merged).toEqual({
dark: { chroma: 0.02, contrast: 0.7 },
light: { surface: 0.9 },
})
})
it('removes an override returned to its displayed default', () => {
const merged = mergeThemeOverride(
{ dark: { chroma: 0.02, contrast: 0.7 } },
'dark',
'contrast',
THEME_OVERRIDE_DEFAULTS.dark.contrast
)
expect(merged).toEqual({ dark: { chroma: 0.02 } })
})
it('removes an empty mode returned to its displayed defaults', () => {
const merged = mergeThemeOverride(
{ dark: { contrast: 0.7 }, light: { surface: 0.9 } },
'dark',
'contrast',
THEME_OVERRIDE_DEFAULTS.dark.contrast
)
expect(merged).toEqual({ light: { surface: 0.9 } })
})
it('clears one mode without leaving an empty entry', () => {
expect(
clearThemeOverridesForMode({ dark: { contrast: 0.7 }, light: { surface: 0.9 } }, 'dark')
).toEqual({ light: { surface: 0.9 } })
})
it('parses legacy values, strips unknown data, and clamps each mode', () => {
expect(
parseThemeOverridesByMode({
dark: { chroma: 99, surface: 0.2, unknown: 123 },
light: { contrast: -5 },
other: { surface: 0 },
})
).toEqual({ dark: { chroma: 0.04, surface: 0.2 }, light: { contrast: 0.45 } })
})
it.each([null, [], 'invalid', { dark: { chroma: 'high' } }])(
'falls back for malformed data: %j',
(value) => expect(parseThemeOverridesByMode(value)).toEqual({})
)
})
describe('theme override application', () => {
it('uses the mode default when a setting has no override', () => {
expect(getThemeOverrideValue(knob('surface'), 'dark', {})).toBe(
THEME_OVERRIDE_DEFAULTS.dark.surface
)
expect(getThemeOverrideValue(knob('surface'), 'light', {})).toBe(
THEME_OVERRIDE_DEFAULTS.light.surface
)
})
it('writes stored settings and hands missing settings back to CSS', () => {
const root = document.createElement('html')
root.style.setProperty('--contrast', '0.7')
applyThemeOverrides(root, 'dark', { chroma: 0.02 })
expect(root.style.getPropertyValue('--chroma')).toBe('0.02')
expect(root.style.getPropertyValue('--contrast')).toBe('')
})
it('applies a spot hue via --primary-hue and leaves brand and surface hues alone', () => {
const root = document.createElement('html')
applyThemeOverrides(root, 'dark', { primaryHue: 48 })
expect(root.style.getPropertyValue('--primary-hue')).toBe('48')
expect(root.style.getPropertyValue('--surface-hue')).toBe('')
expect(root.style.getPropertyValue('--brand-default')).toBe('')
applyThemeOverrides(root, 'dark', {})
expect(root.style.getPropertyValue('--primary-hue')).toBe('')
})
it('reports whether a mode has overrides', () => {
expect(hasThemeOverrides({})).toBe(false)
expect(hasThemeOverrides({ chroma: 0.02 })).toBe(true)
})
it('strips spot hue when the employee-only flag is off', () => {
expect(
resolveThemeOverridesForFlags({ primaryHue: 48, chroma: 0.02 }, { isSpotColorEnabled: false })
).toEqual({ chroma: 0.02 })
expect(
resolveThemeOverridesForFlags({ primaryHue: 48, chroma: 0.02 }, { isSpotColorEnabled: true })
).toEqual({ primaryHue: 48, chroma: 0.02 })
})
it('resolves classic dark as dark for legacy stored themes', () => {
expect(resolveThemeOverrideMode('dark')).toBe('dark')
expect(resolveThemeOverrideMode('light')).toBe('light')
expect(resolveThemeOverrideMode(undefined)).toBe('light')
})
})