import { describe, expect, it } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { axe } from "vitest-axe";
import { Popover } from "./Popover";

describe("Popover", () => {
  it("trigger ouvre + escape ferme", async () => {
    const user = userEvent.setup();
    render(
      <Popover trigger={<button type="button">Open</button>}>
        <p>Contenu</p>
      </Popover>,
    );
    await user.click(screen.getByText("Open"));
    expect(await screen.findByText("Contenu")).toBeInTheDocument();
    await user.keyboard("{Escape}");
    await new Promise((r) => setTimeout(r, 0));
    expect(screen.queryByText("Contenu")).not.toBeInTheDocument();
  });

  it("a11y axe-core (fermé)", async () => {
    const { container } = render(
      <Popover trigger={<button type="button">Open</button>}>
        <p>Contenu</p>
      </Popover>,
    );
    expect(await axe(container)).toHaveNoViolations();
  });
});
