import { render, screen } from '@testing-library/react';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as Sentry from '@sentry/react';
import { ErrorBoundary } from '../ErrorBoundary';
vi.mock('@sentry/react', () => ({
captureException: vi.fn(),
}));
function ThrowingChild(): never {
throw new Error('boom');
}
describe('ErrorBoundary', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('renders children when there is no error', () => {
render(
Alles werkt
,
);
expect(screen.getByText('Alles werkt')).toBeInTheDocument();
});
it('renders the generic fallback message when a child throws', () => {
// Suppress the expected React error boundary console.error noise for this test.
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
render(
,
);
expect(screen.getByTestId('error-boundary-fallback')).toBeInTheDocument();
expect(screen.getByText('Er ging iets mis. Probeer de pagina te vernieuwen.')).toBeInTheDocument();
consoleErrorSpy.mockRestore();
});
it('reports the caught error to Sentry', () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
render(
,
);
expect(Sentry.captureException).toHaveBeenCalledTimes(1);
expect(Sentry.captureException).toHaveBeenCalledWith(
expect.any(Error),
expect.objectContaining({ extra: expect.objectContaining({ componentStack: expect.any(String) }) }),
);
consoleErrorSpy.mockRestore();
});
});