import { render, screen, fireEvent } from "@testing-library/react";
import EditButton from "@/apps/new-ui/components/CustomeComponents/EditButton";

// Mock Tooltip
jest.mock("@/components/ui/tooltip", () => ({
  HoverTooltip: ({ label, children }: any) => (
    <div data-testid="tooltip" data-label={label}>
      {children}
    </div>
  ),
}));

describe("EditButton Component", () => {
  const handleClick = jest.fn();

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it("renders icon type and triggers onclick", () => {
    render(<EditButton onclick={handleClick} type="icon" title="ویرایش کردن" />);

    const button = screen.getByRole("button");

    expect(button).toBeInTheDocument();
    expect(screen.getByTestId("tooltip")).toHaveAttribute(
      "data-label",
      "ویرایش کردن"
    );

    fireEvent.click(button);
    expect(handleClick).toHaveBeenCalledTimes(1);
  });

  it("renders withBackground type and respects disabled state", () => {
    const { rerender } = render(
      <EditButton onclick={handleClick} type="withBackground" disabled />
    );

    const button = screen.getByRole("button");
    expect(button).toBeDisabled();

    rerender(<EditButton onclick={handleClick} type="withBackground" />);
    expect(screen.getByRole("button")).not.toBeDisabled();
  });

  it("renders accordion type and stops propagation", () => {
    const stopPropagation = jest.fn();

    render(<EditButton onclick={handleClick} type="accordion" />);

    const button = screen.getByRole("button");

    // ساخت ایونت واقعی
    const event = new MouseEvent("click", { bubbles: true });
    Object.defineProperty(event, "stopPropagation", {
      value: stopPropagation,
    });

    fireEvent(button, event);

    expect(handleClick).toHaveBeenCalledTimes(1);
    expect(stopPropagation).toHaveBeenCalledTimes(1);
  });
});
