Files
SRCmail/components/contacts/__tests__/contact-form.test.tsx
T
Matthieu MALVACHEandMatthieu MALVACHE fb414bf2c9 feat: add contacts phase 2, advanced search, vacation responder, Docker & TOTP 2FA
- Contact groups/lists, vCard import/export (RFC 6350), bulk operations
- Advanced search with JMAP filter panel, search chips, cross-mailbox queries
- Vacation responder with JMAP VacationResponse, settings tab, sidebar indicator
- TOTP two-factor authentication support
- Docker multi-stage build with standalone output and docker-compose
- CSP Report-Only headers and security headers via proxy middleware
- Virtual scrolling for large email lists
- Structured server-side logger (text/JSON, configurable level)
- 450+ tests (contacts, vCard, threads, headers, identity, components)
- Playwright E2E framework setup
- Updated README and ROADMAP with all new features
2026-02-16 18:51:30 +01:00

84 lines
3.4 KiB
TypeScript

import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { ContactForm } from '../contact-form';
import type { ContactCard } from '@/lib/jmap/types';
const existingContact: ContactCard = {
id: '1',
addressBookIds: {},
name: { components: [{ kind: 'given', value: 'Alice' }, { kind: 'surname', value: 'Smith' }], isOrdered: true },
emails: { e0: { address: 'alice@example.com' } },
phones: { p0: { number: '+33612345678' } },
organizations: { o0: { name: 'Acme Corp' } },
notes: { n0: { note: 'VIP' } },
};
describe('ContactForm', () => {
it('renders create form with empty fields', () => {
render(<ContactForm onSave={vi.fn()} onCancel={vi.fn()} />);
expect(screen.getByText('create_title')).toBeInTheDocument();
const inputs = screen.getAllByRole('textbox');
const emptyInputs = inputs.filter(i => (i as HTMLInputElement).value === '');
expect(emptyInputs.length).toBeGreaterThan(0);
});
it('renders edit form with pre-populated data', () => {
render(<ContactForm contact={existingContact} onSave={vi.fn()} onCancel={vi.fn()} />);
expect(screen.getByText('edit_title')).toBeInTheDocument();
expect(screen.getByDisplayValue('Alice')).toBeInTheDocument();
expect(screen.getByDisplayValue('Smith')).toBeInTheDocument();
});
it('calls onCancel when cancel button is clicked', () => {
const onCancel = vi.fn();
render(<ContactForm onSave={vi.fn()} onCancel={onCancel} />);
fireEvent.click(screen.getByText('cancel'));
expect(onCancel).toHaveBeenCalledOnce();
});
it('shows error on submit with empty name', async () => {
const onSave = vi.fn();
render(<ContactForm onSave={onSave} onCancel={vi.fn()} />);
fireEvent.submit(screen.getByText('save').closest('form')!);
await waitFor(() => {
expect(screen.getByText('name_required')).toBeInTheDocument();
});
expect(onSave).not.toHaveBeenCalled();
});
it('adds email entry when add button is clicked', () => {
render(<ContactForm onSave={vi.fn()} onCancel={vi.fn()} />);
const emailInputsBefore = screen.getAllByPlaceholderText('email_placeholder');
fireEvent.click(screen.getByText('add_email'));
const emailInputsAfter = screen.getAllByPlaceholderText('email_placeholder');
expect(emailInputsAfter.length).toBe(emailInputsBefore.length + 1);
});
it('adds phone entry when add button is clicked', () => {
render(<ContactForm onSave={vi.fn()} onCancel={vi.fn()} />);
const phoneBefore = screen.queryAllByPlaceholderText('phone_placeholder');
fireEvent.click(screen.getByText('add_phone'));
const phoneAfter = screen.getAllByPlaceholderText('phone_placeholder');
expect(phoneAfter.length).toBe(phoneBefore.length + 1);
});
it('submits form data correctly', async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
render(<ContactForm onSave={onSave} onCancel={vi.fn()} />);
const inputs = screen.getAllByRole('textbox');
fireEvent.change(inputs[0], { target: { value: 'Jane' } });
fireEvent.submit(screen.getByText('save').closest('form')!);
await waitFor(() => {
expect(onSave).toHaveBeenCalledOnce();
});
const savedData = onSave.mock.calls[0][0];
expect(savedData.name.components).toEqual(
expect.arrayContaining([expect.objectContaining({ kind: 'given', value: 'Jane' })])
);
});
});