import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it, vi } from "vitest";
import { ErrorFallback } from "./error-fallback";

describe("error-fallback", () => {
  it("renders the default title and message", () => {
    render(<ErrorFallback />);
    expect(screen.getByText("Something went wrong")).toBeInTheDocument();
    expect(screen.getByText("An error occurred while loading this content.")).toBeInTheDocument();
  });

  it("renders a custom message", () => {
    render(<ErrorFallback message="Failed to load products." />);
    expect(screen.getByText("Failed to load products.")).toBeInTheDocument();
  });

  it("does not render the retry button when onReset is omitted", () => {
    render(<ErrorFallback />);
    expect(screen.queryByRole("button", { name: /try again/i })).not.toBeInTheDocument();
  });

  it("calls onReset when the retry button is clicked", async () => {
    const onReset = vi.fn();
    render(<ErrorFallback onReset={onReset} />);
    await userEvent.click(screen.getByRole("button", { name: /try again/i }));
    expect(onReset).toHaveBeenCalledOnce();
  });
});
