|
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import { describe, it, expect, vi, beforeEach, afterEach, type MockInstance } from "vitest"; |
| 3 | +import "@testing-library/jest-dom"; |
| 4 | +import ErrorBoundary from "../../components/ErrorBoundary"; |
| 5 | +import useAppStore from "../../store/store"; |
| 6 | + |
| 7 | +// Component that throws an error for testing |
| 8 | +const ThrowError = ({ shouldThrow }: { shouldThrow: boolean }) => { |
| 9 | + if (shouldThrow) { |
| 10 | + throw new Error("Test error"); |
| 11 | + } |
| 12 | + return <div>Child component</div>; |
| 13 | +}; |
| 14 | + |
| 15 | +describe("ErrorBoundary", () => { |
| 16 | + let consoleErrorSpy: MockInstance; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + // Suppress console.error for cleaner test output |
| 20 | + consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + consoleErrorSpy.mockRestore(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should render children when there is no error", () => { |
| 28 | + render( |
| 29 | + <ErrorBoundary> |
| 30 | + <ThrowError shouldThrow={false} /> |
| 31 | + </ErrorBoundary> |
| 32 | + ); |
| 33 | + |
| 34 | + expect(screen.getByText("Child component")).toBeInTheDocument(); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should catch error and render fallback UI", () => { |
| 38 | + render( |
| 39 | + <ErrorBoundary> |
| 40 | + <ThrowError shouldThrow={true} /> |
| 41 | + </ErrorBoundary> |
| 42 | + ); |
| 43 | + |
| 44 | + expect(screen.getByText("Something went wrong")).toBeInTheDocument(); |
| 45 | + expect( |
| 46 | + screen.getByText(/We apologize for the inconvenience/) |
| 47 | + ).toBeInTheDocument(); |
| 48 | + expect(screen.queryByText("Child component")).not.toBeInTheDocument(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should render reload button that calls window.location.reload", () => { |
| 52 | + const reloadMock = vi.fn(); |
| 53 | + // Mock the entire location object |
| 54 | + const originalLocation = window.location; |
| 55 | + delete (window as any).location; |
| 56 | + window.location = { ...originalLocation, reload: reloadMock } as any; |
| 57 | + |
| 58 | + render( |
| 59 | + <ErrorBoundary> |
| 60 | + <ThrowError shouldThrow={true} /> |
| 61 | + </ErrorBoundary> |
| 62 | + ); |
| 63 | + |
| 64 | + const reloadButton = screen.getByRole("button", { name: /Reload Page/i }); |
| 65 | + expect(reloadButton).toBeInTheDocument(); |
| 66 | + |
| 67 | + reloadButton.click(); |
| 68 | + expect(reloadMock).toHaveBeenCalledTimes(1); |
| 69 | + |
| 70 | + // Restore original location |
| 71 | + delete (window as any).location; |
| 72 | + window.location = originalLocation as any; |
| 73 | + }); |
| 74 | + |
| 75 | + it("should display error details in development mode", () => { |
| 76 | + render( |
| 77 | + <ErrorBoundary showDevDetails={true}> |
| 78 | + <ThrowError shouldThrow={true} /> |
| 79 | + </ErrorBoundary> |
| 80 | + ); |
| 81 | + |
| 82 | + const errorDetails = screen.getByText("Error details"); |
| 83 | + expect(errorDetails).toBeInTheDocument(); |
| 84 | + |
| 85 | + const errorMessage = screen.getByText(/Error: Test error/); |
| 86 | + expect(errorMessage).toBeInTheDocument(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not display error details in production mode", () => { |
| 90 | + render( |
| 91 | + <ErrorBoundary showDevDetails={false}> |
| 92 | + <ThrowError shouldThrow={true} /> |
| 93 | + </ErrorBoundary> |
| 94 | + ); |
| 95 | + |
| 96 | + expect(screen.queryByText("Error details")).not.toBeInTheDocument(); |
| 97 | + }); |
| 98 | + |
| 99 | + it("should call componentDidCatch and log error", () => { |
| 100 | + render( |
| 101 | + <ErrorBoundary> |
| 102 | + <ThrowError shouldThrow={true} /> |
| 103 | + </ErrorBoundary> |
| 104 | + ); |
| 105 | + |
| 106 | + expect(consoleErrorSpy).toHaveBeenCalled(); |
| 107 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 108 | + "ErrorBoundary caught an error:", |
| 109 | + expect.any(Error), |
| 110 | + expect.any(Object) |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should use theme colors from store", () => { |
| 115 | + // Capture previous state and set dark mode |
| 116 | + const previousBackgroundColor = useAppStore.getState().backgroundColor; |
| 117 | + const previousTextColor = useAppStore.getState().textColor; |
| 118 | + useAppStore.setState({ backgroundColor: '#121212', textColor: '#ffffff' }); |
| 119 | + |
| 120 | + try { |
| 121 | + render( |
| 122 | + <ErrorBoundary> |
| 123 | + <ThrowError shouldThrow={true} /> |
| 124 | + </ErrorBoundary> |
| 125 | + ); |
| 126 | + |
| 127 | + const container = screen.getByText("Something went wrong").parentElement; |
| 128 | + expect(container).toHaveStyle({ backgroundColor: '#121212' }); |
| 129 | + } finally { |
| 130 | + // Restore previous state to avoid test order-dependency |
| 131 | + useAppStore.setState({ |
| 132 | + backgroundColor: previousBackgroundColor, |
| 133 | + textColor: previousTextColor |
| 134 | + }); |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + it("should display component stack in development mode", async () => { |
| 139 | + render( |
| 140 | + <ErrorBoundary showDevDetails={true}> |
| 141 | + <ThrowError shouldThrow={true} /> |
| 142 | + </ErrorBoundary> |
| 143 | + ); |
| 144 | + |
| 145 | + const errorDetails = screen.getByText("Error details"); |
| 146 | + expect(errorDetails).toBeInTheDocument(); |
| 147 | + |
| 148 | + // Component stack should be present in the pre element |
| 149 | + // Use waitFor since errorInfo is set via setState in componentDidCatch |
| 150 | + await waitFor(() => { |
| 151 | + const preElement = screen.getByText(/Error: Test error/).closest('pre'); |
| 152 | + expect(preElement?.textContent).toContain('Component Stack:'); |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments