import { describe, it, expect, vi, beforeEach } from 'vitest' import { render, screen, fireEvent, waitFor } from '@testing-library/react' import LoginCard from '../../../src/renderer/src/components/LoginCard' const login = vi.fn() const onSuccess = vi.fn() const onError = vi.fn() beforeEach(() => { vi.clearAllMocks() ;(window as unknown as { api: Record }).api = { login } }) function fillAndSubmit(email: string, password: string): void { fireEvent.change(screen.getByLabelText('Email'), { target: { value: email } }) fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: password } }) fireEvent.click(screen.getByRole('button', { name: 'Se connecter' })) } describe('', () => { it('should show an inline error and not call the API when the email is invalid', async () => { render() fillAndSubmit('not-an-email', 'password123') expect(await screen.findByText('Adresse email invalide')).toBeInTheDocument() expect(login).not.toHaveBeenCalled() }) it('should show an inline error and not call the API when the password is empty', async () => { render() fillAndSubmit('william@example.com', '') expect(await screen.findByText('Mot de passe requis')).toBeInTheDocument() expect(login).not.toHaveBeenCalled() }) it('should toggle the password field between hidden and visible text', () => { render() const passwordInput = screen.getByLabelText('Mot de passe') expect(passwordInput.type).toBe('password') fireEvent.click(screen.getByRole('button', { name: 'Afficher le mot de passe' })) expect(passwordInput.type).toBe('text') fireEvent.click(screen.getByRole('button', { name: 'Masquer le mot de passe' })) expect(passwordInput.type).toBe('password') }) it('should call the API and show a success message on success', async () => { login.mockResolvedValue({ ok: true }) render() fillAndSubmit('william@example.com', 'password123') expect(login).toHaveBeenCalledWith('william@example.com', 'password123') await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument()) expect(onSuccess).toHaveBeenCalled() expect(onError).not.toHaveBeenCalled() }) it('should show the server error and call onError on failure', async () => { login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' }) render() fillAndSubmit('william@example.com', 'mauvais') await waitFor(() => expect(screen.getByText('Email ou mot de passe incorrect')).toBeInTheDocument() ) expect(onError).toHaveBeenCalledWith('Email ou mot de passe incorrect') }) it('should show a generic error when the IPC call itself rejects', async () => { login.mockRejectedValue(new Error('IPC down')) render() fillAndSubmit('william@example.com', 'password123') await waitFor(() => expect(screen.getByText('Erreur inattendue')).toBeInTheDocument()) expect(onError).toHaveBeenCalledWith('Erreur inattendue') }) it('should disable the submit button and show a loading label while submitting', async () => { let resolveLogin: (value: { ok: true }) => void = () => {} login.mockReturnValue( new Promise((resolve) => { resolveLogin = resolve }) ) render() fillAndSubmit('william@example.com', 'password123') const submit = screen.getByRole('button', { name: 'Se connecter' }) expect(submit).toBeDisabled() expect(screen.getByText('Connexion...')).toBeInTheDocument() resolveLogin({ ok: true }) await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument()) }) })