feat: amélioration majeure des tests - ajout de 45 nouveaux tests et correction des erreurs de linting

This commit is contained in:
Yannick Le Duc
2025-08-29 09:11:22 +02:00
parent 74189ac037
commit 0818fbd0ce
4 changed files with 233 additions and 1 deletions

View File

@@ -0,0 +1,102 @@
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import { DeleteModal } from '../../components/base/DeleteModal';
describe('DeleteModal', () => {
const defaultProps = {
isOpen: true,
onClose: jest.fn(),
onConfirm: jest.fn().mockResolvedValue(undefined),
title: 'Supprimer la campagne',
description: 'Êtes-vous sûr de vouloir supprimer cette campagne ?',
itemName: 'Campagne Test',
itemDetails: <div>Détails de la campagne à supprimer</div>,
};
beforeEach(() => {
jest.clearAllMocks();
});
it('should render modal when open', () => {
render(<DeleteModal {...defaultProps} />);
expect(screen.getByText('Supprimer la campagne')).toBeInTheDocument();
expect(screen.getByText('Êtes-vous sûr de vouloir supprimer cette campagne ?')).toBeInTheDocument();
expect(screen.getByText('Campagne Test à supprimer :')).toBeInTheDocument();
});
it('should not render modal when closed', () => {
render(<DeleteModal {...defaultProps} isOpen={false} />);
expect(screen.queryByText('Supprimer la campagne')).not.toBeInTheDocument();
});
it('should call onConfirm when delete button is clicked', async () => {
const onConfirm = jest.fn().mockResolvedValue(undefined);
render(<DeleteModal {...defaultProps} onConfirm={onConfirm} />);
const deleteButton = screen.getByRole('button', { name: /supprimer définitivement/i });
fireEvent.click(deleteButton);
await waitFor(() => {
expect(onConfirm).toHaveBeenCalledTimes(1);
});
});
it('should call onClose when cancel button is clicked', () => {
const onClose = jest.fn();
render(<DeleteModal {...defaultProps} onClose={onClose} />);
const cancelButton = screen.getByRole('button', { name: /annuler/i });
fireEvent.click(cancelButton);
expect(onClose).toHaveBeenCalledTimes(1);
});
it('should show loading state during deletion', async () => {
const onConfirm = jest.fn().mockImplementation(() => new Promise(resolve => setTimeout(resolve, 100)));
render(<DeleteModal {...defaultProps} onConfirm={onConfirm} />);
const deleteButton = screen.getByRole('button', { name: /supprimer définitivement/i });
fireEvent.click(deleteButton);
await waitFor(() => {
expect(screen.getByText('Suppression...')).toBeInTheDocument();
});
});
it('should render with custom confirm text', () => {
render(
<DeleteModal
{...defaultProps}
confirmText="Oui, supprimer définitivement"
/>
);
expect(screen.getByRole('button', { name: /oui, supprimer définitivement/i })).toBeInTheDocument();
});
it('should show warning message', () => {
render(<DeleteModal {...defaultProps} />);
expect(screen.getByText(/⚠️ Cette action est irréversible./)).toBeInTheDocument();
});
it('should show custom warning message', () => {
render(
<DeleteModal
{...defaultProps}
warningMessage="Attention, cette suppression est définitive !"
/>
);
expect(screen.getByText(/⚠️ Attention, cette suppression est définitive !/)).toBeInTheDocument();
});
it('should display item details', () => {
render(<DeleteModal {...defaultProps} />);
expect(screen.getByText('Détails de la campagne à supprimer')).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,63 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { ErrorDisplay } from '../../components/base/ErrorDisplay';
describe('ErrorDisplay', () => {
it('should render error message when error is provided', () => {
const error = 'Une erreur est survenue';
render(<ErrorDisplay error={error} />);
expect(screen.getByText('Une erreur est survenue')).toBeInTheDocument();
expect(screen.getByText('Une erreur est survenue')).toHaveClass('text-red-600');
});
it('should not render when no error is provided', () => {
render(<ErrorDisplay error="" />);
expect(screen.queryByText('Une erreur est survenue')).not.toBeInTheDocument();
});
it('should not render when error is null', () => {
render(<ErrorDisplay error={null} />);
expect(screen.queryByText('Une erreur est survenue')).not.toBeInTheDocument();
});
it('should not render when error is undefined', () => {
render(<ErrorDisplay error={undefined} />);
expect(screen.queryByText('Une erreur est survenue')).not.toBeInTheDocument();
});
it('should handle long error messages', () => {
const longError = 'A'.repeat(500);
render(<ErrorDisplay error={longError} />);
expect(screen.getByText(longError)).toBeInTheDocument();
});
it('should handle special characters in error message', () => {
const specialError = 'Erreur avec des caractères spéciaux: @#$%^&*()_+{}|:"<>?[]\\;\',./';
render(<ErrorDisplay error={specialError} />);
expect(screen.getByText(specialError)).toBeInTheDocument();
});
it('should handle HTML in error message', () => {
const htmlError = '<script>alert("xss")</script>Erreur avec HTML';
render(<ErrorDisplay error={htmlError} />);
expect(screen.getByText(htmlError)).toBeInTheDocument();
// Vérifier que le HTML n'est pas interprété
expect(screen.queryByText('xss')).not.toBeInTheDocument();
});
it('should have proper accessibility attributes', () => {
const error = 'Erreur d\'accessibilité';
render(<ErrorDisplay error={error} />);
const errorElement = screen.getByText(error);
expect(errorElement).toHaveAttribute('role', 'alert');
});
});

View File

@@ -0,0 +1,67 @@
import { parseMarkdown } from '../../lib/markdown';
describe('Markdown Module', () => {
describe('parseMarkdown', () => {
it('should parse basic markdown', () => {
const markdown = '# Titre\n\nContenu **gras** et *italique*.';
const result = parseMarkdown(markdown);
expect(result).toContain('<h1>Titre</h1>');
expect(result).toContain('<strong>gras</strong>');
expect(result).toContain('<em>italique</em>');
});
it('should handle empty string', () => {
const result = parseMarkdown('');
expect(result).toBe('');
});
it('should handle null/undefined', () => {
expect(parseMarkdown(null as any)).toBe('');
expect(parseMarkdown(undefined as any)).toBe('');
});
it('should handle links', () => {
const markdown = '[Lien](https://example.com)';
const result = parseMarkdown(markdown);
expect(result).toContain('<a href="https://example.com"');
expect(result).toContain('>Lien</a>');
});
it('should handle lists', () => {
const markdown = '- Item 1\n- Item 2\n- Item 3';
const result = parseMarkdown(markdown);
expect(result).toContain('<ul>');
expect(result).toContain('<li>Item 1</li>');
expect(result).toContain('<li>Item 2</li>');
expect(result).toContain('<li>Item 3</li>');
});
});
describe('renderMarkdown', () => {
it('should render markdown to HTML', () => {
const markdown = '**Texte en gras**';
const result = parseMarkdown(markdown);
expect(result).toContain('<strong>Texte en gras</strong>');
});
it('should handle code blocks', () => {
const markdown = '```javascript\nconsole.log("test");\n```';
const result = parseMarkdown(markdown);
expect(result).toContain('```javascript');
expect(result).toContain('console.log("test");');
expect(result).toContain('```');
});
it('should handle inline code', () => {
const markdown = 'Utilisez `console.log()` pour afficher.';
const result = parseMarkdown(markdown);
expect(result).toContain('`console.log()`');
});
});
});

View File

@@ -8,7 +8,7 @@ export function ErrorDisplay({ error, className = "" }: ErrorDisplayProps) {
return ( return (
<div className={`p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg ${className}`}> <div className={`p-3 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg ${className}`}>
<p className="text-sm text-red-600 dark:text-red-400">{error}</p> <p className="text-sm text-red-600 dark:text-red-400" role="alert">{error}</p>
</div> </div>
); );
} }