import { fireEvent, render, screen } from "@solidjs/testing-library";
import { describe, expect, it } from "vitest";
import { Tooltip } from "../../../src/components/tooltip/index";

describe("Tooltip", () => {
  it("content has role=tooltip when open", () => {
    render(() => (
      <Tooltip defaultOpen>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content>Help text</Tooltip.Content>
      </Tooltip>
    ));
    expect(screen.getByRole("tooltip")).toBeTruthy();
  });
  it("trigger has aria-describedby when open", () => {
    render(() => (
      <Tooltip defaultOpen>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content>Help text</Tooltip.Content>
      </Tooltip>
    ));
    const trigger = screen.getByText("Hover me");
    const tooltip = screen.getByRole("tooltip");
    expect(trigger.getAttribute("aria-describedby")).toBe(tooltip.id);
  });
  it("content not rendered when closed", () => {
    render(() => (
      <Tooltip>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content>Help text</Tooltip.Content>
      </Tooltip>
    ));
    expect(screen.queryByRole("tooltip")).toBeNull();
  });
  it("closes on Escape", () => {
    render(() => (
      <Tooltip defaultOpen>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content>Help text</Tooltip.Content>
      </Tooltip>
    ));
    fireEvent.keyDown(document, { key: "Escape" });
    expect(screen.queryByRole("tooltip")).toBeNull();
  });
  it("controlled mode", () => {
    render(() => (
      <Tooltip open={true} onOpenChange={() => {}}>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content>Help text</Tooltip.Content>
      </Tooltip>
    ));
    expect(screen.getByRole("tooltip")).toBeTruthy();
  });
  it("content is positioned", () => {
    render(() => (
      <Tooltip defaultOpen>
        <Tooltip.Trigger>Hover me</Tooltip.Trigger>
        <Tooltip.Content data-testid="content">Help text</Tooltip.Content>
      </Tooltip>
    ));
    const content = screen.getByTestId("content");
    expect(content.style.position).toBeTruthy();
  });
});
