Files
faunosteo/apps/app/tests/unit/routes.LoginRoute.test.tsx
T
bchanot aa1985eead test(app): identity step — auth store, locale sync, login/signup, guards, shell, dashboard, account
- tests/helpers/fakeSupabase: in-memory auth events + profiles/profile_roles builder
- validation schemas and safeNextPath; classifyAuthError mapping (packages/supabase)
- LoginRoute (unavailable, validation, next path, phrased failure, OAuth buttons, show password)
- SignupRoute (policy, mismatch, metadata + redirect, user exists)
- RequireAuth / RedirectIfSignedIn, AppLayout (nav, active, drawer, sign out), Dashboard, Account forms
2026-10-11 23:33:25 +02:00

125 lines
4.6 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import { createFakeSupabase, fakeSession, type FakeSupabase } from '../helpers/fakeSupabase'
const mocks = vi.hoisted(() => ({ fake: null as unknown, providers: [] as string[] }))
vi.mock('@/lib/supabase', () => ({
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
isSupabaseConfigured: () => mocks.fake !== null,
}))
vi.mock('@/app/providers/ConfigProvider', async (importOriginal) => {
const mod = await importOriginal<typeof import('@/app/providers/ConfigProvider')>()
return {
...mod,
useConfig: () => ({
siteName: 'Faunosteo',
brandName: 'Faunosteo',
footerName: 'Faunosteo',
tagline: 'T',
supabase: mocks.fake ? { url: 'http://x', anonKey: 'k' } : null,
authProviders: mocks.providers,
}),
}
})
import LoginRoute from '@/app/routes/auth/LoginRoute'
import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
let fake: FakeSupabase
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
useAuthStore.setState({ status: 'signed_out', recovery: false })
fake = createFakeSupabase()
mocks.fake = fake
mocks.providers = []
})
afterEach(cleanup)
function mount(path = '/login') {
return render(
<MemoryRouter initialEntries={[path]}>
<Routes>
<Route path="/login" element={<LoginRoute />} />
<Route path="/app" element={<p>dashboard</p>} />
<Route path="/app/account" element={<p>account</p>} />
</Routes>
</MemoryRouter>,
)
}
describe('LoginRoute', () => {
it('explains when no backend is configured', () => {
mocks.fake = null
mount()
expect(screen.getByText(fr['auth.unavailable_title'])).toBeInTheDocument()
expect(screen.queryByLabelText(fr['auth.email'])).not.toBeInTheDocument()
})
it('validates before calling the server', async () => {
const user = userEvent.setup()
mount()
await user.click(screen.getByRole('button', { name: fr['auth.login.submit'] }))
expect(screen.getByText(fr['auth.error.email_required'])).toBeInTheDocument()
expect(screen.getByText(fr['auth.error.password_required'])).toBeInTheDocument()
expect(fake.auth.signInWithPassword).not.toHaveBeenCalled()
})
it('signs in and follows the next path', async () => {
fake.auth.signInWithPassword.mockResolvedValue({
data: { session: fakeSession(), user: fakeSession().user },
error: null,
})
const user = userEvent.setup()
mount('/login?next=%2Fapp%2Faccount')
await user.type(screen.getByLabelText(fr['auth.email']), 'alice@example.test')
await user.type(screen.getByLabelText(fr['auth.password']), 'CorrectHorse12')
await user.click(screen.getByRole('button', { name: fr['auth.login.submit'] }))
await waitFor(() => expect(screen.getByText('account')).toBeInTheDocument())
expect(fake.auth.signInWithPassword).toHaveBeenCalledWith({
email: 'alice@example.test',
password: 'CorrectHorse12',
})
})
it('phrases a wrong password without leaking the server message', async () => {
fake.auth.signInWithPassword.mockResolvedValue({
data: { session: null, user: null },
error: { code: 'invalid_credentials', message: 'Invalid login credentials', status: 400 },
})
const user = userEvent.setup()
mount()
await user.type(screen.getByLabelText(fr['auth.email']), 'alice@example.test')
await user.type(screen.getByLabelText(fr['auth.password']), 'wrong')
await user.click(screen.getByRole('button', { name: fr['auth.login.submit'] }))
expect(
await screen.findByText(fr['auth.failure.invalid_credentials']),
).toBeInTheDocument()
expect(screen.queryByText(/Invalid login/)).not.toBeInTheDocument()
})
it('shows one OAuth button per configured provider', () => {
mocks.providers = ['google']
mount()
expect(
screen.getByRole('button', { name: fr['auth.continue_with_google'] }),
).toBeInTheDocument()
expect(
screen.queryByRole('button', { name: fr['auth.continue_with_apple'] }),
).not.toBeInTheDocument()
})
it('toggles password visibility', async () => {
const user = userEvent.setup()
mount()
const input = screen.getByLabelText(fr['auth.password'])
expect(input).toHaveAttribute('type', 'password')
await user.click(screen.getByRole('button', { name: fr['auth.show_password'] }))
expect(input).toHaveAttribute('type', 'text')
})
})