import { describe, expect, it, vi } from "vitest";
import { render, screen } from "~/test-utils.js";
import userEvent from "@testing-library/user-event";
import { EmptyState } from "./EmptyState.js";

describe("EmptyState", () => {
  it("renders the title", () => {
    render(<EmptyState title="No results" />);
    expect(screen.getByText("No results")).toBeInTheDocument();
  });

  it("renders detail text when provided", () => {
    render(<EmptyState title="Empty" detail="Try adjusting your filters" />);
    expect(screen.getByText("Try adjusting your filters")).toBeInTheDocument();
  });

  it("does not render detail when not provided", () => {
    const { container } = render(<EmptyState title="Empty" />);
    const paragraphs = container.querySelectorAll("p");
    expect(paragraphs).toHaveLength(1);
  });

  it("renders an action button when action is provided", () => {
    const action = { label: "Create new", onClick: vi.fn() };
    render(<EmptyState title="Empty" action={action} />);
    expect(screen.getByRole("button", { name: "Create new" })).toBeInTheDocument();
  });

  it("calls action onClick when button is clicked", async () => {
    const onClick = vi.fn();
    render(<EmptyState title="Empty" action={{ label: "Add", onClick }} />);
    await userEvent.click(screen.getByRole("button", { name: "Add" }));
    expect(onClick).toHaveBeenCalledOnce();
  });

  it("does not render action button when not provided", () => {
    render(<EmptyState title="Empty" />);
    expect(screen.queryByRole("button")).not.toBeInTheDocument();
  });

  it("renders icon when provided", () => {
    render(<EmptyState title="Empty" icon={<span data-testid="icon">Icon</span>} />);
    expect(screen.getByTestId("icon")).toBeInTheDocument();
  });

  it("applies testId to the container", () => {
    render(<EmptyState title="Empty" testId="empty-state" />);
    expect(screen.getByTestId("empty-state")).toBeInTheDocument();
  });
});
