|
| 1 | +import { describe, expect, it, vi, afterEach } from 'vitest'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import SectionErrorBoundary from '@/components/common/SectionErrorBoundary'; |
| 4 | + |
| 5 | +// Mock component that throws an error |
| 6 | +const BuggyComponent = ({ shouldThrow = false }: { shouldThrow?: boolean }) => { |
| 7 | + if (shouldThrow) { |
| 8 | + throw new Error('Test error'); |
| 9 | + } |
| 10 | + return <div>Normal Content</div>; |
| 11 | +}; |
| 12 | + |
| 13 | +describe('SectionErrorBoundary', () => { |
| 14 | + afterEach(() => { |
| 15 | + vi.restoreAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('renders children when no error occurs', () => { |
| 19 | + render( |
| 20 | + <SectionErrorBoundary> |
| 21 | + <BuggyComponent /> |
| 22 | + </SectionErrorBoundary> |
| 23 | + ); |
| 24 | + expect(screen.getByText('Normal Content')).toBeInTheDocument(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('renders fallback UI when an error occurs', () => { |
| 28 | + // Suppress console.error for the expected error |
| 29 | + const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 30 | + |
| 31 | + render( |
| 32 | + <SectionErrorBoundary sectionName="Test Section"> |
| 33 | + <BuggyComponent shouldThrow={true} /> |
| 34 | + </SectionErrorBoundary> |
| 35 | + ); |
| 36 | + |
| 37 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 38 | + expect(screen.getByText(/something went wrong/i)).toBeInTheDocument(); |
| 39 | + expect(screen.getByText(/Test Section/i)).toBeInTheDocument(); |
| 40 | + expect(consoleSpy).toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('resets error state when Retry button is clicked', () => { |
| 44 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 45 | + |
| 46 | + const { rerender } = render( |
| 47 | + <SectionErrorBoundary> |
| 48 | + <BuggyComponent shouldThrow={true} /> |
| 49 | + </SectionErrorBoundary> |
| 50 | + ); |
| 51 | + |
| 52 | + expect(screen.getByText(/something went wrong/i)).toBeInTheDocument(); |
| 53 | + |
| 54 | + // Update children to not throw anymore |
| 55 | + rerender( |
| 56 | + <SectionErrorBoundary> |
| 57 | + <BuggyComponent shouldThrow={false} /> |
| 58 | + </SectionErrorBoundary> |
| 59 | + ); |
| 60 | + |
| 61 | + // Click retry |
| 62 | + fireEvent.click(screen.getByText(/retry/i)); |
| 63 | + |
| 64 | + expect(screen.queryByText(/something went wrong/i)).not.toBeInTheDocument(); |
| 65 | + expect(screen.getByText('Normal Content')).toBeInTheDocument(); |
| 66 | + }); |
| 67 | + |
| 68 | + it('applies custom minHeight and className', () => { |
| 69 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 70 | + |
| 71 | + render( |
| 72 | + <SectionErrorBoundary minHeight={500} className="custom-class"> |
| 73 | + <BuggyComponent shouldThrow={true} /> |
| 74 | + </SectionErrorBoundary> |
| 75 | + ); |
| 76 | + |
| 77 | + const alert = screen.getByRole('alert'); |
| 78 | + expect(alert).toHaveStyle('min-height: 500px'); |
| 79 | + expect(alert).toHaveClass('custom-class'); |
| 80 | + }); |
| 81 | +}); |
0 commit comments