62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
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(
|
|
<ErrorBoundary>
|
|
<p>Alles werkt</p>
|
|
</ErrorBoundary>,
|
|
);
|
|
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(
|
|
<ErrorBoundary>
|
|
<ThrowingChild />
|
|
</ErrorBoundary>,
|
|
);
|
|
|
|
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(
|
|
<ErrorBoundary>
|
|
<ThrowingChild />
|
|
</ErrorBoundary>,
|
|
);
|
|
|
|
expect(Sentry.captureException).toHaveBeenCalledTimes(1);
|
|
expect(Sentry.captureException).toHaveBeenCalledWith(
|
|
expect.any(Error),
|
|
expect.objectContaining({ extra: expect.objectContaining({ componentStack: expect.any(String) }) }),
|
|
);
|
|
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
});
|