Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions lib/components/ErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Error</div>;
};
});

function render() {
function render(
props: Omit<
ErrorBoundaryPropsWithRender,
"fallbackRender" | "onError"
> = {},
) {
act(() => {
root.render(
<ErrorBoundary fallbackRender={fallbackRender} onError={onError}>
<ErrorBoundary
{...props}
fallbackRender={fallbackRender}
onError={onError}
>
<MaybeThrows>Content</MaybeThrows>
</ErrorBoundary>,
);
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions lib/components/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ErrorBoundary extends Component<

if (
didCatch &&
prevState.error !== null &&
prevState.didCatch &&
hasArrayChanged(prevProps.resetKeys, resetKeys)
) {
this.props.onReset?.({
Expand Down