import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter } from 'react-router-dom'
import { createFakeSupabase, type FakeSupabase } from '../helpers/fakeSupabase'
const mocks = vi.hoisted(() => ({ fake: null as unknown }))
vi.mock('@/lib/supabase', () => ({
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
isSupabaseConfigured: () => mocks.fake !== null,
}))
import SignupRoute from '@/app/routes/auth/SignupRoute'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
let fake: FakeSupabase
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
fake = createFakeSupabase()
mocks.fake = fake
})
afterEach(cleanup)
function mount() {
return render(
,
)
}
async function fill(user: ReturnType, password: string, confirm = password) {
await user.type(screen.getByLabelText(fr['auth.signup.display_name']), 'Alice')
await user.type(screen.getByLabelText(fr['auth.email']), 'alice@example.test')
await user.type(screen.getByLabelText(fr['auth.password']), password)
await user.type(screen.getByLabelText(fr['auth.password_confirm']), confirm)
await user.click(screen.getByRole('button', { name: fr['auth.signup.submit'] }))
}
describe('SignupRoute', () => {
it('enforces the password policy and the confirmation client-side', async () => {
const user = userEvent.setup()
mount()
await fill(user, 'weak', 'weak')
expect(screen.getByText(fr['auth.error.password_rules'])).toBeInTheDocument()
expect(fake.auth.signUp).not.toHaveBeenCalled()
cleanup()
mount()
await fill(user, 'CorrectHorse12', 'CorrectHorse13')
expect(screen.getByText(fr['auth.error.password_mismatch'])).toBeInTheDocument()
})
it('sends display name + locale as metadata and shows the inbox step', async () => {
fake.auth.signUp.mockResolvedValue({ data: { session: null, user: null }, error: null })
const user = userEvent.setup()
mount()
await fill(user, 'CorrectHorse12')
expect(await screen.findByText(fr['auth.signup.check_inbox_title'])).toBeInTheDocument()
expect(screen.getByText(/alice@example\.test/)).toBeInTheDocument()
const [call] = fake.auth.signUp.mock.calls[0] as [Record]
expect(call).toMatchObject({
email: 'alice@example.test',
password: 'CorrectHorse12',
options: {
data: { display_name: 'Alice', locale: 'fr' },
},
})
const options = call.options as { emailRedirectTo: string }
expect(options.emailRedirectTo).toMatch(/\/auth\/callback$/)
})
it('tells when the address is already taken', async () => {
fake.auth.signUp.mockResolvedValue({
data: { session: null, user: null },
error: { code: 'user_already_exists', message: 'User already registered', status: 422 },
})
const user = userEvent.setup()
mount()
await fill(user, 'CorrectHorse12')
expect(await screen.findByText(fr['auth.failure.user_exists'])).toBeInTheDocument()
})
})