import { describe, it, expect, vi, beforeEach } from 'vitest' import { screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import SaisirCodeUnique from '../../pages/SaisirCodeUnique' import Perdu from '../../pages/Perdu' import { renderWithProviders } from '../renderWithProviders' import { participateApi } from '../../services/game' vi.mock('../../services/game', () => ({ participateApi: vi.fn(), })) const client = { id: 1, email: 'client@example.com', role: 'CLIENT' } async function saisirEtValider(code) { await userEvent.type(screen.getByPlaceholderText(/votre code/i), code) await userEvent.click(screen.getByRole('button', { name: /valider/i })) } describe('SaisirCodeUnique', () => { beforeEach(() => { vi.clearAllMocks() }) it('désactive le bouton tant que le code ne fait pas 10 caractères', async () => { renderWithProviders(, { route: '/saisir-code-unique', user: client }) const bouton = screen.getByRole('button', { name: /valider/i }) expect(bouton).toBeDisabled() await userEvent.type(screen.getByPlaceholderText(/votre code/i), 'ABCDE12345') expect(bouton).toBeEnabled() }) it('met le code en majuscules et l\'envoie à l\'API', async () => { participateApi.mockResolvedValue({ gain: { name: 'Thé détox' }, claim: { id: 1 } }) renderWithProviders(, { route: '/saisir-code-unique', user: client }) await saisirEtValider('abcde12345') expect(participateApi).toHaveBeenCalledWith('ABCDE12345') }) it("affiche l'écran Félicitation puis le détail du lot quand le code est gagnant", async () => { participateApi.mockResolvedValue({ gain: { name: 'Thé signature', description: 'Un thé d\'exception', value: 39 }, claim: { id: 1 }, }) renderWithProviders(, { route: '/saisir-code-unique', user: client }) await saisirEtValider('ABCDE12345') // Overlay de félicitation expect(await screen.findByText(/félicitation/i)).toBeInTheDocument() expect(screen.getByText('Thé signature')).toBeInTheDocument() // Détail du lot await userEvent.click(screen.getByRole('button', { name: /découvrir mon lot/i })) expect(screen.getByText(/valeur estimée/i)).toBeInTheDocument() expect(screen.getByText('39 €')).toBeInTheDocument() expect(screen.getByText(/en attente de remise/i)).toBeInTheDocument() }) it("redirige vers /perdu avec le message de l'API quand le code est invalide", async () => { participateApi.mockRejectedValue(new Error('Ce code a déjà été utilisé.')) renderWithProviders(, { route: '/saisir-code-unique', user: client, routes: { '/perdu': }, }) await saisirEtValider('ABCDE12345') expect(await screen.findByText(/réessayer la prochaine fois/i)).toBeInTheDocument() expect(screen.getByText('Ce code a déjà été utilisé.')).toBeInTheDocument() }) it('déconnecte et redirige vers /connexion si la session a expiré (401)', async () => { participateApi.mockRejectedValue( Object.assign(new Error('Session expirée'), { status: 401 }) ) renderWithProviders(, { route: '/saisir-code-unique', user: client, routes: { '/connexion':

Page Connexion

}, }) await saisirEtValider('ABCDE12345') expect(await screen.findByText('Page Connexion')).toBeInTheDocument() expect(localStorage.getItem('token')).toBeNull() }) })