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:
co-authored by
Claude Sonnet 5
parent
050aad9469
commit
ed32c21f24
@@ -0,0 +1,104 @@
|
||||
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<string, unknown> }).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('<LoginCard />', () => {
|
||||
it('should show an inline error and not call the API when the email is invalid', async () => {
|
||||
render(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||
|
||||
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(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||
|
||||
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(<LoginCard onSuccess={onSuccess} />)
|
||||
|
||||
const passwordInput = screen.getByLabelText<HTMLInputElement>('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(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||
|
||||
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(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||
|
||||
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(<LoginCard onSuccess={onSuccess} onError={onError} />)
|
||||
|
||||
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(<LoginCard onSuccess={onSuccess} />)
|
||||
|
||||
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())
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user