// @vitest-environment jsdom import { flushSync } from "react-dom"; import { createRoot } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { PriorityIcon } from "./PriorityIcon"; // eslint-disable-next-line @typescript-eslint/no-explicit-any (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; async function act(callback: () => void | Promise) { let result: void | Promise = undefined; flushSync(() => { result = callback(); }); await result; } describe("PriorityIcon picker", () => { let container: HTMLDivElement; let root: ReturnType | null; beforeEach(() => { container = document.createElement("div"); document.body.appendChild(container); root = createRoot(container); }); afterEach(async () => { await act(async () => root?.unmount()); container.remove(); document.body.innerHTML = ""; }); it("opens the real popover and selects a priority", async () => { const onChange = vi.fn(); await act(async () => root?.render()); const trigger = container.querySelector( 'button[aria-label="Change priority (current: Medium)"]', ); expect(trigger).not.toBeNull(); await act(async () => { trigger?.dispatchEvent(new MouseEvent("click", { bubbles: true })); }); const highOption = [...document.body.querySelectorAll("button")] .find((button) => button.textContent?.trim() === "High"); expect(highOption).not.toBeUndefined(); await act(async () => { highOption?.dispatchEvent(new MouseEvent("click", { bubbles: true })); }); expect(onChange).toHaveBeenCalledOnce(); expect(onChange).toHaveBeenCalledWith("high"); }); });