-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathErrorBoundary.test.tsx
More file actions
70 lines (60 loc) · 1.73 KB
/
Copy pathErrorBoundary.test.tsx
File metadata and controls
70 lines (60 loc) · 1.73 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
/// <reference types="vitest" />
import { render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";
import { vi } from "vitest";
import { ErrorBoundary } from "./ErrorBoundary";
const ThrowError = () => {
throw new Error("Test error");
};
describe("ErrorBoundary", () => {
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
afterEach(() => {
consoleSpy.mockClear();
});
afterAll(() => {
consoleSpy.mockRestore();
});
it("renders children when no error", () => {
render(
<ErrorBoundary>
<div>Test content</div>
</ErrorBoundary>,
);
expect(screen.getByText("Test content")).toBeInTheDocument();
});
it("renders error page when error occurs", () => {
render(
<BrowserRouter>
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>
</BrowserRouter>,
);
expect(screen.getByText("Something went wrong")).toBeInTheDocument();
expect(
screen.getByText(
"We encountered an unexpected error. Our team has been notified and is working to fix it.",
),
).toBeInTheDocument();
expect(consoleSpy).toHaveBeenCalled();
});
it("has recovery actions", () => {
render(
<BrowserRouter>
<ErrorBoundary>
<ThrowError />
</ErrorBoundary>
</BrowserRouter>,
);
expect(
screen.getByRole("button", { name: /go back/i }),
).toBeInTheDocument();
expect(
screen.getByRole("link", { name: /dashboard/i }),
).toBeInTheDocument();
expect(screen.getByRole("button", { name: /reload/i })).toBeInTheDocument();
expect(
screen.getByRole("link", { name: /contact support/i }),
).toBeInTheDocument();
});
});