import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { vi } from "vitest";

import { HorizonSelector } from "../HorizonSelector";

describe("HorizonSelector", () => {
  it("renders one radio per accepted horizon, incl. 24 (ТЗ §12.1)", () => {
    render(<HorizonSelector value={12} onChange={() => {}} />);
    const radios = screen.getAllByRole("radio");
    expect(radios.map((r) => r.textContent)).toEqual([
      "6 мес",
      "12 мес",
      "18 мес",
      "24 мес",
    ]);
  });

  it("marks the selected horizon via aria-checked", () => {
    render(<HorizonSelector value={24} onChange={() => {}} />);
    const selected = screen.getByRole("radio", { name: "24 мес" });
    expect(selected).toHaveAttribute("aria-checked", "true");
    expect(screen.getByRole("radio", { name: "12 мес" })).toHaveAttribute(
      "aria-checked",
      "false",
    );
  });

  it("calls onChange with 24 when the 24-month segment is clicked", async () => {
    const onChange = vi.fn();
    render(<HorizonSelector value={12} onChange={onChange} />);
    await userEvent.click(screen.getByRole("radio", { name: "24 мес" }));
    expect(onChange).toHaveBeenCalledWith(24);
  });

  it("does not fire onChange while disabled", async () => {
    const onChange = vi.fn();
    render(<HorizonSelector value={12} onChange={onChange} disabled />);
    await userEvent.click(screen.getByRole("radio", { name: "24 мес" }));
    expect(onChange).not.toHaveBeenCalled();
  });
});
