/**
* Characterization ("golden") tests — freeze the observable API surface of
* Popover. Added to guard the swap from @floating-ui/react to a local
* implementation; retained as a long-lived contract test.
*
* Pixel math (flip/shift/offset values) is NOT asserted here — jsdom has no
* layout. Those are verified manually in Storybook.
*/
import { fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Popover } from ".";
describe("Popover — characterization (API contract)", () => {
describe("trigger element", () => {
it("renders the trigger as the cloned child with merged class names", () => {
render(
Toggle
}
>
Body
,
);
const trigger = screen.getByText("Toggle");
expect(trigger.tagName).toBe("BUTTON");
expect(trigger).toHaveClass("my-trigger", "mobius-popover__toggle");
});
it("does not wrap the trigger in an extra element", () => {
const { container } = render(
Toggle}>Body,
);
expect(container.firstElementChild?.tagName).toBe("BUTTON");
});
});
describe("floating container (when open)", () => {
const open = () => {
render(
Toggle}>Body,
);
fireEvent.click(screen.getByText("Toggle"));
return document.querySelector(
".mobius-popover__container",
) as HTMLDivElement;
};
it("is not in the DOM when closed", () => {
render(
Toggle}>Body,
);
expect(document.querySelector(".mobius-popover__container")).toBeNull();
});
it("is portalled to document.body when open", () => {
const floating = open();
expect(floating).toHaveClass("mobius", "mobius-popover__container");
expect(floating.parentElement).toBe(document.body);
});
it("uses a viewport-anchored positioning strategy", () => {
// Local implementation uses position: absolute with document-relative
// top/left (viewport rect + window.scrollY/X). Because the popover is
// portalled to document.body, the initial containing block is the
// document, so absolute coords pin it to the trigger and scroll
// natively with the page.
const floating = open();
expect(["fixed", "absolute"]).toContain(floating.style.position);
});
it("has inline top/left styles applied", () => {
const floating = open();
expect(floating).not.toHaveStyle({ top: "" });
expect(floating).not.toHaveStyle({ left: "" });
});
});
describe("arrow element", () => {
it("renders an SVG with the arrow class and aria-hidden", () => {
render(
Toggle}>Body,
);
fireEvent.click(screen.getByText("Toggle"));
const arrow = document.querySelector("svg.mobius-popover__arrow-icon");
expect(arrow).not.toBeNull();
expect(arrow).toHaveAttribute("aria-hidden", "true");
expect(arrow).toHaveStyle({ position: "absolute" });
});
});
});
describe("Popover — characterization (dismiss semantics)", () => {
it("closes and fires onClose when pressing outside via pointerdown", () => {
const onClose = vi.fn();
render(