import { render, screen } from '@testing-library/react'; import { describe, it, expect, vi } from 'vitest'; import { ErrorBoundary } from '@/components/ErrorBoundary'; // Test component that throws an error const ThrowError = ({ shouldThrow }: { shouldThrow: boolean }) => { if (shouldThrow) { throw new Error('Test error'); } return
No error
; }; describe('ErrorBoundary', () => { it('renders children when no error occurs', () => { render(
Test content
); expect(screen.getByText('Test content')).toBeInTheDocument(); }); it('catches and displays error when child throws', () => { // Suppress console.error for this test const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); render( ); expect(screen.getByText('Something went wrong')).toBeInTheDocument(); expect(screen.getByText('An unexpected error occurred. The error has been reported.')).toBeInTheDocument(); consoleSpy.mockRestore(); }); it('uses custom fallback when provided', () => { const customFallback =
Custom error message
; // Suppress console.error for this test const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); render( ); expect(screen.getByText('Custom error message')).toBeInTheDocument(); consoleSpy.mockRestore(); }); });