Skip to content
Open
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
40 changes: 25 additions & 15 deletions src/components/shared/SuspenseWrapper.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ describe("<SuspenseWrapper />", () => {
);

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

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

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

// Fix the error condition and click retry
shouldError = false;
Expand All @@ -118,7 +120,11 @@ describe("<SuspenseWrapper />", () => {
expect(screen.getByTestId("recovered-content")).toBeInTheDocument();
});
expect(screen.getByText("Recovered")).toBeInTheDocument();
expect(screen.queryByText("There was an error!")).not.toBeInTheDocument();
expect(
screen.queryByRole("button", {
name: "A UI element failed to render. Click to retry.",
}),
).not.toBeInTheDocument();
});

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

// Should NOT show default error UI
expect(screen.queryByText("There was an error!")).not.toBeInTheDocument();
expect(
screen.queryByRole("button", { name: "Try again" }),
screen.queryByRole("button", {
name: "A UI element failed to render. Click to retry.",
}),
).not.toBeInTheDocument();
});

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

// Should show error UI
expect(screen.getByText("There was an error!")).toBeInTheDocument();
expect(
screen.getByRole("button", { name: "Try again" }),
).toBeInTheDocument();
const errorButton = screen.getByRole("button", {
name: "A UI element failed to render. Click to retry.",
});
expect(errorButton).toBeInTheDocument();
});

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

// Initially shows error
expect(screen.getByText("There was an error!")).toBeInTheDocument();
const retryButton = screen.getByRole("button", {
name: "A UI element failed to render. Click to retry.",
});
expect(retryButton).toBeInTheDocument();

// Fix error and retry
shouldError = false;
fireEvent.click(screen.getByRole("button", { name: "Try again" }));
fireEvent.click(retryButton);

// Should recover
await waitFor(() => {
Expand Down
20 changes: 13 additions & 7 deletions src/components/shared/SuspenseWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
} from "react";
import { ErrorBoundary, type FallbackProps } from "react-error-boundary";

import { Button } from "@/components/ui/button";
import { Icon } from "@/components/ui/icon";
import { Spinner } from "@/components/ui/spinner";

import { InfoBox } from "./InfoBox";
import TooltipButton from "./Buttons/TooltipButton";

type ErrorFallbackProps<T extends ComponentType<any>> = FallbackProps & {
originalProps?: Partial<ComponentProps<T>>;
Expand All @@ -25,12 +25,18 @@ interface SuspenseWrapperProps {
const ErrorFallback = <T extends ComponentType<any>>({
resetErrorBoundary,
}: ErrorFallbackProps<T>) => {
const tooltipText = "A UI element failed to render. Click to retry.";

return (
<InfoBox title="There was an error!" variant="error">
<Button onClick={() => resetErrorBoundary()} variant={"ghost"} size="xs">
Try again
</Button>
</InfoBox>
<TooltipButton
tooltip={tooltipText}
aria-label={tooltipText}
onClick={() => resetErrorBoundary()}
variant="ghost"
size="min"
>
<Icon name="MonitorX" className="text-destructive" />
</TooltipButton>
);
};

Expand Down
Loading