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

const OPTIONS = [
	{ value: "any", label: "All" },
	{ value: "blocked", label: "Blocked only" },
];

/** RAC opens a Select from the keyboard as readily as from a pointer. */
function open(trigger: HTMLElement) {
	fireEvent.keyDown(trigger, { key: "Enter" });
	fireEvent.keyUp(trigger, { key: "Enter" });
}

test("a disabled select keeps its value on screen but takes no input", () => {
	const onChange = vi.fn();
	render(<Select label="Status" options={OPTIONS} value="blocked" onChange={onChange} isDisabled />);

	const trigger = screen.getByRole("button");
	expect(trigger.textContent).toContain("Blocked only");
	// A disabled button is out of the tab order by definition, so the filter row
	// cannot be reached by keyboard while live mode owns it.
	expect(trigger).toHaveProperty("disabled", true);

	open(trigger);
	expect(screen.queryByRole("listbox")).toBeNull();
	expect(onChange).not.toHaveBeenCalled();
});

test("an enabled select still opens", () => {
	render(<Select label="Status" options={OPTIONS} value="any" onChange={vi.fn()} />);
	open(screen.getByRole("button"));
	expect(screen.getByRole("listbox")).toBeTruthy();
});
