Skip to content

Commit 46b245b

Browse files
committed
test: add ErrorBoundary
1 parent 7ed6b75 commit 46b245b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/components/ErrorBoundary.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export default class ErrorBoundary extends Component<Props, State> {
1515
hasError: false,
1616
};
1717

18-
public static getDerivedStateFromError(_: Error): State {
18+
public static getDerivedStateFromError(): State {
1919
return { hasError: true };
2020
}
2121

22-
public componentDidCatch(error: Error) {
23-
console.error(error);
22+
public componentDidCatch() {
23+
// this should log the error to a service like Sentry
2424
}
2525

2626
public render() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { render } from "@/lib/test-utils";
2+
import { screen } from "@testing-library/react";
3+
import { describe, expect, it, vi } from "vitest";
4+
import ErrorBoundary from "../ErrorBoundary";
5+
import { Error } from "../Error";
6+
7+
const ErrorComponent = () => {
8+
throw Error();
9+
};
10+
11+
describe("ErrorBoundary", () => {
12+
it("renders fallback when a child throws an error", () => {
13+
vi.spyOn(console, "error").mockImplementation(() => {});
14+
render(
15+
<ErrorBoundary fallback={<Error />}>
16+
<ErrorComponent />
17+
</ErrorBoundary>,
18+
);
19+
20+
expect(screen.getByText(/an error occurred/i)).toBeVisible();
21+
});
22+
});

0 commit comments

Comments
 (0)