Files
faunosteo/apps/app/tests/unit/routes.AccountRoute.test.tsx
T
bchanot 4744e28986 feat(app): admin and catalogue entries, step-up guard, MFA in Mon compte (V1-2.5 E, F)
Nav hint from own grants and roles (never a control); RequireCatalog, RequireAdmin (no admin read before aal2), Accès non autorisé page, placeholder routes; TOTP factor list, enrolment with QR and one-time secret, removal, inline step-up for aal2 actions, step-up in password reset; auth kind aal2_required. App 601, e2e:ui 227.
2026-11-02 16:49:37 +01:00

102 lines
3.7 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 } from 'react-router-dom'
import { createFakeSupabase, fakeProfile, fakeUser, 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 AccountRoute from '@/app/routes/app/AccountRoute'
import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
let fake: FakeSupabase
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
fake = createFakeSupabase()
mocks.fake = fake
useAuthStore.setState({
status: 'signed_in',
user: fakeUser(),
profile: fakeProfile(),
roles: ['owner'],
})
})
afterEach(cleanup)
function mount() {
return render(
<MemoryRouter>
<AccountRoute />
</MemoryRouter>,
)
}
describe('AccountRoute', () => {
it('shows the four sections, the roles and the sign-in address', async () => {
mount()
for (const key of [
'account.personal.title',
'account.professional.title',
'account.preferences.title',
'account.security.title',
]) {
expect(screen.getByRole('heading', { name: fr[key] })).toBeInTheDocument()
}
expect(screen.getByText(fr['shell.role.owner'])).toBeInTheDocument()
expect(screen.getByText('alice@example.test')).toBeInTheDocument()
expect(await screen.findByText(fr['mfa.factor.none'])).toBeInTheDocument()
})
it('saves personal information, empty optionals as null', async () => {
const user = userEvent.setup()
mount()
await user.type(screen.getByLabelText(new RegExp(`^${fr['account.personal.first_name']}`)), 'Alice')
const [save] = screen.getAllByRole('button', { name: fr['account.save'] })
await user.click(save)
expect(await screen.findByText(fr['account.saved'])).toBeInTheDocument()
expect(fake.tables.profiles[0]).toMatchObject({
display_name: 'Alice Local',
first_name: 'Alice',
last_name: null,
phone: null,
})
})
it('refuses a malformed SIRET next to the field, without calling the server', async () => {
const user = userEvent.setup()
mount()
await user.type(screen.getByLabelText(new RegExp(`^${fr['account.professional.siret']}`)), '1234')
const saves = screen.getAllByRole('button', { name: fr['account.save'] })
await user.click(saves[1])
expect(await screen.findByText(fr['account.error.siret_format'])).toBeInTheDocument()
expect(fake.tables.profiles[0].siret).toBeNull()
})
it('changes the password through the client', async () => {
fake.auth.updateUser.mockResolvedValue({ data: {}, error: null })
const user = userEvent.setup()
mount()
await user.type(screen.getByLabelText(fr['account.security.new_password']), 'CorrectHorse12')
await user.type(screen.getByLabelText(fr['auth.password_confirm']), 'CorrectHorse12')
await user.click(screen.getByRole('button', { name: fr['account.security.change_password'] }))
await waitFor(() =>
expect(fake.auth.updateUser).toHaveBeenCalledWith({ password: 'CorrectHorse12' }),
)
expect(await screen.findByText(fr['account.security.password_changed'])).toBeInTheDocument()
})
it('switching the language select updates the locale store', async () => {
const user = userEvent.setup()
mount()
await user.selectOptions(screen.getByLabelText(fr['account.preferences.language']), 'en')
expect(useLocaleStore.getState().locale).toBe('en')
})
})