Files
canina/frontend/application/components/__tests__/ErrorBoundary.test.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

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,
});
});
});