import * as Sentry from "@sentry/nextjs"; import { cleanup, render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { afterEach, describe, expect, test, vi } from "vitest"; import ErrorBoundary from "./error"; vi.mock("@/modules/ui/components/button", () => ({ Button: (props: any) => , })); vi.mock("@/modules/ui/components/error-component", () => ({ ErrorComponent: () =>
ErrorComponent
, })); vi.mock("@sentry/nextjs", () => ({ captureException: vi.fn(), })); describe("ErrorBoundary", () => { afterEach(() => { cleanup(); }); const dummyError = new Error("Test error"); const resetMock = vi.fn(); test("logs error via console.error in development", async () => { (process.env as { [key: string]: string }).NODE_ENV = "development"; const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); vi.mocked(Sentry.captureException).mockImplementation((() => {}) as any); render(); await waitFor(() => { expect(consoleErrorSpy).toHaveBeenCalledWith("Test error"); }); expect(Sentry.captureException).not.toHaveBeenCalled(); }); test("captures error with Sentry in production", async () => { (process.env as { [key: string]: string }).NODE_ENV = "production"; const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {}); vi.mocked(Sentry.captureException).mockImplementation((() => {}) as any); render(); await waitFor(() => { expect(Sentry.captureException).toHaveBeenCalled(); }); expect(consoleErrorSpy).not.toHaveBeenCalled(); }); test("calls reset when try again button is clicked", async () => { render(); const tryAgainBtn = screen.getByRole("button", { name: "common.try_again" }); userEvent.click(tryAgainBtn); await waitFor(() => expect(resetMock).toHaveBeenCalled()); }); test("sets window.location.href to '/' when dashboard button is clicked", async () => { const originalLocation = window.location; delete (window as any).location; (window as any).location = { href: "" }; render(); const dashBtn = screen.getByRole("button", { name: "common.go_to_dashboard" }); userEvent.click(dashBtn); await waitFor(() => { expect(window.location.href).toBe("/"); }); window.location = originalLocation; }); });