Files
supabase/apps/studio/lib/external-identity-providers.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

84 lines
3.4 KiB
TypeScript

// @vitest-environment jsdom
import { afterEach, describe, expect, test, vi } from 'vitest'
import {
buildProviderAuthRedirect,
getIdentityProviderConfig,
getProviderDisplay,
normalizeIconPath,
} from './external-identity-providers'
describe('external identity providers', () => {
test('normalizes relative icon paths against the dashboard base path', () => {
expect(normalizeIconPath('/img/icons/github-icon.svg')).toBe('/img/icons/github-icon.svg')
expect(normalizeIconPath('img/icons/github-icon.svg')).toBe('/img/icons/github-icon.svg')
expect(normalizeIconPath('https://example.com/icon.svg')).toBe('https://example.com/icon.svg')
})
test('resolves static provider config by provider id or auth provider', () => {
expect(getIdentityProviderConfig('github')?.displayName).toBe('GitHub')
expect(getIdentityProviderConfig('unknown')).toBeUndefined()
})
test('returns display metadata for static, built-in, and fallback providers', () => {
expect(getProviderDisplay('github').displayName).toBe('GitHub')
expect(getProviderDisplay('email').displayName).toBe('Email')
expect(getProviderDisplay('sso:test').displayName).toBe('SSO')
expect(getProviderDisplay('my_provider').displayName).toBe('my provider')
})
test('derives a title-cased name for unregistered custom providers', () => {
expect(getProviderDisplay('custom:acme').displayName).toBe('Acme')
expect(getProviderDisplay('Custom:Acme').displayName).toBe('Acme')
expect(getProviderDisplay('custom:my_provider').displayName).toBe('My Provider')
expect(getProviderDisplay('custom:MY_PROVIDER').displayName).toBe('My Provider')
// registered custom providers keep their configured display name
expect(getProviderDisplay('custom:openai').displayName).toBe('ChatGPT')
})
test('marks static provider icons as monochrome but not built-in or fallback icons', () => {
expect(getProviderDisplay('github').hasMonochromeIcon).toBe(true)
expect(getProviderDisplay('email').hasMonochromeIcon).toBeUndefined()
expect(getProviderDisplay('sso:test').hasMonochromeIcon).toBeUndefined()
})
describe('buildProviderAuthRedirect', () => {
afterEach(() => {
vi.unstubAllEnvs()
})
test('builds the MFA-check URL against the configured site URL', () => {
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com/dashboard')
expect(buildProviderAuthRedirect('github')).toBe(
'https://supabase.com/dashboard/sign-in-mfa?method=github'
)
})
test('URL-encodes custom provider ids in the method param', () => {
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com/dashboard')
expect(buildProviderAuthRedirect('custom:example')).toBe(
'https://supabase.com/dashboard/sign-in-mfa?method=custom%3Aexample'
)
})
test('appends an encoded returnTo destination when provided', () => {
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com/dashboard')
expect(buildProviderAuthRedirect('custom:example', '/account/me')).toBe(
'https://supabase.com/dashboard/sign-in-mfa?method=custom%3Aexample&returnTo=%2Faccount%2Fme'
)
})
test('uses the current origin on Vercel preview deployments', () => {
vi.stubEnv('NEXT_PUBLIC_VERCEL_ENV', 'preview')
vi.stubEnv('NEXT_PUBLIC_SITE_URL', 'https://supabase.com/dashboard')
expect(buildProviderAuthRedirect('github')).toBe(
`${location.origin}/sign-in-mfa?method=github`
)
})
})
})