import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import { Package } from "lucide-react";
import EmptyState from "./EmptyState";

describe("EmptyState", () => {
  it("renders title", () => {
    render(<EmptyState icon={Package} title="没有数据" />);
    expect(screen.getByText("没有数据")).toBeInTheDocument();
  });

  it("renders subtitle when provided", () => {
    render(
      <EmptyState icon={Package} title="空" subtitle="还没有任何内容" />,
    );
    expect(screen.getByText("还没有任何内容")).toBeInTheDocument();
  });

  it("does not render subtitle when not provided", () => {
    const { container } = render(<EmptyState icon={Package} title="空" />);
    expect(container.querySelectorAll("p")).toHaveLength(1);
  });

  it("renders CTA button when ctaLabel and onCta provided", () => {
    const onCta = vi.fn();
    render(
      <EmptyState icon={Package} title="空" ctaLabel="添加" onCta={onCta} />,
    );
    const button = screen.getByText("添加");
    expect(button).toBeInTheDocument();

    fireEvent.click(button);
    expect(onCta).toHaveBeenCalled();
  });

  it("does not render CTA without both ctaLabel and onCta", () => {
    render(<EmptyState icon={Package} title="空" ctaLabel="添加" />);
    expect(screen.queryByText("添加")).not.toBeInTheDocument();
  });

  it("renders image when provided", () => {
    render(
      <EmptyState icon={Package} title="空" image="/test-image.png" />,
    );
    expect(screen.getByAltText("空")).toBeInTheDocument();
  });
});
