mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +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
150 lines
4.1 KiB
TypeScript
150 lines
4.1 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { PublishableAPIKeys } from './PublishableAPIKeys'
|
|
import { customRender as render } from '@/tests/lib/custom-render'
|
|
|
|
const {
|
|
mockIsPlatform,
|
|
mockUseAPIKeysQuery,
|
|
mockUseAsyncCheckPermissions,
|
|
mockUseAPIKeyDeleteMutation,
|
|
} = vi.hoisted(() => ({
|
|
mockIsPlatform: { value: true },
|
|
mockUseAPIKeysQuery: vi.fn(),
|
|
mockUseAsyncCheckPermissions: vi.fn(),
|
|
mockUseAPIKeyDeleteMutation: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('common', async () => {
|
|
const actual = await vi.importActual<Record<string, unknown>>('common')
|
|
return {
|
|
...actual,
|
|
get IS_PLATFORM() {
|
|
return mockIsPlatform.value
|
|
},
|
|
useParams: () => ({ ref: 'default' }),
|
|
}
|
|
})
|
|
|
|
vi.mock('@/data/api-keys/api-keys-query', () => ({
|
|
useAPIKeysQuery: mockUseAPIKeysQuery,
|
|
}))
|
|
|
|
vi.mock('@/data/api-keys/api-key-delete-mutation', () => ({
|
|
useAPIKeyDeleteMutation: mockUseAPIKeyDeleteMutation,
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useCheckPermissionsV2', () => ({
|
|
useAsyncCheckPermissionsV2: mockUseAsyncCheckPermissions,
|
|
FGA_PERMISSIONS: { PROJECT: { API_GATEWAY_KEYS_READ: 'api_gateway_keys_read' } },
|
|
}))
|
|
|
|
vi.mock('./CreatePublishableAPIKeyDialog', () => ({
|
|
CreatePublishableAPIKeyDialog: () => <div>CreatePublishableAPIKeyDialog</div>,
|
|
}))
|
|
|
|
vi.mock('./APIKeyRow', () => ({
|
|
APIKeyRow: ({ apiKey }: { apiKey: { id: string; name: string } }) => (
|
|
<tr>
|
|
<td>api-key-row:{apiKey.name}</td>
|
|
</tr>
|
|
),
|
|
}))
|
|
|
|
const publishableKey = {
|
|
id: 'pk-1',
|
|
name: 'default-publishable',
|
|
type: 'publishable' as const,
|
|
api_key: 'sb_publishable_xyz',
|
|
}
|
|
|
|
const secretKey = {
|
|
id: 'sk-1',
|
|
name: 'default-secret',
|
|
type: 'secret' as const,
|
|
api_key: 'sb_secret_xyz',
|
|
}
|
|
|
|
describe('PublishableAPIKeys', () => {
|
|
beforeEach(() => {
|
|
mockIsPlatform.value = true
|
|
mockUseAsyncCheckPermissions.mockReturnValue({ can: true, isLoading: false })
|
|
mockUseAPIKeyDeleteMutation.mockReturnValue({
|
|
mutate: vi.fn(),
|
|
isPending: false,
|
|
isSuccess: false,
|
|
})
|
|
})
|
|
|
|
it('renders the table with publishable keys and the create button on platform', () => {
|
|
mockUseAPIKeysQuery.mockReturnValue({
|
|
data: [publishableKey],
|
|
isPending: false,
|
|
isSuccess: true,
|
|
isError: false,
|
|
})
|
|
|
|
render(<PublishableAPIKeys />)
|
|
|
|
expect(screen.getByText('api-key-row:default-publishable')).toBeInTheDocument()
|
|
expect(screen.getByText('CreatePublishableAPIKeyDialog')).toBeInTheDocument()
|
|
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('on platform with secret-only keys, shows the inline "No publishable keys created yet" admonition', () => {
|
|
mockUseAPIKeysQuery.mockReturnValue({
|
|
data: [secretKey],
|
|
isPending: false,
|
|
isSuccess: true,
|
|
isError: false,
|
|
})
|
|
|
|
render(<PublishableAPIKeys />)
|
|
|
|
expect(screen.getByText('No publishable keys created yet')).toBeInTheDocument()
|
|
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('on platform with no keys, does NOT show the self-hosted empty-state card', () => {
|
|
mockUseAPIKeysQuery.mockReturnValue({
|
|
data: [],
|
|
isPending: false,
|
|
isSuccess: true,
|
|
isError: false,
|
|
})
|
|
|
|
render(<PublishableAPIKeys />)
|
|
|
|
expect(screen.queryByText('No publishable API keys found')).not.toBeInTheDocument()
|
|
})
|
|
|
|
it('on self-hosted with no publishable keys, shows the empty-state card', () => {
|
|
mockIsPlatform.value = false
|
|
mockUseAPIKeysQuery.mockReturnValue({
|
|
data: [],
|
|
isPending: false,
|
|
isSuccess: true,
|
|
isError: false,
|
|
})
|
|
|
|
render(<PublishableAPIKeys />)
|
|
|
|
expect(screen.getByText('No publishable API keys found')).toBeInTheDocument()
|
|
})
|
|
|
|
it('hides the create button on non-platform', () => {
|
|
mockIsPlatform.value = false
|
|
mockUseAPIKeysQuery.mockReturnValue({
|
|
data: [publishableKey],
|
|
isPending: false,
|
|
isSuccess: true,
|
|
isError: false,
|
|
})
|
|
|
|
render(<PublishableAPIKeys />)
|
|
|
|
expect(screen.queryByText('CreatePublishableAPIKeyDialog')).not.toBeInTheDocument()
|
|
})
|
|
})
|