85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { screen } from '@testing-library/react'
|
|
import userEvent from '@testing-library/user-event'
|
|
import Connexion from '../../pages/Connexion'
|
|
import { renderWithProviders } from '../renderWithProviders'
|
|
import { loginApi } from '../../services/auth'
|
|
|
|
// On mocke le service d'API : les tests de composant ne font pas de vraies requêtes HTTP.
|
|
vi.mock('../../services/auth', () => ({
|
|
loginApi: vi.fn(),
|
|
}))
|
|
|
|
describe('Connexion', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
it('affiche le formulaire de connexion', () => {
|
|
renderWithProviders(<Connexion />, { route: '/connexion' })
|
|
|
|
expect(screen.getByRole('heading', { name: /se connecter/i })).toBeInTheDocument()
|
|
expect(screen.getByLabelText(/adresse email/i)).toBeInTheDocument()
|
|
expect(screen.getByLabelText(/^mot de passe/i)).toBeInTheDocument()
|
|
expect(screen.getByRole('button', { name: /se connecter/i })).toBeInTheDocument()
|
|
})
|
|
|
|
it('affiche le message de succès après une inscription', () => {
|
|
renderWithProviders(<Connexion />, {
|
|
route: { pathname: '/connexion', state: { registered: true } },
|
|
})
|
|
|
|
expect(screen.getByText(/compte créé avec succès/i)).toBeInTheDocument()
|
|
})
|
|
|
|
it("affiche l'erreur renvoyée par l'API en cas d'échec", async () => {
|
|
loginApi.mockRejectedValue(new Error('Email ou mot de passe incorrect.'))
|
|
|
|
renderWithProviders(<Connexion />, { route: '/connexion' })
|
|
|
|
await userEvent.type(screen.getByLabelText(/adresse email/i), 'test@example.com')
|
|
await userEvent.type(screen.getByLabelText(/^mot de passe/i), 'mauvais-mdp')
|
|
await userEvent.click(screen.getByRole('button', { name: /se connecter/i }))
|
|
|
|
expect(await screen.findByText('Email ou mot de passe incorrect.')).toBeInTheDocument()
|
|
expect(loginApi).toHaveBeenCalledWith({ email: 'test@example.com', password: 'mauvais-mdp' })
|
|
})
|
|
|
|
it('connecte un client et redirige vers /mes-gains', async () => {
|
|
loginApi.mockResolvedValue({
|
|
token: 'jwt-token',
|
|
user: { id: 1, email: 'client@example.com', role: 'CLIENT' },
|
|
})
|
|
|
|
renderWithProviders(<Connexion />, {
|
|
route: '/connexion',
|
|
routes: { '/mes-gains': <p>Page Mes Gains</p> },
|
|
})
|
|
|
|
await userEvent.type(screen.getByLabelText(/adresse email/i), 'client@example.com')
|
|
await userEvent.type(screen.getByLabelText(/^mot de passe/i), 'MotDePasse123!')
|
|
await userEvent.click(screen.getByRole('button', { name: /se connecter/i }))
|
|
|
|
expect(await screen.findByText('Page Mes Gains')).toBeInTheDocument()
|
|
expect(localStorage.getItem('token')).toBe('jwt-token')
|
|
})
|
|
|
|
it("connecte un admin et redirige vers /admin", async () => {
|
|
loginApi.mockResolvedValue({
|
|
token: 'jwt-token',
|
|
user: { id: 2, email: 'admin@example.com', role: 'ADMIN' },
|
|
})
|
|
|
|
renderWithProviders(<Connexion />, {
|
|
route: '/connexion',
|
|
routes: { '/admin': <p>Page Admin</p> },
|
|
})
|
|
|
|
await userEvent.type(screen.getByLabelText(/adresse email/i), 'admin@example.com')
|
|
await userEvent.type(screen.getByLabelText(/^mot de passe/i), 'MotDePasse123!')
|
|
await userEvent.click(screen.getByRole('button', { name: /se connecter/i }))
|
|
|
|
expect(await screen.findByText('Page Admin')).toBeInTheDocument()
|
|
})
|
|
})
|