|
| 1 | +import React, { useState } from "react" |
| 2 | +import { act, render, screen, fireEvent } from "@testing-library/react" |
| 3 | +import useErrorBoundary from "../index" |
| 4 | +import { UseErrorBoundaryOptions } from "../../lib" |
| 5 | +import { renderHook } from "@testing-library/react-hooks" |
| 6 | + |
| 7 | +/** |
| 8 | + * Mock console and suppress errors. |
| 9 | + */ |
| 10 | +let consoleSpy: jest.SpyInstance | null = null |
| 11 | + |
| 12 | +beforeEach(() => { |
| 13 | + consoleSpy = jest.spyOn(console, "error").mockImplementation() |
| 14 | +}) |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + consoleSpy?.mockRestore() |
| 18 | +}) |
| 19 | + |
| 20 | +/** |
| 21 | + * Helper Components |
| 22 | + */ |
| 23 | + |
| 24 | +const ExplosionErrorMessage = "💥" |
| 25 | +const Explosion = () => { |
| 26 | + throw new Error(ExplosionErrorMessage) |
| 27 | +} |
| 28 | + |
| 29 | +// Use to test hook behaviour |
| 30 | +function ClickToExplode(options?: UseErrorBoundaryOptions) { |
| 31 | + const [explode, setExplode] = useState(false) |
| 32 | + const { ErrorBoundary, didCatch, error } = useErrorBoundary(options) |
| 33 | + |
| 34 | + return ( |
| 35 | + <> |
| 36 | + <div data-testid="boundary-inside"> |
| 37 | + <ErrorBoundary> |
| 38 | + {explode ? ( |
| 39 | + <Explosion /> |
| 40 | + ) : ( |
| 41 | + <button onClick={() => setExplode(true)}>Explode!</button> |
| 42 | + )} |
| 43 | + </ErrorBoundary> |
| 44 | + </div> |
| 45 | + <p data-testid="didcatch">{didCatch ? "true" : "false"}</p> |
| 46 | + <p data-testid="error-message">{error?.message}</p> |
| 47 | + </> |
| 48 | + ) |
| 49 | +} |
| 50 | + |
| 51 | +// Use to test render props of wrapped ErrorBoundary |
| 52 | +function InstantExplosion(options?: UseErrorBoundaryOptions) { |
| 53 | + const { ErrorBoundary } = useErrorBoundary(options) |
| 54 | + |
| 55 | + return ( |
| 56 | + <> |
| 57 | + <div data-testid="boundary-inside"> |
| 58 | + <ErrorBoundary |
| 59 | + render={() => <Explosion />} |
| 60 | + renderError={({ error }) => ( |
| 61 | + <p data-testid="error-message">{error.message}</p> |
| 62 | + )} |
| 63 | + /> |
| 64 | + </div> |
| 65 | + </> |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Test useErrorBoundary hook |
| 71 | + */ |
| 72 | + |
| 73 | +test("Hook initializes state", async () => { |
| 74 | + const { result } = renderHook(() => useErrorBoundary()) |
| 75 | + |
| 76 | + expect(result.current.didCatch).toBe(false) |
| 77 | + expect(result.current.ErrorBoundary).toBeDefined() |
| 78 | + expect(result.current.error).toBe(null) |
| 79 | +}) |
| 80 | + |
| 81 | +test("Wrapped ErrorBoundary catches error", async () => { |
| 82 | + const onDidCatch = jest.fn() |
| 83 | + |
| 84 | + render(<ClickToExplode onDidCatch={onDidCatch} />) |
| 85 | + |
| 86 | + act(() => { |
| 87 | + fireEvent.click(screen.getByText("Explode!")) |
| 88 | + }) |
| 89 | + |
| 90 | + // Boundary should render nothing |
| 91 | + expect(screen.getByTestId("boundary-inside")).toBeEmptyDOMElement() |
| 92 | + // Hook should provide didCatch and error |
| 93 | + expect(screen.getByTestId("didcatch")).toHaveTextContent("true") |
| 94 | + expect(screen.getByTestId("error-message")).toHaveTextContent( |
| 95 | + ExplosionErrorMessage |
| 96 | + ) |
| 97 | + // Provided callback should be called once |
| 98 | + expect(onDidCatch).toBeCalledTimes(1) |
| 99 | + // React and testing-library calls console.error when a boundary catches |
| 100 | + expect(console.error).toHaveBeenCalledTimes(2) |
| 101 | +}) |
| 102 | + |
| 103 | +test("Wrapped ErrorBoundary catches error with render props", async () => { |
| 104 | + const onDidCatch = jest.fn() |
| 105 | + |
| 106 | + render(<InstantExplosion onDidCatch={onDidCatch} />) |
| 107 | + |
| 108 | + // Hook should provide didCatch and error |
| 109 | + expect(screen.getByTestId("error-message")).toBeInTheDocument() |
| 110 | + expect(screen.getByTestId("error-message")).toHaveTextContent( |
| 111 | + ExplosionErrorMessage |
| 112 | + ) |
| 113 | + // Provided callback should be called once |
| 114 | + expect(onDidCatch).toBeCalledTimes(1) |
| 115 | + // React and testing-library calls console.error when a boundary catches |
| 116 | + expect(console.error).toHaveBeenCalledTimes(2) |
| 117 | +}) |
0 commit comments