Skip to content

Commit ca40873

Browse files
committed
A more modest error fallback
1 parent 3a98a7c commit ca40873

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

src/components/shared/SuspenseWrapper.test.tsx

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ describe("<SuspenseWrapper />", () => {
8282
);
8383

8484
// Should show error message and retry button
85-
expect(screen.getByText("There was an error!")).toBeInTheDocument();
86-
expect(
87-
screen.getByRole("button", { name: "Try again" }),
88-
).toBeInTheDocument();
85+
const errorButton = screen.getByRole("button", {
86+
name: "A UI element failed to render. Click to retry.",
87+
});
88+
expect(errorButton).toBeInTheDocument();
8989
expect(screen.queryByTestId("success-content")).not.toBeInTheDocument();
9090
});
9191

@@ -106,8 +106,10 @@ describe("<SuspenseWrapper />", () => {
106106
);
107107

108108
// Initially shows error
109-
expect(screen.getByText("There was an error!")).toBeInTheDocument();
110-
const retryButton = screen.getByRole("button", { name: "Try again" });
109+
const retryButton = screen.getByRole("button", {
110+
name: "A UI element failed to render. Click to retry.",
111+
});
112+
expect(retryButton).toBeInTheDocument();
111113

112114
// Fix the error condition and click retry
113115
shouldError = false;
@@ -118,7 +120,11 @@ describe("<SuspenseWrapper />", () => {
118120
expect(screen.getByTestId("recovered-content")).toBeInTheDocument();
119121
});
120122
expect(screen.getByText("Recovered")).toBeInTheDocument();
121-
expect(screen.queryByText("There was an error!")).not.toBeInTheDocument();
123+
expect(
124+
screen.queryByRole("button", {
125+
name: "A UI element failed to render. Click to retry.",
126+
}),
127+
).not.toBeInTheDocument();
122128
});
123129

124130
test("shows custom errorFallback when provided", () => {
@@ -145,9 +151,10 @@ describe("<SuspenseWrapper />", () => {
145151
expect(screen.getByTestId("custom-retry")).toBeInTheDocument();
146152

147153
// Should NOT show default error UI
148-
expect(screen.queryByText("There was an error!")).not.toBeInTheDocument();
149154
expect(
150-
screen.queryByRole("button", { name: "Try again" }),
155+
screen.queryByRole("button", {
156+
name: "A UI element failed to render. Click to retry.",
157+
}),
151158
).not.toBeInTheDocument();
152159
});
153160

@@ -376,10 +383,10 @@ describe("withSuspenseWrapper()", () => {
376383
render(<WrappedComponent />);
377384

378385
// Should show error UI
379-
expect(screen.getByText("There was an error!")).toBeInTheDocument();
380-
expect(
381-
screen.getByRole("button", { name: "Try again" }),
382-
).toBeInTheDocument();
386+
const errorButton = screen.getByRole("button", {
387+
name: "A UI element failed to render. Click to retry.",
388+
});
389+
expect(errorButton).toBeInTheDocument();
383390
});
384391

385392
test("wrapped component can recover from error", async () => {
@@ -397,11 +404,14 @@ describe("withSuspenseWrapper()", () => {
397404
render(<WrappedComponent />);
398405

399406
// Initially shows error
400-
expect(screen.getByText("There was an error!")).toBeInTheDocument();
407+
const retryButton = screen.getByRole("button", {
408+
name: "A UI element failed to render. Click to retry.",
409+
});
410+
expect(retryButton).toBeInTheDocument();
401411

402412
// Fix error and retry
403413
shouldError = false;
404-
fireEvent.click(screen.getByRole("button", { name: "Try again" }));
414+
fireEvent.click(retryButton);
405415

406416
// Should recover
407417
await waitFor(() => {

src/components/shared/SuspenseWrapper.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
} from "react";
99
import { ErrorBoundary, type FallbackProps } from "react-error-boundary";
1010

11-
import { Button } from "@/components/ui/button";
11+
import { Icon } from "@/components/ui/icon";
1212
import { Spinner } from "@/components/ui/spinner";
1313

14-
import { InfoBox } from "./InfoBox";
14+
import TooltipButton from "./Buttons/TooltipButton";
1515

1616
type ErrorFallbackProps<T extends ComponentType<any>> = FallbackProps & {
1717
originalProps?: Partial<ComponentProps<T>>;
@@ -25,12 +25,18 @@ interface SuspenseWrapperProps {
2525
const ErrorFallback = <T extends ComponentType<any>>({
2626
resetErrorBoundary,
2727
}: ErrorFallbackProps<T>) => {
28+
const tooltipText = "A UI element failed to render. Click to retry.";
29+
2830
return (
29-
<InfoBox title="There was an error!" variant="error">
30-
<Button onClick={() => resetErrorBoundary()} variant={"ghost"} size="xs">
31-
Try again
32-
</Button>
33-
</InfoBox>
31+
<TooltipButton
32+
tooltip={tooltipText}
33+
aria-label={tooltipText}
34+
onClick={() => resetErrorBoundary()}
35+
variant="ghost"
36+
size="min"
37+
>
38+
<Icon name="MonitorX" className="text-destructive" />
39+
</TooltipButton>
3440
);
3541
};
3642

0 commit comments

Comments
 (0)