mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +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
127 lines
4.0 KiB
TypeScript
127 lines
4.0 KiB
TypeScript
import { fireEvent, screen, waitFor } from '@testing-library/react'
|
|
import { mockAnimationsApi } from 'jsdom-testing-mocks'
|
|
import { HttpResponse } from 'msw'
|
|
import { toast } from 'sonner'
|
|
import { beforeEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { DeleteProjectModal } from './DeleteProjectModal'
|
|
import type { OrgProject } from '@/data/projects/org-projects-infinite-query'
|
|
import { createMockOrganization } from '@/tests/helpers'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
|
|
mockAnimationsApi()
|
|
|
|
// Heavy child with its own queries and permission checks, unrelated to this flow
|
|
vi.mock('@/components/layouts/ProjectLayout/LogicalBackupCliInstructions', () => ({
|
|
LogicalBackupCliInstructions: () => null,
|
|
}))
|
|
|
|
const PROJECT: OrgProject = {
|
|
ref: 'abcdefghijklmnopqrst',
|
|
name: 'Project 1',
|
|
status: 'ACTIVE_HEALTHY',
|
|
cloud_provider: 'AWS',
|
|
region: 'us-east-1',
|
|
inserted_at: '2024-01-01T00:00:00Z',
|
|
integration_source: null,
|
|
is_branch: false,
|
|
databases: [],
|
|
}
|
|
const ORGANIZATION = createMockOrganization({ slug: 'org-slug', plan: { id: 'pro', name: 'Pro' } })
|
|
|
|
const mockSubscription = (planId: 'free' | 'pro') => {
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/organizations/:slug/billing/subscription',
|
|
response: {
|
|
addons: [],
|
|
billing_via_partner: false,
|
|
current_period_end: 1700000000,
|
|
current_period_start: 1700000000,
|
|
next_invoice_at: 1700000000,
|
|
payment_method_type: 'card',
|
|
plan: { id: planId, name: planId },
|
|
project_addons: [],
|
|
usage_billing_enabled: false,
|
|
},
|
|
})
|
|
}
|
|
|
|
// Records the order requests hit the API, since the ordering is the contract under test
|
|
const mockSurveyAndDelete = ({ surveyStatus }: { surveyStatus: 201 | 403 }) => {
|
|
const calls: string[] = []
|
|
|
|
// The selected-project hook fires alongside the props; the component prefers the props
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/projects/:ref',
|
|
response: () => HttpResponse.json<APIErrorBody>({ message: 'Not found' }, { status: 404 }),
|
|
})
|
|
addAPIMock({
|
|
method: 'post',
|
|
path: '/platform/feedback/downgrade',
|
|
response: () => {
|
|
calls.push('survey')
|
|
if (surveyStatus === 403) {
|
|
return HttpResponse.json<APIErrorBody>({ message: 'Forbidden' }, { status: 403 })
|
|
}
|
|
return HttpResponse.json<{ result: string }>({ result: 'success' }, { status: 201 })
|
|
},
|
|
})
|
|
addAPIMock({
|
|
method: 'delete',
|
|
path: '/platform/projects/:ref',
|
|
response: () => {
|
|
calls.push('delete')
|
|
return HttpResponse.json<{ id: number; name: string; ref: string }>({
|
|
id: 1,
|
|
name: PROJECT.name,
|
|
ref: PROJECT.ref,
|
|
})
|
|
},
|
|
})
|
|
|
|
return calls
|
|
}
|
|
|
|
const deleteProject = async () => {
|
|
const [reason] = await screen.findAllByRole('radio', { hidden: true })
|
|
fireEvent.click(reason)
|
|
fireEvent.change(screen.getByPlaceholderText('Type the project name in here'), {
|
|
target: { value: PROJECT.name },
|
|
})
|
|
fireEvent.click(screen.getByRole('button', { name: 'I understand, delete this project' }))
|
|
}
|
|
|
|
describe('DeleteProjectModal', () => {
|
|
beforeEach(() => {
|
|
vi.spyOn(toast, 'error')
|
|
})
|
|
|
|
test('sends the exit survey before deleting a paid project', async () => {
|
|
mockSubscription('pro')
|
|
const calls = mockSurveyAndDelete({ surveyStatus: 201 })
|
|
|
|
customRender(
|
|
<DeleteProjectModal visible onClose={vi.fn()} project={PROJECT} organization={ORGANIZATION} />
|
|
)
|
|
await deleteProject()
|
|
|
|
await waitFor(() => expect(calls).toEqual(['survey', 'delete']))
|
|
})
|
|
|
|
test('still deletes the project, without an error toast, when the survey fails', async () => {
|
|
mockSubscription('pro')
|
|
const calls = mockSurveyAndDelete({ surveyStatus: 403 })
|
|
|
|
customRender(
|
|
<DeleteProjectModal visible onClose={vi.fn()} project={PROJECT} organization={ORGANIZATION} />
|
|
)
|
|
await deleteProject()
|
|
|
|
await waitFor(() => expect(calls).toEqual(['survey', 'delete']))
|
|
expect(toast.error).not.toHaveBeenCalled()
|
|
})
|
|
})
|