import { ViewCartButton } from "./index";

import { fireEvent, render, screen } from "@testing-library/react";

describe("ViewCartButton", () => {
  const defaultProps = {
    cartTotalText: "$49.99/mo",
    onClick: jest.fn(),
    isOpen: false,
  };

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

  it("renders 'View cart' label", () => {
    render(<ViewCartButton {...defaultProps} />);
    expect(screen.getByText("View cart")).toBeInTheDocument();
  });

  it("renders cart total text", () => {
    render(<ViewCartButton {...defaultProps} />);
    expect(screen.getByTestId("cart-total")).toHaveTextContent("$49.99/mo");
  });

  it("calls onClick when clicked", () => {
    render(<ViewCartButton {...defaultProps} />);
    fireEvent.click(screen.getByRole("button"));
    expect(defaultProps.onClick).toHaveBeenCalledTimes(1);
  });

  it("renders arrow icon", () => {
    render(<ViewCartButton {...defaultProps} />);
    expect(screen.getByText("keyboard_arrow_up")).toBeInTheDocument();
  });

  it("applies rotate-0 class when isOpen is false", () => {
    render(<ViewCartButton {...defaultProps} isOpen={false} />);
    const icon = screen.getByText("keyboard_arrow_up");
    expect(icon.className).toContain("rotate-0");
  });

  it("applies rotate-180 class when isOpen is true", () => {
    render(<ViewCartButton {...defaultProps} isOpen={true} />);
    const icon = screen.getByText("keyboard_arrow_up");
    expect(icon.className).toContain("rotate-180");
  });

  it("passes additional button attributes", () => {
    render(
      <ViewCartButton {...defaultProps} disabled={true} aria-label="cart" />
    );
    const button = screen.getByRole("button");
    expect(button).toBeDisabled();
    expect(button).toHaveAttribute("aria-label", "cart");
  });
});
