|
| 1 | +import { describe, it, expect, vi, afterEach } from 'vitest' |
| 2 | +import { render, screen, cleanup } from '@testing-library/react' |
| 3 | + |
| 4 | +vi.mock('../observability', () => ({ |
| 5 | + Sentry: { |
| 6 | + captureException: vi.fn(), |
| 7 | + }, |
| 8 | +})) |
| 9 | + |
| 10 | +vi.mock('../utils/walletManager', () => ({ |
| 11 | + walletManager: { |
| 12 | + getPublicKey: vi.fn(() => null), |
| 13 | + }, |
| 14 | +})) |
| 15 | + |
| 16 | +import { ErrorBoundary } from './ErrorBoundary' |
| 17 | +import { Sentry } from '../observability' |
| 18 | + |
| 19 | +function Bomb() { |
| 20 | + throw new Error('Test render error') |
| 21 | +} |
| 22 | + |
| 23 | +function Safe() { |
| 24 | + return <div>Safe content</div> |
| 25 | +} |
| 26 | + |
| 27 | +describe('ErrorBoundary', () => { |
| 28 | + afterEach(() => { |
| 29 | + cleanup() |
| 30 | + vi.clearAllMocks() |
| 31 | + }) |
| 32 | + |
| 33 | + it('renders children when no error occurs', () => { |
| 34 | + render( |
| 35 | + <ErrorBoundary> |
| 36 | + <Safe /> |
| 37 | + </ErrorBoundary>, |
| 38 | + ) |
| 39 | + expect(screen.getByText('Safe content')).toBeInTheDocument() |
| 40 | + }) |
| 41 | + |
| 42 | + it('renders fallback UI when a child throws', () => { |
| 43 | + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 44 | + |
| 45 | + render( |
| 46 | + <ErrorBoundary> |
| 47 | + <Bomb /> |
| 48 | + </ErrorBoundary>, |
| 49 | + ) |
| 50 | + |
| 51 | + expect(screen.getByText(/section error/i)).toBeInTheDocument() |
| 52 | + expect(screen.getByText(/something went wrong/i)).toBeInTheDocument() |
| 53 | + |
| 54 | + consoleError.mockRestore() |
| 55 | + }) |
| 56 | + |
| 57 | + it('reports the error to Sentry with context', () => { |
| 58 | + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 59 | + |
| 60 | + render( |
| 61 | + <ErrorBoundary fallbackTitle="PortfolioSection"> |
| 62 | + <Bomb /> |
| 63 | + </ErrorBoundary>, |
| 64 | + ) |
| 65 | + |
| 66 | + expect(Sentry.captureException).toHaveBeenCalledTimes(1) |
| 67 | + expect(Sentry.captureException).toHaveBeenCalledWith( |
| 68 | + expect.any(Error), |
| 69 | + expect.objectContaining({ |
| 70 | + extra: expect.objectContaining({ |
| 71 | + componentStack: expect.any(String), |
| 72 | + section: 'PortfolioSection', |
| 73 | + }), |
| 74 | + }), |
| 75 | + ) |
| 76 | + |
| 77 | + consoleError.mockRestore() |
| 78 | + }) |
| 79 | + |
| 80 | + it('resets error state on retry', () => { |
| 81 | + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 82 | + const onRetry = vi.fn() |
| 83 | + |
| 84 | + const { rerender } = render( |
| 85 | + <ErrorBoundary key="first" onRetry={onRetry}> |
| 86 | + <Bomb /> |
| 87 | + </ErrorBoundary>, |
| 88 | + ) |
| 89 | + |
| 90 | + expect(screen.getByText(/section error/i)).toBeInTheDocument() |
| 91 | + |
| 92 | + rerender( |
| 93 | + <ErrorBoundary key="second" onRetry={onRetry}> |
| 94 | + <Safe /> |
| 95 | + </ErrorBoundary>, |
| 96 | + ) |
| 97 | + |
| 98 | + expect(screen.getByText('Safe content')).toBeInTheDocument() |
| 99 | + |
| 100 | + consoleError.mockRestore() |
| 101 | + }) |
| 102 | + |
| 103 | + it('calls onRetry when retry button is clicked', () => { |
| 104 | + const consoleError = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 105 | + const onRetry = vi.fn() |
| 106 | + |
| 107 | + render( |
| 108 | + <ErrorBoundary onRetry={onRetry}> |
| 109 | + <Bomb /> |
| 110 | + </ErrorBoundary>, |
| 111 | + ) |
| 112 | + |
| 113 | + const retryButton = screen.getByRole('button', { name: /retry/i }) |
| 114 | + retryButton.click() |
| 115 | + |
| 116 | + expect(onRetry).toHaveBeenCalledTimes(1) |
| 117 | + |
| 118 | + consoleError.mockRestore() |
| 119 | + }) |
| 120 | +}) |
0 commit comments