Files
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

58 lines
1.5 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import ErrorBoundary from '../common/ErrorBoundary';
// Component that throws an error
const ThrowError = () => {
throw new Error('Test error');
};
// Component that works fine
const WorkingComponent = () => <div>Working Component</div>;
describe('ErrorBoundary', () => {
// Suppress console.error for these tests
const originalError = console.error;
beforeAll(() => {
console.error = vi.fn();
});
afterAll(() => {
console.error = originalError;
});
it('should render children when there is no error', () => {
render(
<ErrorBoundary>
<WorkingComponent />
</ErrorBoundary>
);
expect(screen.getByText('Working Component')).toBeInTheDocument();
});
it('should render error fallback when child component throws', () => {
render(
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>
);
expect(screen.getByText('Something went wrong')).toBeInTheDocument();
expect(screen.getByRole('button', { name: /refresh page/i })).toBeInTheDocument();
});
it('should render custom fallback when provided', () => {
const customFallback = <div>Custom Error Message</div>;
render(
<ErrorBoundary fallback={customFallback}>
<ThrowError />
</ErrorBoundary>
);
expect(screen.getByText('Custom Error Message')).toBeInTheDocument();
expect(screen.queryByText('Something went wrong')).not.toBeInTheDocument();
});
});