forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorBoundary.test.tsx
More file actions
92 lines (82 loc) · 2.61 KB
/
Copy pathErrorBoundary.test.tsx
File metadata and controls
92 lines (82 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { screen, fireEvent } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import { renderWithProviders } from "@/test/render";
import { ErrorBoundary } from "./ErrorBoundary";
function ThrowingComponent({ shouldThrow = true }: { shouldThrow?: boolean }) {
if (shouldThrow) {
throw new Error("Test error message");
}
return <div>Child content</div>;
}
describe("ErrorBoundary", () => {
it("renders children when no error occurs", () => {
renderWithProviders(
<ErrorBoundary>
<div>No error here</div>
</ErrorBoundary>
);
expect(screen.getByText("No error here")).toBeInTheDocument();
});
it("renders error UI when child throws", () => {
renderWithProviders(
<ErrorBoundary>
<ThrowingComponent />
</ErrorBoundary>
);
expect(screen.getByText("Oops, something went wrong.")).toBeInTheDocument();
});
it("displays a generic message without leaking the internal error text", () => {
renderWithProviders(
<ErrorBoundary>
<ThrowingComponent />
</ErrorBoundary>
);
expect(
screen.getByText(/we hit an unexpected error loading this page/i)
).toBeInTheDocument();
expect(screen.queryByText("Test error message")).not.toBeInTheDocument();
});
it("renders reload button", () => {
renderWithProviders(
<ErrorBoundary>
<ThrowingComponent />
</ErrorBoundary>
);
expect(screen.getByRole("button", { name: /reload page/i })).toBeInTheDocument();
});
it("calls window.location.reload when reload button is clicked", () => {
const mockReload = vi.fn();
Object.defineProperty(window, "location", {
value: { reload: mockReload },
writable: true,
});
renderWithProviders(
<ErrorBoundary>
<ThrowingComponent />
</ErrorBoundary>
);
fireEvent.click(screen.getByRole("button", { name: /reload page/i }));
expect(mockReload).toHaveBeenCalledTimes(1);
});
it("does not render error UI when no error", () => {
renderWithProviders(
<ErrorBoundary>
<ThrowingComponent shouldThrow={false} />
</ErrorBoundary>
);
expect(screen.queryByText("Oops, something went wrong.")).not.toBeInTheDocument();
expect(screen.getByText("Child content")).toBeInTheDocument();
});
it("catches errors from deeply nested children", () => {
renderWithProviders(
<ErrorBoundary>
<div>
<div>
<ThrowingComponent />
</div>
</div>
</ErrorBoundary>
);
expect(screen.getByText("Oops, something went wrong.")).toBeInTheDocument();
});
});