|
| 1 | +import React, { Component, ErrorInfo, ReactNode } from 'react'; |
| 2 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 3 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 4 | +import '@testing-library/jest-dom'; |
| 5 | +import Template from './template'; |
| 6 | + |
| 7 | +// Mock telemetry tracker |
| 8 | +const mockTelemetryLogger = vi.fn(); |
| 9 | + |
| 10 | +// Localized Error Boundary for testing exception safety and fallbacks |
| 11 | +interface ErrorBoundaryProps { |
| 12 | + children: ReactNode; |
| 13 | + onReset: () => void; |
| 14 | + telemetryLogger?: (error: Error, errorInfo: ErrorInfo) => void; |
| 15 | +} |
| 16 | + |
| 17 | +interface ErrorBoundaryState { |
| 18 | + hasError: boolean; |
| 19 | + error: Error | null; |
| 20 | +} |
| 21 | + |
| 22 | +class LocalErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> { |
| 23 | + constructor(props: ErrorBoundaryProps) { |
| 24 | + super(props); |
| 25 | + this.state = { hasError: false, error: null }; |
| 26 | + } |
| 27 | + |
| 28 | + static getDerivedStateFromError(error: Error): ErrorBoundaryState { |
| 29 | + return { hasError: true, error }; |
| 30 | + } |
| 31 | + |
| 32 | + componentDidCatch(error: Error, errorInfo: ErrorInfo) { |
| 33 | + if (this.props.telemetryLogger) { |
| 34 | + this.props.telemetryLogger(error, errorInfo); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + render() { |
| 39 | + if (this.state.hasError) { |
| 40 | + return ( |
| 41 | + <div data-testid="error-boundary-fallback"> |
| 42 | + <h1>Something went wrong.</h1> |
| 43 | + <p>{this.state.error?.message}</p> |
| 44 | + <button |
| 45 | + onClick={() => { |
| 46 | + this.setState({ hasError: false, error: null }); |
| 47 | + this.props.onReset(); |
| 48 | + }} |
| 49 | + > |
| 50 | + Try again |
| 51 | + </button> |
| 52 | + </div> |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + return this.props.children; |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// Dummy components to simulate varied conditions |
| 61 | +const BuggyRuntimeChild = () => { |
| 62 | + throw new Error('Unexpected runtime exception!'); |
| 63 | +}; |
| 64 | + |
| 65 | +const BuggyDatabaseChild = () => { |
| 66 | + throw new Error('Database connection timeout or anomaly!'); |
| 67 | +}; |
| 68 | + |
| 69 | +// Start testing suite |
| 70 | +describe('Template Component: Hydration Stability, Exception Safety & Error Fallbacks', () => { |
| 71 | + let mockReset: ReturnType<typeof vi.fn>; |
| 72 | + |
| 73 | + beforeEach(() => { |
| 74 | + mockReset = vi.fn(); |
| 75 | + mockTelemetryLogger.mockClear(); |
| 76 | + |
| 77 | + // Suppress console.error in tests to keep output clean when errors are thrown intentionally |
| 78 | + vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 79 | + }); |
| 80 | + |
| 81 | + afterEach(() => { |
| 82 | + vi.restoreAllMocks(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('Test 1: Hydration Stability - renders valid children without crashing', () => { |
| 86 | + render( |
| 87 | + // @ts-expect-error Mock does not perfectly match the function signature but is callable |
| 88 | + <LocalErrorBoundary onReset={mockReset} telemetryLogger={mockTelemetryLogger}> |
| 89 | + <Template> |
| 90 | + <div data-testid="valid-child">Valid Content</div> |
| 91 | + </Template> |
| 92 | + </LocalErrorBoundary> |
| 93 | + ); |
| 94 | + |
| 95 | + // Assert that the target modules render cleanly |
| 96 | + expect(screen.getByTestId('valid-child')).toBeInTheDocument(); |
| 97 | + expect(screen.queryByTestId('error-boundary-fallback')).not.toBeInTheDocument(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('Test 2: Exception Safety - catches unexpected runtime exceptions and renders localized boundary element', () => { |
| 101 | + render( |
| 102 | + // @ts-expect-error Mock does not perfectly match the function signature but is callable |
| 103 | + <LocalErrorBoundary onReset={mockReset} telemetryLogger={mockTelemetryLogger}> |
| 104 | + <Template> |
| 105 | + <BuggyRuntimeChild /> |
| 106 | + </Template> |
| 107 | + </LocalErrorBoundary> |
| 108 | + ); |
| 109 | + |
| 110 | + // Assert that the site does not crash, but rather displays the localized error recovery UI |
| 111 | + expect(screen.getByTestId('error-boundary-fallback')).toBeInTheDocument(); |
| 112 | + expect(screen.getByText('Something went wrong.')).toBeInTheDocument(); |
| 113 | + expect(screen.getByText('Unexpected runtime exception!')).toBeInTheDocument(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('Test 3: Dev-Telemetry - verifies exceptions are logged to dev-telemetry trackers appropriately', () => { |
| 117 | + render( |
| 118 | + // @ts-expect-error Mock does not perfectly match the function signature but is callable |
| 119 | + <LocalErrorBoundary onReset={mockReset} telemetryLogger={mockTelemetryLogger}> |
| 120 | + <Template> |
| 121 | + <BuggyRuntimeChild /> |
| 122 | + </Template> |
| 123 | + </LocalErrorBoundary> |
| 124 | + ); |
| 125 | + |
| 126 | + // Assert that the mock telemetry logger was called with the error object |
| 127 | + expect(mockTelemetryLogger).toHaveBeenCalledTimes(1); |
| 128 | + const [errorArg] = mockTelemetryLogger.mock.calls[0]; |
| 129 | + expect(errorArg).toBeInstanceOf(Error); |
| 130 | + expect(errorArg.message).toBe('Unexpected runtime exception!'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('Test 4: Error Fallbacks - isolates and handles mocked database connectivity errors properly', () => { |
| 134 | + render( |
| 135 | + // @ts-expect-error Mock does not perfectly match the function signature but is callable |
| 136 | + <LocalErrorBoundary onReset={mockReset} telemetryLogger={mockTelemetryLogger}> |
| 137 | + <Template> |
| 138 | + <BuggyDatabaseChild /> |
| 139 | + </Template> |
| 140 | + </LocalErrorBoundary> |
| 141 | + ); |
| 142 | + |
| 143 | + // Assert that database connectivity anomalies trigger the error recovery fallback |
| 144 | + expect(screen.getByTestId('error-boundary-fallback')).toBeInTheDocument(); |
| 145 | + expect(screen.getByText('Database connection timeout or anomaly!')).toBeInTheDocument(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('Test 5: Reset/Reload Paths - ensures user reset/reload paths are available on recovery panels and functioning', () => { |
| 149 | + render( |
| 150 | + // @ts-expect-error Mock does not perfectly match the function signature but is callable |
| 151 | + <LocalErrorBoundary onReset={mockReset} telemetryLogger={mockTelemetryLogger}> |
| 152 | + <Template> |
| 153 | + <BuggyRuntimeChild /> |
| 154 | + </Template> |
| 155 | + </LocalErrorBoundary> |
| 156 | + ); |
| 157 | + |
| 158 | + // Verify recovery panel is present |
| 159 | + expect(screen.getByTestId('error-boundary-fallback')).toBeInTheDocument(); |
| 160 | + |
| 161 | + // Find and click the 'Try again' reset button |
| 162 | + const resetButton = screen.getByRole('button', { name: /try again/i }); |
| 163 | + expect(resetButton).toBeInTheDocument(); |
| 164 | + |
| 165 | + fireEvent.click(resetButton); |
| 166 | + |
| 167 | + // Verify the mockReset callback is executed when the user initiates a reset path |
| 168 | + expect(mockReset).toHaveBeenCalledTimes(1); |
| 169 | + }); |
| 170 | +}); |
0 commit comments