- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
94 lines
2.8 KiB
TypeScript
94 lines
2.8 KiB
TypeScript
import React from 'react';
|
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
import { render, screen, fireEvent } from '@testing-library/react';
|
|
import { ErrorBoundary } from '../ErrorBoundary';
|
|
|
|
// Helper component that throws an error during rendering
|
|
const ProblemChild = ({ shouldThrow = true }: { shouldThrow?: boolean }) => {
|
|
if (shouldThrow) {
|
|
throw new Error('Test render crash');
|
|
}
|
|
return <div>محتوای سالم کامپوننت</div>;
|
|
};
|
|
|
|
describe('ErrorBoundary Component', () => {
|
|
let consoleErrorSpy: any;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
});
|
|
|
|
afterEach(() => {
|
|
consoleErrorSpy.mockRestore();
|
|
});
|
|
|
|
it('renders children normally when there is no error', () => {
|
|
render(
|
|
<ErrorBoundary>
|
|
<ProblemChild shouldThrow={false} />
|
|
</ErrorBoundary>
|
|
);
|
|
|
|
expect(screen.getByText('محتوای سالم کامپوننت')).toBeInTheDocument();
|
|
expect(screen.queryByText('متأسفانه خطایی رخ داده است')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('catches render errors and displays fallback UI', () => {
|
|
render(
|
|
<ErrorBoundary>
|
|
<ProblemChild shouldThrow={true} />
|
|
</ErrorBoundary>
|
|
);
|
|
|
|
expect(screen.getByText('متأسفانه خطایی رخ داده است')).toBeInTheDocument();
|
|
expect(
|
|
screen.getByText('مشکلی در بارگذاری این بخش پیش آمده است. لطفاً برای ادامه دوباره تلاش کنید.')
|
|
).toBeInTheDocument();
|
|
expect(screen.getByRole('button', { name: /تلاش مجدد/ })).toBeInTheDocument();
|
|
expect(screen.getByText('Canina Pharma Support')).toBeInTheDocument();
|
|
expect(screen.queryByText('محتوای سالم کامپوننت')).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('logs the uncaught error to console.error via componentDidCatch', () => {
|
|
render(
|
|
<ErrorBoundary>
|
|
<ProblemChild shouldThrow={true} />
|
|
</ErrorBoundary>
|
|
);
|
|
|
|
expect(consoleErrorSpy).toHaveBeenCalled();
|
|
});
|
|
|
|
it('triggers window.location.reload when reload/retry button is clicked', () => {
|
|
const originalLocation = window.location;
|
|
const reloadMock = vi.fn();
|
|
|
|
// Mock window.location.reload
|
|
Object.defineProperty(window, 'location', {
|
|
configurable: true,
|
|
value: {
|
|
...originalLocation,
|
|
reload: reloadMock,
|
|
},
|
|
});
|
|
|
|
render(
|
|
<ErrorBoundary>
|
|
<ProblemChild shouldThrow={true} />
|
|
</ErrorBoundary>
|
|
);
|
|
|
|
const retryButton = screen.getByRole('button', { name: /تلاش مجدد/ });
|
|
fireEvent.click(retryButton);
|
|
|
|
expect(reloadMock).toHaveBeenCalledTimes(1);
|
|
|
|
// Restore original location
|
|
Object.defineProperty(window, 'location', {
|
|
configurable: true,
|
|
value: originalLocation,
|
|
});
|
|
});
|
|
});
|