"use client";

import * as Sentry from "@sentry/nextjs";
import { render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ErrorBoundary } from "./error-boundary";

vi.mock("@sentry/nextjs", () => ({
  captureException: vi.fn(),
}));

function ThrowingComponent(): never {
  throw new Error("boom");
}

describe("ErrorBoundary", () => {
  afterEach(() => {
    vi.clearAllMocks();
  });

  it("captures client render errors in Sentry", async () => {
    render(
      <ErrorBoundary>
        <ThrowingComponent />
      </ErrorBoundary>,
    );

    expect(screen.getByText("Something went wrong")).toBeInTheDocument();
    await waitFor(() => {
      expect(Sentry.captureException).toHaveBeenCalledWith(
        expect.objectContaining({ message: "boom" }),
        expect.objectContaining({
          extra: expect.objectContaining({
            componentStack: expect.any(String),
          }),
        }),
      );
    });
  });
});
