mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
## 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
136 lines
3.0 KiB
TypeScript
136 lines
3.0 KiB
TypeScript
import { act, renderHook } from '@testing-library/react'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { useConfirmOnClose } from './useConfirmOnClose'
|
|
|
|
describe('useConfirmOnClose', () => {
|
|
it('closes immediately when the form is clean', () => {
|
|
const onClose = vi.fn()
|
|
|
|
const { result } = renderHook(() =>
|
|
useConfirmOnClose({
|
|
checkIsDirty: () => false,
|
|
onClose,
|
|
})
|
|
)
|
|
|
|
act(() => {
|
|
result.current.confirmOnClose()
|
|
})
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
expect(result.current.modalProps.visible).toBe(false)
|
|
})
|
|
|
|
it('ignores open events and handles close events via handleOpenChange', () => {
|
|
const onClose = vi.fn()
|
|
|
|
const { result } = renderHook(() =>
|
|
useConfirmOnClose({
|
|
checkIsDirty: () => true,
|
|
onClose,
|
|
})
|
|
)
|
|
|
|
act(() => {
|
|
result.current.handleOpenChange(true)
|
|
})
|
|
|
|
expect(onClose).not.toHaveBeenCalled()
|
|
expect(result.current.modalProps.visible).toBe(false)
|
|
|
|
act(() => {
|
|
result.current.handleOpenChange(false)
|
|
})
|
|
|
|
expect(onClose).not.toHaveBeenCalled()
|
|
expect(result.current.modalProps.visible).toBe(true)
|
|
})
|
|
|
|
it('confirms and closes after the discard modal is accepted', () => {
|
|
const onClose = vi.fn()
|
|
|
|
const { result } = renderHook(() =>
|
|
useConfirmOnClose({
|
|
checkIsDirty: () => true,
|
|
onClose,
|
|
})
|
|
)
|
|
|
|
act(() => {
|
|
result.current.confirmOnClose()
|
|
})
|
|
|
|
expect(result.current.modalProps.visible).toBe(true)
|
|
|
|
act(() => {
|
|
result.current.modalProps.onClose()
|
|
})
|
|
|
|
expect(onClose).toHaveBeenCalledTimes(1)
|
|
expect(result.current.modalProps.visible).toBe(false)
|
|
})
|
|
|
|
it('cancels and keeps the form open after the discard modal is dismissed', () => {
|
|
const onClose = vi.fn()
|
|
|
|
const { result } = renderHook(() =>
|
|
useConfirmOnClose({
|
|
checkIsDirty: () => true,
|
|
onClose,
|
|
})
|
|
)
|
|
|
|
act(() => {
|
|
result.current.confirmOnClose()
|
|
})
|
|
|
|
expect(result.current.modalProps.visible).toBe(true)
|
|
|
|
act(() => {
|
|
result.current.modalProps.onCancel()
|
|
})
|
|
|
|
expect(onClose).not.toHaveBeenCalled()
|
|
expect(result.current.modalProps.visible).toBe(false)
|
|
})
|
|
|
|
it('uses the latest checkIsDirty and onClose callbacks', () => {
|
|
const onCloseA = vi.fn()
|
|
const onCloseB = vi.fn()
|
|
let isDirty = false
|
|
|
|
const { result, rerender } = renderHook(
|
|
({ onClose }: { onClose: () => void }) =>
|
|
useConfirmOnClose({
|
|
checkIsDirty: () => isDirty,
|
|
onClose,
|
|
}),
|
|
{
|
|
initialProps: { onClose: onCloseA },
|
|
}
|
|
)
|
|
|
|
act(() => {
|
|
result.current.confirmOnClose()
|
|
})
|
|
|
|
expect(onCloseA).toHaveBeenCalledTimes(1)
|
|
|
|
isDirty = true
|
|
rerender({ onClose: onCloseB })
|
|
|
|
act(() => {
|
|
result.current.confirmOnClose()
|
|
})
|
|
|
|
expect(result.current.modalProps.visible).toBe(true)
|
|
|
|
act(() => {
|
|
result.current.modalProps.onClose()
|
|
})
|
|
|
|
expect(onCloseB).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|