diff --git a/lib/components/ErrorBoundary.test.tsx b/lib/components/ErrorBoundary.test.tsx index c5d8409..eb19595 100644 --- a/lib/components/ErrorBoundary.test.tsx +++ b/lib/components/ErrorBoundary.test.tsx @@ -317,25 +317,39 @@ describe("ErrorBoundary", () => { describe("thrown values", () => { let lastRenderedError: unknown | null = null; + let lastRenderedResetErrorBoundary: + | FallbackProps["resetErrorBoundary"] + | null = null; let fallbackRender: (props: FallbackProps) => ReactElement; let onError: Mock<(...args: unknown[]) => unknown>; beforeEach(() => { lastRenderedError = null; + lastRenderedResetErrorBoundary = null; onError = vi.fn(); - fallbackRender = ({ error }: FallbackProps) => { + fallbackRender = ({ error, resetErrorBoundary }: FallbackProps) => { lastRenderedError = error; + lastRenderedResetErrorBoundary = resetErrorBoundary; return
Error
; }; }); - function render() { + function render( + props: Omit< + ErrorBoundaryPropsWithRender, + "fallbackRender" | "onError" + > = {}, + ) { act(() => { root.render( - + Content , ); @@ -365,6 +379,34 @@ describe("ErrorBoundary", () => { expect(onError.mock.calls[0][0]).toEqual(null); expect(container.textContent).toBe("Error"); }); + + it("should re-render children if a boundary that caught a thrown null is reset via prop", () => { + shouldThrow = true; + valueToThrow = null; + + render(); + expect(container.textContent).toBe("Error"); + + act(() => { + shouldThrow = false; + assert(lastRenderedResetErrorBoundary !== null); + lastRenderedResetErrorBoundary(); + }); + + expect(container.textContent).toBe("Content"); + }); + + it("should re-render children if a boundary that caught a thrown null is reset reset keys", () => { + shouldThrow = true; + valueToThrow = null; + + render({ resetKeys: [1] }); + expect(container.textContent).toBe("Error"); + + shouldThrow = false; + render({ resetKeys: [2] }); + expect(container.textContent).toBe("Content"); + }); }); // TODO Various cases with resetKeys changing (length, order, etc) diff --git a/lib/components/ErrorBoundary.tsx b/lib/components/ErrorBoundary.tsx index fc35eed..5468fd3 100644 --- a/lib/components/ErrorBoundary.tsx +++ b/lib/components/ErrorBoundary.tsx @@ -58,9 +58,9 @@ export class ErrorBoundary extends Component< } resetErrorBoundary(...args: unknown[]) { - const { error } = this.state; + const { didCatch } = this.state; - if (error !== null) { + if (didCatch) { this.props.onReset?.({ args, reason: "imperative-api", @@ -88,7 +88,7 @@ export class ErrorBoundary extends Component< if ( didCatch && - prevState.error !== null && + prevState.didCatch && hasArrayChanged(prevProps.resetKeys, resetKeys) ) { this.props.onReset?.({