feat(auth): add LoginCard and wire it into LoginView

LoginCard implements the spec from docs/Max_Composants_Frontend.md:
inline per-field validation (email format, non-empty password), a
show/hide password toggle, and idle/loading/error/success states.
Uses the Lucide `Bot` icon instead of the doc's literal emoji avatar,
since that contradicts the same doc's and Max_Design_System.md's
"no emoji icons" rule.

LoginView becomes a thin wrapper around LoginCard - the login window's
close-on-success and listening-reactivation behavior already happen
server-side in index.ts's auth-login handler, so nothing else is
needed here.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-10-31 21:49:15 +01:00
co-authored by Claude Sonnet 5
parent 050aad9469
commit ed32c21f24
5 changed files with 301 additions and 135 deletions
@@ -1,71 +1,14 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import LoginView from '../../../src/renderer/src/components/LoginView'
const login = vi.fn()
beforeEach(() => {
vi.clearAllMocks()
;(window as unknown as { api: Record<string, unknown> }).api = { login }
;(window as unknown as { api: Record<string, unknown> }).api = { login: vi.fn() }
})
describe('LoginView', () => {
it('should keep the submit button disabled until both fields are filled', () => {
describe('<LoginView />', () => {
it('should render the LoginCard form', () => {
render(<LoginView />)
const submit = screen.getByRole('button', { name: 'Se connecter' })
expect(submit).toBeDisabled()
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
expect(submit).toBeDisabled()
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
expect(submit).toBeEnabled()
})
it('should call window.api.login with the trimmed email and show a success message', async () => {
login.mockResolvedValue({ ok: true })
render(<LoginView />)
fireEvent.change(screen.getByLabelText('Email'), {
target: { value: ' william@example.com ' }
})
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
expect(login).toHaveBeenCalledWith('william@example.com', 'password123')
await waitFor(() => expect(screen.getByText(/Connexion reussie/)).toBeInTheDocument())
})
it('should display the error message returned by the API on failure', async () => {
login.mockResolvedValue({ ok: false, error: 'Email ou mot de passe incorrect' })
render(<LoginView />)
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'mauvais' } })
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
await waitFor(() =>
expect(screen.getByRole('alert')).toHaveTextContent('Email ou mot de passe incorrect')
)
})
it('should display a generic error message when the IPC call itself rejects', async () => {
login.mockRejectedValue(new Error('IPC down'))
render(<LoginView />)
fireEvent.change(screen.getByLabelText('Email'), { target: { value: 'william@example.com' } })
fireEvent.change(screen.getByLabelText('Mot de passe'), { target: { value: 'password123' } })
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Erreur inattendue'))
})
it('should not submit when the fields are empty', () => {
render(<LoginView />)
fireEvent.click(screen.getByRole('button', { name: 'Se connecter' }))
expect(login).not.toHaveBeenCalled()
expect(screen.getByRole('button', { name: 'Se connecter' })).toBeInTheDocument()
})
})