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
126 lines
4.8 KiB
TypeScript
126 lines
4.8 KiB
TypeScript
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import { toast } from 'sonner'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { RoleImpersonationSelectorInterface } from '@/components/interfaces/RoleImpersonationSelector'
|
|
import type { ImpersonationRole } from '@/lib/role-impersonation'
|
|
import type { RoleImpersonationController } from '@/state/role-impersonation-state'
|
|
import { customRender } from '@/tests/lib/custom-render'
|
|
|
|
vi.mock('sonner', () => ({ toast: { error: vi.fn() } }))
|
|
|
|
vi.mock('@/components/interfaces/RoleImpersonationSelector/UserImpersonationSelector', () => ({
|
|
UserImpersonationSelector: ({ disabled }: { disabled?: boolean }) => (
|
|
<fieldset data-testid="user-settings" disabled={disabled}>
|
|
<button type="button" tabIndex={0}>
|
|
Project
|
|
</button>
|
|
</fieldset>
|
|
),
|
|
}))
|
|
|
|
describe('RoleImpersonationSelectorInterface', () => {
|
|
it('enables user settings for authenticated queries and summarizes each role', async () => {
|
|
const user = userEvent.setup()
|
|
let currentRole: ImpersonationRole | undefined
|
|
const setRole = vi.fn(async (role: ImpersonationRole | undefined) => {
|
|
currentRole = role
|
|
})
|
|
const state: RoleImpersonationController = {
|
|
get role() {
|
|
return currentRole
|
|
},
|
|
claims: undefined,
|
|
setRole,
|
|
}
|
|
|
|
const { rerender } = customRender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
expect(screen.queryByText('Role')).not.toBeInTheDocument()
|
|
expect(screen.getByRole('radio', { name: 'PostgresSuperuser' })).toBeChecked()
|
|
expect(screen.getByText('Bypasses RLS and can return all rows.')).toBeVisible()
|
|
expect(screen.getByTestId('user-settings')).toBeDisabled()
|
|
|
|
await user.click(screen.getByRole('radio', { name: 'AuthenticatedLogged-in user' }))
|
|
|
|
expect(screen.getByText('Returns rows available to the selected user.')).toBeVisible()
|
|
expect(screen.getByTestId('user-settings')).not.toBeDisabled()
|
|
|
|
await user.click(screen.getByRole('radio', { name: 'AnonymousNot logged in' }))
|
|
rerender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
expect(screen.getByText('Returns rows available to anonymous users.')).toBeVisible()
|
|
expect(setRole).toHaveBeenCalledWith({ type: 'postgrest', role: 'anon' })
|
|
expect(screen.getByTestId('user-settings')).toBeDisabled()
|
|
|
|
await user.click(screen.getByRole('radio', { name: 'PostgresSuperuser' }))
|
|
rerender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
expect(screen.getByText('Bypasses RLS and can return all rows.')).toBeVisible()
|
|
expect(setRole).toHaveBeenCalledWith(undefined)
|
|
})
|
|
|
|
it('follows externally cleared role state', () => {
|
|
let currentRole: ImpersonationRole | undefined = {
|
|
type: 'postgrest',
|
|
role: 'authenticated',
|
|
userType: 'native',
|
|
}
|
|
const state: RoleImpersonationController = {
|
|
get role() {
|
|
return currentRole
|
|
},
|
|
claims: undefined,
|
|
setRole: vi.fn(),
|
|
}
|
|
|
|
const { rerender } = customRender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
expect(screen.getByRole('radio', { name: 'AuthenticatedLogged-in user' })).toBeChecked()
|
|
expect(screen.getByTestId('user-settings')).not.toBeDisabled()
|
|
|
|
currentRole = undefined
|
|
rerender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
expect(screen.getByRole('radio', { name: 'PostgresSuperuser' })).toBeChecked()
|
|
expect(screen.getByTestId('user-settings')).toBeDisabled()
|
|
})
|
|
|
|
it('allows switching roles while impersonating a user', async () => {
|
|
const user = userEvent.setup()
|
|
const setRole = vi.fn().mockResolvedValue(undefined)
|
|
const state: RoleImpersonationController = {
|
|
role: {
|
|
type: 'postgrest',
|
|
role: 'authenticated',
|
|
userType: 'external',
|
|
externalAuth: { sub: 'external-user', additionalClaims: {} },
|
|
},
|
|
claims: undefined,
|
|
setRole,
|
|
}
|
|
|
|
customRender(<RoleImpersonationSelectorInterface state={state} />)
|
|
|
|
await user.click(screen.getByRole('radio', { name: 'AnonymousNot logged in' }))
|
|
|
|
expect(setRole).toHaveBeenCalledWith({ type: 'postgrest', role: 'anon' })
|
|
})
|
|
|
|
it('keeps the current selection and reports rejected role switches', async () => {
|
|
const user = userEvent.setup()
|
|
const state: RoleImpersonationController = {
|
|
role: undefined,
|
|
claims: undefined,
|
|
setRole: vi.fn().mockRejectedValue({ message: 'Role switch failed' }),
|
|
}
|
|
|
|
customRender(<RoleImpersonationSelectorInterface state={state} />)
|
|
await user.click(screen.getByRole('radio', { name: 'AnonymousNot logged in' }))
|
|
|
|
expect(screen.getByRole('radio', { name: 'PostgresSuperuser' })).toBeChecked()
|
|
expect(toast.error).toHaveBeenCalledWith('Failed to impersonate user: Role switch failed')
|
|
})
|
|
})
|