|
| 1 | +import { expect, it, describe, vi, beforeEach, beforeAll } from 'vitest'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import React, { Component, ErrorInfo, ReactNode } from 'react'; |
| 4 | + |
| 5 | +const mockTelemetry = { |
| 6 | + trackException: vi.fn(), |
| 7 | +}; |
| 8 | + |
| 9 | +vi.mock('../utils/telemetry', () => ({ |
| 10 | + telemetry: { |
| 11 | + trackException: (...args: unknown[]) => mockTelemetry.trackException(...args), |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +interface Props { |
| 16 | + children: ReactNode; |
| 17 | + forceError?: boolean; |
| 18 | +} |
| 19 | + |
| 20 | +interface State { |
| 21 | + hasError: boolean; |
| 22 | +} |
| 23 | + |
| 24 | +class TestErrorBoundary extends Component<Props, State> { |
| 25 | + public state: State = { |
| 26 | + hasError: this.props.forceError || false, |
| 27 | + }; |
| 28 | + |
| 29 | + public componentDidMount() { |
| 30 | + if (this.props.forceError) { |
| 31 | + this.componentDidCatch(new Error('Database connectivity error'), { componentStack: '' }); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + public componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
| 36 | + mockTelemetry.trackException(error, errorInfo); |
| 37 | + } |
| 38 | + |
| 39 | + public render() { |
| 40 | + if (this.state.hasError) { |
| 41 | + return ( |
| 42 | + <div data-testid="error-fallback"> |
| 43 | + <h2>Something went wrong</h2> |
| 44 | + <button onClick={() => this.setState({ hasError: false })}>Retry</button> |
| 45 | + </div> |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + return this.props.children; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe('WallOfLove Error Resilience', () => { |
| 54 | + let WallOfLoveModule: React.ComponentType<{ tweets: unknown[] }>; |
| 55 | + |
| 56 | + beforeAll(async () => { |
| 57 | + Object.defineProperty(window, 'matchMedia', { |
| 58 | + writable: true, |
| 59 | + value: vi.fn().mockImplementation((query) => ({ |
| 60 | + matches: false, |
| 61 | + media: query, |
| 62 | + onchange: null, |
| 63 | + addListener: vi.fn(), |
| 64 | + removeListener: vi.fn(), |
| 65 | + addEventListener: vi.fn(), |
| 66 | + removeEventListener: vi.fn(), |
| 67 | + dispatchEvent: vi.fn(), |
| 68 | + })), |
| 69 | + }); |
| 70 | + |
| 71 | + const mod = await import('./WallOfLove'); |
| 72 | + WallOfLoveModule = mod.WallOfLove; |
| 73 | + }); |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + vi.clearAllMocks(); |
| 77 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should maintain hydration stability when initial data is missing', () => { |
| 81 | + const { container } = render(<WallOfLoveModule tweets={[]} />); |
| 82 | + expect(container).toBeDefined(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should render error recovery UI when runtime exception occurs', () => { |
| 86 | + render( |
| 87 | + <TestErrorBoundary forceError={true}> |
| 88 | + <WallOfLoveModule tweets={[]} /> |
| 89 | + </TestErrorBoundary> |
| 90 | + ); |
| 91 | + |
| 92 | + expect(screen.getByTestId('error-fallback')).toBeDefined(); |
| 93 | + expect(screen.getByText(/something went wrong/i)).toBeDefined(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should log exception to dev-telemetry tracker on failure', () => { |
| 97 | + render( |
| 98 | + <TestErrorBoundary forceError={true}> |
| 99 | + <WallOfLoveModule tweets={[]} /> |
| 100 | + </TestErrorBoundary> |
| 101 | + ); |
| 102 | + |
| 103 | + expect(mockTelemetry.trackException).toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should prevent full screen crashes using localized boundary containment', () => { |
| 107 | + render( |
| 108 | + <div> |
| 109 | + <header data-testid="app-header">App Header</header> |
| 110 | + <TestErrorBoundary forceError={true}> |
| 111 | + <WallOfLoveModule tweets={[]} /> |
| 112 | + </TestErrorBoundary> |
| 113 | + </div> |
| 114 | + ); |
| 115 | + expect(screen.getByTestId('app-header')).toBeDefined(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should trigger user reset or reload path on recovery panel click', () => { |
| 119 | + render( |
| 120 | + <TestErrorBoundary forceError={true}> |
| 121 | + <WallOfLoveModule tweets={[]} /> |
| 122 | + </TestErrorBoundary> |
| 123 | + ); |
| 124 | + |
| 125 | + const resetButton = screen.getByRole('button', { name: /retry/i }); |
| 126 | + fireEvent.click(resetButton); |
| 127 | + |
| 128 | + expect(screen.queryByTestId('error-fallback')).toBeNull(); |
| 129 | + }); |
| 130 | +}); |
0 commit comments