Files
faunosteo/apps/app/tests/unit/routes.AccountRoute.test.tsx
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

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', () => {
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(screen.getByText(fr['account.security.mfa_note'])).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')
})
})