Skip to content

AI media harness for planning, generating, and editing locally

TypeScript

Test the Fluent component library

Bitti6fa48fe78c3f
diff --git a/src/components/ui/ComboBox.test.tsx b/src/components/ui/ComboBox.test.tsx
new file mode 100644
index 0000000..eb06b02
--- /dev/null
+++ b/src/components/ui/ComboBox.test.tsx
@@ -0,0 +1,150 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { useState } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { ComboBox, optionsFromChildren, type ComboItems } from "./ComboBox";
+
+afterEach(cleanup);
+
+const fruit: ComboItems = [
+ { value: "apple", label: "Apple" },
+ { value: "banana", label: "Banana", disabled: true },
+ { value: "cherry", label: "Cherry", description: "Stone fruit" },
+ { label: "Citrus", options: [{ value: "lemon", label: "Lemon" }, { value: "lime", label: "Lime" }] },
+];
+
+function Controlled({ initial = "apple", onChange }: { initial?: string; onChange?: (value: string) => void }) {
+ const [value, setValue] = useState(initial);
+ return (
+ <>
+ <label htmlFor="fruit">Fruit</label>
+ <ComboBox id="fruit" value={value} onChange={(next) => { setValue(next); onChange?.(next); }} options={fruit} />
+ <button>After</button>
+ </>
+ );
+}
+
+const trigger = () => screen.getByRole("combobox", { name: "Fruit" });
+
+describe("ComboBox", () => {
+ it("shows the selected label on a labelled combobox trigger", () => {
+ render(<Controlled />);
+ expect(trigger()).toHaveTextContent("Apple");
+ expect(trigger()).toHaveAttribute("aria-expanded", "false");
+ expect(trigger()).toHaveAttribute("aria-haspopup", "listbox");
+ });
+
+ it("opens a portalled listbox with the selected item marked and picks with the mouse", () => {
+ const onChange = vi.fn();
+ render(<Controlled onChange={onChange} />);
+ fireEvent.click(trigger());
+ const list = screen.getByRole("listbox");
+ expect(list.parentElement).toBe(document.body);
+ expect(trigger()).toHaveAttribute("aria-expanded", "true");
+ expect(trigger()).toHaveAttribute("aria-controls", list.id);
+ expect(screen.getByRole("option", { name: "Apple" })).toHaveAttribute("aria-selected", "true");
+ expect(screen.getByRole("option", { name: "Banana" })).toHaveAttribute("aria-disabled", "true");
+ expect(screen.getByText("Citrus")).toBeInTheDocument();
+ fireEvent.click(screen.getByRole("option", { name: "Banana" }));
+ expect(onChange).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole("option", { name: /Cherry/ }));
+ expect(onChange).toHaveBeenCalledWith("cherry");
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ expect(trigger()).toHaveTextContent("Cherry");
+ expect(trigger()).toHaveFocus();
+ });
+
+ it("changes the value with arrows while closed, skipping disabled items, like a native select", () => {
+ render(<Controlled />);
+ fireEvent.keyDown(trigger(), { key: "ArrowDown" });
+ expect(trigger()).toHaveTextContent("Cherry");
+ fireEvent.keyDown(trigger(), { key: "End" });
+ expect(trigger()).toHaveTextContent("Lime");
+ fireEvent.keyDown(trigger(), { key: "ArrowDown" });
+ expect(trigger()).toHaveTextContent("Lime");
+ fireEvent.keyDown(trigger(), { key: "Home" });
+ expect(trigger()).toHaveTextContent("Apple");
+ fireEvent.keyDown(trigger(), { key: "l" });
+ expect(trigger()).toHaveTextContent("Lemon");
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ });
+
+ it("opens with Alt+Down, F4, Enter or Space and drives the list with aria-activedescendant", () => {
+ render(<Controlled />);
+ for (const init of [{ key: "ArrowDown", altKey: true }, { key: "F4" }, { key: "Enter" }, { key: " " }]) {
+ fireEvent.keyDown(trigger(), init);
+ expect(screen.getByRole("listbox")).toBeInTheDocument();
+ fireEvent.keyDown(trigger(), { key: "Escape" });
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ expect(trigger()).toHaveFocus();
+ }
+ fireEvent.keyDown(trigger(), { key: "Enter" });
+ const active = () => document.getElementById(trigger().getAttribute("aria-activedescendant")!);
+ expect(active()).toHaveTextContent("Apple");
+ fireEvent.keyDown(trigger(), { key: "ArrowDown" });
+ expect(active()).toHaveTextContent("Cherry");
+ fireEvent.keyDown(trigger(), { key: "End" });
+ expect(active()).toHaveTextContent("Lime");
+ fireEvent.keyDown(trigger(), { key: "l" });
+ expect(active()).toHaveTextContent("Lemon");
+ fireEvent.keyDown(trigger(), { key: "Enter" });
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ expect(trigger()).toHaveTextContent("Lemon");
+ });
+
+ it("closes without choosing on Tab and on a press outside", () => {
+ render(<Controlled />);
+ fireEvent.click(trigger());
+ fireEvent.keyDown(trigger(), { key: "ArrowDown" });
+ fireEvent.keyDown(trigger(), { key: "Tab" });
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ expect(trigger()).toHaveTextContent("Apple");
+ fireEvent.click(trigger());
+ fireEvent.mouseDown(screen.getByText("After"));
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ });
+
+ it("keeps Esc inside itself so a dialog around it stays open", () => {
+ const outer = vi.fn();
+ render(<div onKeyDown={outer}><Controlled /></div>);
+ fireEvent.click(trigger());
+ fireEvent.keyDown(trigger(), { key: "Escape" });
+ expect(outer).not.toHaveBeenCalled();
+ });
+
+ it("reads <option> and <optgroup> children, so a <select> migrates by renaming", () => {
+ const onChange = vi.fn();
+ render(
+ <ComboBox aria-label="Size" value="m" onChange={onChange}>
+ <option value="s">Small</option>
+ <option value="m">Medium</option>
+ <optgroup label="Big"><option value="l">Large</option><option value="xl" disabled>Huge</option></optgroup>
+ </ComboBox>,
+ );
+ const combo = screen.getByRole("combobox", { name: "Size" });
+ expect(combo).toHaveTextContent("Medium");
+ fireEvent.keyDown(combo, { key: "ArrowDown" });
+ expect(onChange).toHaveBeenCalledWith("l");
+ expect(optionsFromChildren(<><option value="a">A {"1"}</option></>)).toEqual([{ value: "a", label: ["A ", "1"], disabled: false, textValue: "A 1" }]);
+ });
+
+ it("shows a placeholder, and does nothing when disabled", () => {
+ const onChange = vi.fn();
+ const view = render(<ComboBox aria-label="Pick" value="" onChange={onChange} placeholder="Choose…" options={[{ value: "a", label: "A" }]} />);
+ expect(screen.getByRole("combobox")).toHaveTextContent("Choose…");
+ view.rerender(<ComboBox aria-label="Pick" value="" onChange={onChange} disabled options={[{ value: "a", label: "A" }]} />);
+ fireEvent.click(screen.getByRole("combobox"));
+ fireEvent.keyDown(screen.getByRole("combobox"), { key: "ArrowDown" });
+ expect(screen.queryByRole("listbox")).not.toBeInTheDocument();
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it("works with numeric values", () => {
+ const onChange = vi.fn();
+ render(<ComboBox<number> aria-label="Steps" value={20} onChange={onChange} options={[{ value: 10, label: "10" }, { value: 20, label: "20" }, { value: 30, label: "30" }]} />);
+ fireEvent.keyDown(screen.getByRole("combobox"), { key: "ArrowUp" });
+ expect(onChange).toHaveBeenCalledWith(10);
+ });
+});
diff --git a/src/components/ui/ContentDialog.test.tsx b/src/components/ui/ContentDialog.test.tsx
new file mode 100644
index 0000000..7f6dd64
--- /dev/null
+++ b/src/components/ui/ContentDialog.test.tsx
@@ -0,0 +1,114 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { useState } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { ContentDialog } from "./ContentDialog";
+
+afterEach(cleanup);
+
+function Harness(props: Partial<Parameters<typeof ContentDialog>[0]> & { body?: React.ReactNode }) {
+ const [open, setOpen] = useState(false);
+ return (
+ <>
+ <button onClick={() => setOpen(true)}>Open</button>
+ {open && (
+ <ContentDialog
+ title="Save changes?"
+ primaryText="Save"
+ secondaryText="Don't save"
+ closeText="Cancel"
+ onClose={() => setOpen(false)}
+ {...props}
+ >
+ {props.body ?? "Your edits will be lost."}
+ </ContentDialog>
+ )}
+ </>
+ );
+}
+
+describe("ContentDialog", () => {
+ it("is a labelled modal with buttons in the Windows order", () => {
+ render(<ContentDialog title="Delete project?" primaryText="Delete" closeText="Cancel" secondaryText="Archive" onClose={() => {}}>Gone for good.</ContentDialog>);
+ const dialog = screen.getByRole("dialog", { name: "Delete project?" });
+ expect(dialog).toHaveAttribute("aria-modal", "true");
+ expect(dialog).toHaveAccessibleDescription("Gone for good.");
+ expect(screen.getAllByRole("button").map((button) => button.textContent)).toEqual(["Delete", "Archive", "Cancel"]);
+ // Rendered into document.body, not inside the caller.
+ expect(dialog.closest(".ui-dialog-scrim")?.parentElement).toBe(document.body);
+ });
+
+ it("accent-styles the default button and runs it on Enter, but not from a textarea or a button", () => {
+ const onPrimary = vi.fn();
+ render(<ContentDialog title="Rename" primaryText="Rename" onPrimary={onPrimary} closeText="Cancel" onClose={() => {}} defaultButton="primary">
+ <input aria-label="Name" />
+ <textarea aria-label="Notes" />
+ </ContentDialog>);
+ expect(screen.getByRole("button", { name: "Rename" })).toHaveClass("primary-button");
+ expect(screen.getByRole("button", { name: "Cancel" })).toHaveClass("secondary-button");
+ fireEvent.keyDown(screen.getByLabelText("Notes"), { key: "Enter" });
+ fireEvent.keyDown(screen.getByRole("button", { name: "Cancel" }), { key: "Enter" });
+ expect(onPrimary).not.toHaveBeenCalled();
+ fireEvent.keyDown(screen.getByLabelText("Name"), { key: "Enter" });
+ expect(onPrimary).toHaveBeenCalledTimes(1);
+ });
+
+ it("does not run a disabled default button", () => {
+ const onPrimary = vi.fn();
+ render(<ContentDialog title="Go" primaryText="Go" onPrimary={onPrimary} primaryDisabled onClose={() => {}} defaultButton="primary"><input aria-label="x" /></ContentDialog>);
+ fireEvent.keyDown(screen.getByLabelText("x"), { key: "Enter" });
+ expect(onPrimary).not.toHaveBeenCalled();
+ expect(screen.getByRole("button", { name: "Go" })).toBeDisabled();
+ });
+
+ it("closes on Esc and the Close button but never on a click outside", () => {
+ render(<Harness />);
+ fireEvent.click(screen.getByText("Open"));
+ fireEvent.mouseDown(document.querySelector(".ui-dialog-scrim")!);
+ fireEvent.click(document.querySelector(".ui-dialog-scrim")!);
+ expect(screen.getByRole("dialog")).toBeInTheDocument();
+ fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" });
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ fireEvent.click(screen.getByText("Open"));
+ fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ });
+
+ it("focuses the first field in the body, traps Tab and restores focus on close", () => {
+ render(<Harness body={<input aria-label="Name" />} />);
+ const opener = screen.getByText("Open");
+ opener.focus();
+ fireEvent.click(opener);
+ const name = screen.getByLabelText("Name");
+ expect(name).toHaveFocus();
+ const cancel = screen.getByRole("button", { name: "Cancel" });
+ cancel.focus();
+ fireEvent.keyDown(cancel, { key: "Tab" });
+ expect(name).toHaveFocus();
+ fireEvent.keyDown(name, { key: "Tab", shiftKey: true });
+ expect(cancel).toHaveFocus();
+ fireEvent.keyDown(cancel, { key: "Escape" });
+ expect(opener).toHaveFocus();
+ });
+
+ it("falls back to the default button when the body has nothing to focus", () => {
+ render(<Harness defaultButton="secondary" />);
+ fireEvent.click(screen.getByText("Open"));
+ expect(screen.getByRole("button", { name: "Don't save" })).toHaveFocus();
+ });
+
+ it("pulls focus back when it escapes to the page behind", () => {
+ render(<Harness />);
+ fireEvent.click(screen.getByText("Open"));
+ const outside = screen.getByText("Open");
+ outside.focus();
+ expect(screen.getByRole("dialog").contains(document.activeElement)).toBe(true);
+ });
+
+ it("honours a wider max width", () => {
+ render(<ContentDialog title="Wide" onClose={() => {}} width={720} />);
+ expect(screen.getByRole("dialog").style.maxWidth).toContain("720px");
+ });
+});
diff --git a/src/components/ui/ContextMenu.test.tsx b/src/components/ui/ContextMenu.test.tsx
new file mode 100644
index 0000000..6bb84c6
--- /dev/null
+++ b/src/components/ui/ContextMenu.test.tsx
@@ -0,0 +1,132 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { useContextMenu, type MenuEntry } from "./ContextMenu";
+
+afterEach(cleanup);
+
+function Surface({ items, label = "Clip actions" }: { items: MenuEntry[]; label?: string }) {
+ const menu = useContextMenu();
+ return (
+ <>
+ <div
+ tabIndex={0}
+ data-testid="surface"
+ onContextMenu={(event) => menu.open(event, items, { "aria-label": label })}
+ onKeyDown={(event) => { if (event.key === "F10" && event.shiftKey) menu.open(event, items, { "aria-label": label }); }}
+ >
+ surface
+ </div>
+ <button>elsewhere</button>
+ {menu.element}
+ </>
+ );
+}
+
+const make = () => {
+ const rename = vi.fn();
+ const copy = vi.fn();
+ const remove = vi.fn();
+ const snap = vi.fn();
+ const items: MenuEntry[] = [
+ { id: "rename", label: "Rename", icon: <span>R</span>, shortcut: "F2", onSelect: rename },
+ { id: "copy", label: "Copy", shortcut: "Ctrl+C", onSelect: copy },
+ { id: "paste", label: "Paste", disabled: true, onSelect: vi.fn() },
+ { separator: true },
+ { id: "snap", label: "Snap to grid", checked: true, onSelect: snap },
+ { id: "delete", label: "Delete", shortcut: "Delete", danger: true, onSelect: remove },
+ ];
+ return { items, rename, copy, remove, snap };
+};
+
+describe("ContextMenu", () => {
+ it("opens at the pointer, labelled, with formatted accelerators, separators and checks", () => {
+ const { items } = make();
+ render(<Surface items={items} />);
+ fireEvent.contextMenu(screen.getByTestId("surface"), { clientX: 40, clientY: 50 });
+ const menu = screen.getByRole("menu", { name: "Clip actions" });
+ expect(menu.parentElement).toBe(document.body);
+ expect(screen.getByRole("menuitem", { name: /Copy/ })).toHaveTextContent("Ctrl+C");
+ expect(screen.getByRole("menuitem", { name: /Paste/ })).toBeDisabled();
+ expect(screen.getByRole("separator")).toBeInTheDocument();
+ expect(screen.getByRole("menuitemcheckbox", { name: /Snap/ })).toHaveAttribute("aria-checked", "true");
+ // Opened with the mouse: the menu holds focus, not the first item.
+ expect(menu).toHaveFocus();
+ });
+
+ it("focuses the first item when opened from the keyboard, and navigates skipping disabled items", () => {
+ const { items } = make();
+ render(<Surface items={items} />);
+ const surface = screen.getByTestId("surface");
+ surface.focus();
+ fireEvent.keyDown(surface, { key: "F10", shiftKey: true });
+ const rename = screen.getByRole("menuitem", { name: /Rename/ });
+ expect(rename).toHaveFocus();
+ fireEvent.keyDown(rename, { key: "ArrowDown" });
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowDown" });
+ expect(screen.getByRole("menuitemcheckbox", { name: /Snap/ })).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "End" });
+ expect(screen.getByRole("menuitem", { name: /Delete/ })).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowDown" });
+ expect(rename).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowUp" });
+ expect(screen.getByRole("menuitem", { name: /Delete/ })).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "Home" });
+ expect(rename).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "c" });
+ expect(screen.getByRole("menuitem", { name: /Copy/ })).toHaveFocus();
+ });
+
+ it("runs an item on click or Enter, closes, and returns focus to the opener", () => {
+ const { items, rename, copy } = make();
+ render(<Surface items={items} />);
+ const surface = screen.getByTestId("surface");
+ surface.focus();
+ fireEvent.keyDown(surface, { key: "F10", shiftKey: true });
+ fireEvent.keyDown(document.activeElement!, { key: "Enter" });
+ expect(rename).toHaveBeenCalledTimes(1);
+ expect(screen.queryByRole("menu")).not.toBeInTheDocument();
+ expect(surface).toHaveFocus();
+ fireEvent.contextMenu(surface, { clientX: 5, clientY: 5 });
+ fireEvent.click(screen.getByRole("menuitem", { name: /Copy/ }));
+ expect(copy).toHaveBeenCalledTimes(1);
+ });
+
+ it("honours an item's own shortcut while open", () => {
+ const { items, remove } = make();
+ render(<Surface items={items} />);
+ fireEvent.contextMenu(screen.getByTestId("surface"), { clientX: 5, clientY: 5 });
+ fireEvent.keyDown(screen.getByRole("menu"), { key: "Delete" });
+ expect(remove).toHaveBeenCalledTimes(1);
+ expect(screen.queryByRole("menu")).not.toBeInTheDocument();
+ });
+
+ it("closes on Esc (anywhere), Tab, and a press outside, without running anything", () => {
+ const { items, rename, copy, remove } = make();
+ render(<Surface items={items} />);
+ const surface = screen.getByTestId("surface");
+ fireEvent.contextMenu(surface, { clientX: 5, clientY: 5 });
+ fireEvent.keyDown(window, { key: "Escape" });
+ expect(screen.queryByRole("menu")).not.toBeInTheDocument();
+ fireEvent.contextMenu(surface, { clientX: 5, clientY: 5 });
+ fireEvent.keyDown(screen.getByRole("menu"), { key: "Tab" });
+ expect(screen.queryByRole("menu")).not.toBeInTheDocument();
+ fireEvent.contextMenu(surface, { clientX: 5, clientY: 5 });
+ fireEvent.pointerDown(screen.getByText("elsewhere"));
+ expect(screen.queryByRole("menu")).not.toBeInTheDocument();
+ expect(rename).not.toHaveBeenCalled();
+ expect(copy).not.toHaveBeenCalled();
+ expect(remove).not.toHaveBeenCalled();
+ });
+
+ it("does not let keys leak to the surface behind it", () => {
+ const { items } = make();
+ const behind = vi.fn();
+ render(<div onKeyDown={behind}><Surface items={items} /></div>);
+ fireEvent.contextMenu(screen.getByTestId("surface"), { clientX: 5, clientY: 5 });
+ fireEvent.keyDown(screen.getByRole("menu"), { key: "ArrowDown" });
+ expect(behind).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/components/ui/Controls.test.tsx b/src/components/ui/Controls.test.tsx
new file mode 100644
index 0000000..7572c4e
--- /dev/null
+++ b/src/components/ui/Controls.test.tsx
@@ -0,0 +1,246 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { useRef, useState } from "react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ Checkbox, Expander, InfoBadge, ProgressBar, ProgressRing, Radio, RadioGroup, SelectorBar, Slider, ToggleSwitch,
+} from "./Controls";
+import { CommandBar, CommandBarButton, CommandBarSeparator, NavItem, NavPane, PaneHeader, StatusBar, TextField } from "./Chrome";
+import { Flyout } from "./Flyout";
+import { InfoBar } from "./InfoBar";
+
+afterEach(cleanup);
+
+describe("ToggleSwitch", () => {
+ it("is a switch with On/Off text beside it and a visible label as its name", () => {
+ const onChange = vi.fn();
+ const view = render(<ToggleSwitch label="Autosave" checked={false} onChange={onChange} />);
+ const toggle = screen.getByRole("switch", { name: "Autosave" });
+ expect(toggle).toHaveAttribute("aria-checked", "false");
+ expect(screen.getByText("Off")).toBeInTheDocument();
+ fireEvent.click(toggle);
+ expect(onChange).toHaveBeenCalledWith(true);
+ view.rerender(<ToggleSwitch aria-label="Loop" checked onChange={onChange} stateText={false} />);
+ expect(screen.getByRole("switch", { name: "Loop" })).toHaveAttribute("aria-checked", "true");
+ expect(screen.queryByText("On")).not.toBeInTheDocument();
+ });
+
+ it("does nothing when disabled", () => {
+ const onChange = vi.fn();
+ render(<ToggleSwitch aria-label="x" checked={false} disabled onChange={onChange} />);
+ fireEvent.click(screen.getByRole("switch"));
+ expect(onChange).not.toHaveBeenCalled();
+ });
+});
+
+describe("Checkbox", () => {
+ it("wraps a native checkbox with a label and supports indeterminate", () => {
+ const onChange = vi.fn();
+ const view = render(<Checkbox label="Mute" description="Silence this track" checked={false} indeterminate onChange={onChange} />);
+ const box = screen.getByRole("checkbox", { name: /Mute/ }) as HTMLInputElement;
+ expect(box.indeterminate).toBe(true);
+ expect(box).toHaveAccessibleDescription("Silence this track");
+ fireEvent.click(box);
+ expect(onChange).toHaveBeenCalledWith(true, expect.anything());
+ view.rerender(<Checkbox label="Mute" checked onChange={onChange} />);
+ expect(box.indeterminate).toBe(false);
+ expect(box).toBeChecked();
+ });
+});
+
+describe("RadioGroup", () => {
+ it("is a labelled radiogroup from data or children", () => {
+ function Group() {
+ const [value, setValue] = useState<"fit" | "fill" | "stretch">("fit");
+ return (
+ <RadioGroup label="Scaling" value={value} onChange={setValue} options={[{ value: "fit", label: "Fit" }, { value: "fill", label: "Fill" }]}>
+ <Radio value="stretch" label="Stretch" disabled />
+ </RadioGroup>
+ );
+ }
+ render(<Group />);
+ expect(screen.getByRole("radiogroup", { name: "Scaling" })).toBeInTheDocument();
+ expect(screen.getByRole("radio", { name: "Fit" })).toBeChecked();
+ fireEvent.click(screen.getByRole("radio", { name: "Fill" }));
+ expect(screen.getByRole("radio", { name: "Fill" })).toBeChecked();
+ expect(screen.getByRole("radio", { name: "Stretch" })).toBeDisabled();
+ });
+});
+
+describe("Slider", () => {
+ it("sets --p from the value and reports numbers", () => {
+ const onChange = vi.fn();
+ render(<Slider aria-label="Volume" value={30} min={0} max={60} onChange={onChange} />);
+ const slider = screen.getByRole("slider", { name: "Volume" });
+ expect(slider.style.getPropertyValue("--p")).toBe("50%");
+ fireEvent.change(slider, { target: { value: "45" } });
+ expect(onChange).toHaveBeenCalledWith(45, expect.anything());
+ });
+});
+
+describe("Progress", () => {
+ it("reports determinate values and omits them when indeterminate", () => {
+ const view = render(<ProgressBar aria-label="Export" value={42.4} />);
+ const bar = screen.getByRole("progressbar", { name: "Export" });
+ expect(bar).toHaveAttribute("aria-valuenow", "42");
+ view.rerender(<ProgressBar aria-label="Export" state="paused" />);
+ expect(bar).not.toHaveAttribute("aria-valuenow");
+ expect(bar).toHaveClass("ui-progress--indeterminate", "ui-progress--paused");
+ view.rerender(<ProgressRing aria-label="Loading" size={32} />);
+ expect(screen.getByRole("progressbar", { name: "Loading" })).toHaveClass("ui-ring--indeterminate");
+ view.rerender(<ProgressRing aria-label="Loading" value={150} />);
+ expect(screen.getByRole("progressbar", { name: "Loading" })).toHaveAttribute("aria-valuenow", "100");
+ });
+});
+
+describe("InfoBadge", () => {
+ it("shows a capped number or a dot", () => {
+ const view = render(<InfoBadge value={120} aria-label="120 jobs" />);
+ expect(screen.getByRole("status", { name: "120 jobs" })).toHaveTextContent("99+");
+ view.rerender(<InfoBadge severity="critical" />);
+ expect(document.querySelector(".ui-badge--dot.ui-badge--critical")).toBeInTheDocument();
+ });
+});
+
+describe("Expander", () => {
+ it("toggles its content", () => {
+ render(<Expander header="Advanced">secret</Expander>);
+ const header = screen.getByRole("button", { name: "Advanced" });
+ expect(screen.getByText("secret")).not.toBeVisible();
+ fireEvent.click(header);
+ expect(header).toHaveAttribute("aria-expanded", "true");
+ expect(screen.getByText("secret")).toBeVisible();
+ });
+});
+
+describe("SelectorBar", () => {
+ it("is a tablist with one tab stop, arrow keys moving the selection", () => {
+ function Bar() {
+ const [value, setValue] = useState("all");
+ return <SelectorBar aria-label="Filter" value={value} onChange={setValue} items={[{ value: "all", label: "All" }, { value: "video", label: "Video" }, { value: "audio", label: "Audio", disabled: true }, { value: "image", label: "Image" }]} />;
+ }
+ render(<Bar />);
+ const all = screen.getByRole("tab", { name: "All" });
+ expect(screen.getByRole("tablist", { name: "Filter" })).toBeInTheDocument();
+ expect(all).toHaveAttribute("aria-selected", "true");
+ expect(all).toHaveAttribute("tabindex", "0");
+ expect(screen.getByRole("tab", { name: "Video" })).toHaveAttribute("tabindex", "-1");
+ fireEvent.keyDown(all, { key: "ArrowRight" });
+ expect(screen.getByRole("tab", { name: "Video" })).toHaveAttribute("aria-selected", "true");
+ expect(screen.getByRole("tab", { name: "Video" })).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowRight" });
+ expect(screen.getByRole("tab", { name: "Image" })).toHaveAttribute("aria-selected", "true");
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowRight" });
+ expect(all).toHaveAttribute("aria-selected", "true");
+ fireEvent.keyDown(document.activeElement!, { key: "End" });
+ expect(screen.getByRole("tab", { name: "Image" })).toHaveAttribute("aria-selected", "true");
+ });
+});
+
+describe("CommandBar", () => {
+ it("renders subtle buttons with tooltip data and arrow navigation", () => {
+ const save = vi.fn();
+ render(
+ <CommandBar aria-label="Editor" end={<span>end</span>}>
+ <CommandBarButton icon={<i />} label="Save" shortcut="Ctrl+S" onClick={save} />
+ <CommandBarSeparator />
+ <CommandBarButton icon={<i />} label="Snap" pressed />
+ <CommandBarButton label="Export" />
+ </CommandBar>,
+ );
+ expect(screen.getByRole("toolbar", { name: "Editor" })).toBeInTheDocument();
+ const saveButton = screen.getByRole("button", { name: "Save" });
+ expect(saveButton).toHaveAttribute("data-tooltip", "Save");
+ expect(saveButton).toHaveAttribute("data-tooltip-shortcut", "Ctrl+S");
+ expect(saveButton).toHaveAttribute("aria-keyshortcuts", "Control+S");
+ expect(screen.getByRole("button", { name: "Snap" })).toHaveAttribute("aria-pressed", "true");
+ expect(screen.getByRole("button", { name: "Export" })).toHaveTextContent("Export");
+ fireEvent.click(saveButton);
+ expect(save).toHaveBeenCalled();
+ saveButton.focus();
+ fireEvent.keyDown(saveButton, { key: "ArrowRight" });
+ expect(screen.getByRole("button", { name: "Snap" })).toHaveFocus();
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowLeft" });
+ fireEvent.keyDown(document.activeElement!, { key: "ArrowLeft" });
+ expect(screen.getByRole("button", { name: "Export" })).toHaveFocus();
+ });
+});
+
+describe("chrome pieces", () => {
+ it("renders a pane header, nav items with a badge, a text field and a status bar", () => {
+ const onChange = vi.fn();
+ render(
+ <>
+ <PaneHeader title="Inspector" id="inspector-title" actions={<button>More</button>} />
+ <NavPane aria-label="Main" footer={<NavItem label="Settings" />}>
+ <NavItem label="Library" selected badge={3} badgeLabel="3 running" />
+ <NavItem label="Queue" badge />
+ </NavPane>
+ <TextField aria-label="Search" value="" onChange={onChange} icon={<i />} trailing={<button className="ui-textfield__button">x</button>} />
+ <StatusBar aria-label="Status" end={<span>100%</span>}><span>Ready</span></StatusBar>
+ </>,
+ );
+ expect(screen.getByRole("heading", { name: "Inspector" })).toHaveAttribute("id", "inspector-title");
+ expect(screen.getByRole("navigation", { name: "Main" })).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: /Library/ })).toHaveAttribute("aria-current", "page");
+ expect(screen.getByRole("status", { name: "3 running" })).toHaveTextContent("3");
+ expect(screen.getByRole("button", { name: "Queue" })).not.toHaveAttribute("aria-current");
+ fireEvent.change(screen.getByRole("textbox", { name: "Search" }), { target: { value: "cat" } });
+ expect(onChange).toHaveBeenCalledWith("cat", expect.anything());
+ expect(screen.getByRole("region", { name: "Status" })).toHaveTextContent("Ready100%");
+ });
+});
+
+describe("InfoBar", () => {
+ it("is a status for information and an alert for errors, with an optional close", () => {
+ const onClose = vi.fn();
+ const view = render(<InfoBar severity="success" title="Saved" message="out.mp4" action={<button>Open folder</button>} onClose={onClose} />);
+ expect(screen.getByRole("status")).toHaveTextContent("Savedout.mp4");
+ fireEvent.click(screen.getByRole("button", { name: "Close" }));
+ expect(onClose).toHaveBeenCalled();
+ view.rerender(<InfoBar severity="error" title="Export failed">Disk full</InfoBar>);
+ expect(screen.getByRole("alert")).toHaveTextContent("Export failedDisk full");
+ expect(screen.queryByRole("button", { name: "Close" })).not.toBeInTheDocument();
+ });
+});
+
+describe("Flyout", () => {
+ function Anchored() {
+ const button = useRef<HTMLButtonElement>(null);
+ const [open, setOpen] = useState(false);
+ return (
+ <>
+ <button ref={button} aria-expanded={open} onClick={() => setOpen(!open)}>Queue</button>
+ <button>Elsewhere</button>
+ <Flyout open={open} anchor={button} onClose={() => setOpen(false)} aria-label="Work queue">
+ <button>First</button>
+ </Flyout>
+ </>
+ );
+ }
+
+ it("opens anchored, focuses inside, and light-dismisses", () => {
+ render(<Anchored />);
+ const anchor = screen.getByRole("button", { name: "Queue" });
+ fireEvent.click(anchor);
+ const flyout = screen.getByRole("dialog", { name: "Work queue" });
+ expect(flyout.parentElement).toBe(document.body);
+ expect(screen.getByRole("button", { name: "First" })).toHaveFocus();
+ // Pressing the anchor itself is not "outside": its own click toggles.
+ fireEvent.mouseDown(anchor);
+ expect(screen.getByRole("dialog")).toBeInTheDocument();
+ fireEvent.click(anchor);
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+
+ fireEvent.click(anchor);
+ fireEvent.mouseDown(screen.getByRole("button", { name: "Elsewhere" }));
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+
+ fireEvent.click(anchor);
+ fireEvent.keyDown(window, { key: "Escape" });
+ expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
+ expect(anchor).toHaveFocus();
+ });
+});
diff --git a/src/components/ui/Layout.test.tsx b/src/components/ui/Layout.test.tsx
new file mode 100644
index 0000000..b95a915
--- /dev/null
+++ b/src/components/ui/Layout.test.tsx
@@ -0,0 +1,206 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { useState } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { PropRow, PropSection } from "./PropRow";
+import { SettingsCard, SettingsExpander, SettingsGroup, SettingsRow } from "./Settings";
+import { Splitter, usePaneSize } from "./Splitter";
+import { placeAnchored, placeAtPoint } from "./internal";
+
+/* jsdom has no PointerEvent, and without one testing-library drops clientX
+ and pointerId from pointer events. A MouseEvent carries both. */
+if (typeof window.PointerEvent === "undefined") {
+ class PointerEventShim extends MouseEvent {
+ pointerId: number;
+ constructor(type: string, init: PointerEventInit = {}) {
+ super(type, init);
+ this.pointerId = init.pointerId ?? 1;
+ }
+ }
+ (window as unknown as { PointerEvent: unknown }).PointerEvent = PointerEventShim;
+}
+
+beforeEach(() => localStorage.clear());
+afterEach(cleanup);
+
+function Panes({ reverse = false }: { reverse?: boolean }) {
+ const pane = usePaneSize("test.inspector", 300, { min: 200, max: 400 });
+ return (
+ <div data-testid="grid" style={pane.style}>
+ <Splitter {...pane.splitterProps} reverse={reverse} aria-label="Resize inspector" />
+ </div>
+ );
+}
+
+describe("Splitter + usePaneSize", () => {
+ it("exposes the size as a CSS variable and a separator value", () => {
+ render(<Panes />);
+ expect(screen.getByTestId("grid").style.getPropertyValue("--pane-test-inspector")).toBe("300px");
+ const separator = screen.getByRole("separator", { name: "Resize inspector" });
+ expect(separator).toHaveAttribute("aria-valuenow", "300");
+ expect(separator).toHaveAttribute("aria-valuemin", "200");
+ expect(separator).toHaveAttribute("aria-valuemax", "400");
+ expect(separator).toHaveAttribute("aria-orientation", "vertical");
+ });
+
+ it("steps with the arrow keys, clamps, persists and resets on double-click", () => {
+ const view = render(<Panes />);
+ const separator = screen.getByRole("separator");
+ fireEvent.keyDown(separator, { key: "ArrowRight" });
+ expect(separator).toHaveAttribute("aria-valuenow", "308");
+ fireEvent.keyDown(separator, { key: "ArrowLeft", shiftKey: true });
+ expect(separator).toHaveAttribute("aria-valuenow", "276");
+ fireEvent.keyDown(separator, { key: "End" });
+ expect(separator).toHaveAttribute("aria-valuenow", "400");
+ fireEvent.keyDown(separator, { key: "ArrowRight" });
+ expect(separator).toHaveAttribute("aria-valuenow", "400");
+ expect(localStorage.getItem("slopus.pane.test.inspector")).toBe("400");
+ view.unmount();
+ render(<Panes />);
+ expect(screen.getByRole("separator")).toHaveAttribute("aria-valuenow", "400");
+ fireEvent.doubleClick(screen.getByRole("separator"));
+ expect(screen.getByRole("separator")).toHaveAttribute("aria-valuenow", "300");
+ });
+
+ it("drags with the pointer, inverted for a pane after the splitter", () => {
+ render(<Panes reverse />);
+ const separator = screen.getByRole("separator");
+ fireEvent.pointerDown(separator, { button: 0, clientX: 500, pointerId: 1 });
+ fireEvent.pointerMove(separator, { clientX: 450, pointerId: 1 });
+ expect(separator).toHaveAttribute("aria-valuenow", "350");
+ expect(separator).toHaveClass("ui-splitter--dragging");
+ fireEvent.pointerUp(separator, { pointerId: 1 });
+ fireEvent.pointerMove(separator, { clientX: 300, pointerId: 1 });
+ expect(separator).toHaveAttribute("aria-valuenow", "350");
+ // Reverse: ArrowLeft grows the pane on the right.
+ fireEvent.keyDown(separator, { key: "ArrowLeft" });
+ expect(separator).toHaveAttribute("aria-valuenow", "358");
+ });
+
+ it("survives unusable storage", () => {
+ const get = vi.spyOn(Storage.prototype, "getItem").mockImplementation(() => { throw new Error("denied"); });
+ const set = vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => { throw new Error("denied"); });
+ render(<Panes />);
+ fireEvent.keyDown(screen.getByRole("separator"), { key: "ArrowRight" });
+ expect(screen.getByRole("separator")).toHaveAttribute("aria-valuenow", "308");
+ get.mockRestore();
+ set.mockRestore();
+ });
+});
+
+describe("SettingsCard family", () => {
+ it("lays out a labelled card with a control, a clickable card and a link card", () => {
+ const open = vi.fn();
+ render(
+ <SettingsGroup heading="Appearance">
+ <SettingsCard icon={<span>i</span>} header="Theme" description="Follow Windows"><button>Pick</button></SettingsCard>
+ <SettingsCard header="About" description="Version 1" onClick={open} />
+ <SettingsCard header="Docs" href="https://example.com" />
+ </SettingsGroup>,
+ );
+ expect(screen.getByRole("region", { name: "Appearance" })).toBeInTheDocument();
+ const card = screen.getByRole("group", { name: "Theme" });
+ expect(card).toHaveAccessibleDescription("Follow Windows");
+ expect(card).toContainElement(screen.getByRole("button", { name: "Pick" }));
+ fireEvent.click(screen.getByRole("button", { name: /About/ }));
+ expect(open).toHaveBeenCalled();
+ expect(screen.getByRole("link", { name: /Docs/ })).toHaveAttribute("href", "https://example.com");
+ });
+
+ it("expands, keeps its header control independent, and supports control", () => {
+ const toggled = vi.fn();
+ render(
+ <SettingsExpander header="GPU" description="Device options" control={<button onClick={toggled}>Enable</button>}>
+ <SettingsRow header="Device">cuda:0</SettingsRow>
+ <SettingsRow header="Memory">8 GB</SettingsRow>
+ </SettingsExpander>,
+ );
+ const header = screen.getByRole("button", { name: "GPU" });
+ expect(header).toHaveAttribute("aria-expanded", "false");
+ expect(screen.queryByText("cuda:0")).not.toBeVisible();
+ fireEvent.click(screen.getByRole("button", { name: "Enable" }));
+ expect(toggled).toHaveBeenCalled();
+ expect(header).toHaveAttribute("aria-expanded", "false");
+ fireEvent.click(header);
+ expect(header).toHaveAttribute("aria-expanded", "true");
+ expect(screen.getByText("cuda:0")).toBeVisible();
+ expect(screen.getByRole("group", { name: "Memory" })).toBeInTheDocument();
+ });
+
+ it("can be controlled", () => {
+ function Controlled() {
+ const [open, setOpen] = useState(true);
+ return <><span>{open ? "open" : "shut"}</span><SettingsExpander header="X" expanded={open} onExpandedChange={setOpen}>body</SettingsExpander></>;
+ }
+ render(<Controlled />);
+ fireEvent.click(screen.getByRole("button", { name: "X" }));
+ expect(screen.getByText("shut")).toBeInTheDocument();
+ });
+});
+
+describe("PropRow + PropSection", () => {
+ function Opacity() {
+ const [value, setValue] = useState(100);
+ return (
+ <PropSection title="Transform" persistKey="test.transform">
+ <PropRow label="Opacity" htmlFor="opacity" value={value} defaultValue={100} onReset={() => setValue(100)} scrub={{ value, onChange: setValue, min: 0, max: 100 }}>
+ <input id="opacity" type="number" value={value} onChange={(event) => setValue(Number(event.target.value))} />
+ </PropRow>
+ </PropSection>
+ );
+ }
+
+ it("labels the field, shows reset only when modified, and scrubs from the label", () => {
+ render(<Opacity />);
+ const field = screen.getByLabelText("Opacity");
+ expect(screen.queryByRole("button", { name: "Reset Opacity" })).not.toBeInTheDocument();
+ const label = screen.getByText("Opacity");
+ expect(label).toHaveClass("ui-prop-row__label--scrub");
+ fireEvent.pointerDown(label, { button: 0, clientX: 100, pointerId: 1 });
+ fireEvent.pointerMove(label, { clientX: 98, pointerId: 1 }); // under the drag threshold
+ expect(field).toHaveValue(100);
+ fireEvent.pointerMove(label, { clientX: 80, pointerId: 1 });
+ expect(field).toHaveValue(80);
+ fireEvent.pointerMove(label, { clientX: 79, pointerId: 1, shiftKey: true });
+ expect(field).toHaveValue(70);
+ fireEvent.pointerMove(label, { clientX: -500, pointerId: 1 });
+ expect(field).toHaveValue(0);
+ fireEvent.pointerUp(label, { pointerId: 1 });
+ fireEvent.click(screen.getByRole("button", { name: "Reset Opacity" }));
+ expect(field).toHaveValue(100);
+ expect(screen.queryByRole("button", { name: "Reset Opacity" })).not.toBeInTheDocument();
+ });
+
+ it("collapses its section and remembers it", () => {
+ const view = render(<Opacity />);
+ const toggle = screen.getByRole("button", { name: "Transform" });
+ expect(toggle).toHaveAttribute("aria-expanded", "true");
+ fireEvent.click(toggle);
+ expect(toggle).toHaveAttribute("aria-expanded", "false");
+ expect(localStorage.getItem("slopus.section.test.transform")).toBe("0");
+ view.unmount();
+ render(<Opacity />);
+ expect(screen.getByRole("button", { name: "Transform" })).toHaveAttribute("aria-expanded", "false");
+ });
+});
+
+describe("placement", () => {
+ const view = { width: 800, height: 600 };
+ it("opens a menu down-right of the point and flips near the edges", () => {
+ expect(placeAtPoint(100, 100, { width: 200, height: 300 }, view)).toEqual({ left: 100, top: 100 });
+ expect(placeAtPoint(700, 500, { width: 200, height: 300 }, view)).toEqual({ left: 500, top: 200 });
+ // Too big to flip either way: clamped inside the margin.
+ expect(placeAtPoint(10, 10, { width: 900, height: 100 }, view).left).toBe(8);
+ });
+
+ it("anchors below, flips above, and aligns the end", () => {
+ const anchor = { left: 100, top: 100, right: 200, bottom: 132, width: 100, height: 32 };
+ expect(placeAnchored(anchor, { width: 150, height: 100 }, "bottom", 4, view)).toEqual({ left: 100, top: 136, placement: "bottom" });
+ const low = { ...anchor, top: 550, bottom: 582 };
+ expect(placeAnchored(low, { width: 150, height: 100 }, "bottom", 4, view)).toMatchObject({ top: 446, placement: "top" });
+ expect(placeAnchored(anchor, { width: 150, height: 100 }, "bottom-end", 4, view)).toMatchObject({ left: 50, placement: "bottom-end" });
+ expect(placeAnchored(anchor, { width: 150, height: 100 }, "right", 4, view)).toMatchObject({ left: 204, top: 100, placement: "right" });
+ });
+});
diff --git a/src/components/ui/Tooltip.test.tsx b/src/components/ui/Tooltip.test.tsx
new file mode 100644
index 0000000..8d67865
--- /dev/null
+++ b/src/components/ui/Tooltip.test.tsx
@@ -0,0 +1,109 @@
+// @vitest-environment jsdom
+
+import "@testing-library/jest-dom/vitest";
+import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { FluentRuntime, TOOLTIP_BETWEEN_DELAY, TOOLTIP_DELAY, TooltipLayer, tooltipProps } from "./Tooltip";
+
+beforeEach(() => { vi.useFakeTimers(); });
+afterEach(() => { cleanup(); vi.useRealTimers(); });
+
+const advance = (ms: number) => act(() => { vi.advanceTimersByTime(ms); });
+
+function Page() {
+ return (
+ <>
+ <TooltipLayer />
+ <button aria-label="Split" {...tooltipProps("Split clip", "s")}>A</button>
+ <button aria-label="Trim" {...tooltipProps("Trim")}>B</button>
+ <button>plain</button>
+ </>
+ );
+}
+
+describe("tooltipProps", () => {
+ it("spells the data attributes, formatting the shortcut", () => {
+ expect(tooltipProps("Save", "ctrl+s")).toEqual({ "data-tooltip": "Save", "data-tooltip-shortcut": "Ctrl+S" });
+ expect(tooltipProps("Save")).toEqual({ "data-tooltip": "Save" });
+ expect(tooltipProps(undefined)).toEqual({});
+ });
+});
+
+describe("TooltipLayer", () => {
+ it("shows after the hover delay with the shortcut in its own span", () => {
+ render(<Page />);
+ fireEvent.mouseOver(screen.getByText("A"), { clientX: 10, clientY: 10 });
+ advance(TOOLTIP_DELAY - 10);
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ advance(20);
+ const tip = screen.getByRole("tooltip");
+ expect(tip).toHaveTextContent("Split clipS");
+ expect(tip.querySelector(".ui-tooltip__shortcut")).toHaveTextContent("S");
+ expect(tip.parentElement).toBe(document.body);
+ // The element's name already differs from the tooltip text, so it is described by it.
+ expect(screen.getByText("A")).toHaveAttribute("aria-describedby", tip.id);
+ });
+
+ it("moves between tooltips quickly and hides on leave, press, Esc", () => {
+ render(<Page />);
+ fireEvent.mouseOver(screen.getByText("A"));
+ advance(TOOLTIP_DELAY);
+ expect(screen.getByRole("tooltip")).toHaveTextContent("Split clip");
+ fireEvent.mouseOut(screen.getByText("A"), { relatedTarget: screen.getByText("B") });
+ fireEvent.mouseOver(screen.getByText("B"));
+ advance(TOOLTIP_BETWEEN_DELAY);
+ expect(screen.getByRole("tooltip")).toHaveTextContent("Trim");
+
+ fireEvent.mouseDown(screen.getByText("B"));
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+
+ fireEvent.mouseOver(screen.getByText("plain"));
+ fireEvent.mouseOver(screen.getByText("A"));
+ advance(TOOLTIP_DELAY);
+ fireEvent.keyDown(window, { key: "Escape" });
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+
+ advance(1000);
+ fireEvent.mouseOver(screen.getByText("B"));
+ advance(TOOLTIP_DELAY);
+ fireEvent.mouseOut(screen.getByText("B"), { relatedTarget: document.body });
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ });
+
+ it("does not show if the pointer leaves before the delay", () => {
+ render(<Page />);
+ fireEvent.mouseOver(screen.getByText("A"));
+ advance(200);
+ fireEvent.mouseOut(screen.getByText("A"), { relatedTarget: screen.getByText("plain") });
+ fireEvent.mouseOver(screen.getByText("plain"));
+ advance(TOOLTIP_DELAY);
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ });
+
+ it("shows on keyboard focus and hides on blur", () => {
+ render(<Page />);
+ fireEvent.keyDown(document.body, { key: "Tab" });
+ const a = screen.getByText("A");
+ act(() => a.focus());
+ advance(TOOLTIP_DELAY);
+ expect(screen.getByRole("tooltip")).toHaveTextContent("Split clip");
+ act(() => a.blur());
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ });
+
+ it("does not describe an element whose name already is the tooltip", () => {
+ render(<><TooltipLayer /><button aria-label="Trim" data-tooltip="Trim">x</button></>);
+ fireEvent.mouseOver(screen.getByText("x"));
+ advance(TOOLTIP_DELAY);
+ expect(screen.getByRole("tooltip")).toBeInTheDocument();
+ expect(screen.getByText("x")).not.toHaveAttribute("aria-describedby");
+ });
+});
+
+describe("FluentRuntime", () => {
+ it("mounts the tooltip layer and fills every slider", () => {
+ render(<><FluentRuntime /><input type="range" aria-label="Volume" defaultValue="25" /></>);
+ expect(screen.getByLabelText("Volume").style.getPropertyValue("--p")).toBe("25%");
+ fireEvent.mouseOver(screen.getByLabelText("Volume"));
+ });
+});
diff --git a/src/components/ui/internal.ts b/src/components/ui/internal.ts
index 34da010..57f7b8c 100644
--- a/src/components/ui/internal.ts
+++ b/src/components/ui/internal.ts
@@ -82,11 +82,9 @@ export function trackModality() {
const pointer = () => { lastKeyboard = false; };
document.addEventListener("pointerdown", pointer, true);
document.addEventListener("mousedown", pointer, true);
- document.addEventListener("contextmenu", (event) => {
- // A contextmenu event from the keyboard arrives with no pointer position.
- if ((event as MouseEvent).button === 0 && (event as MouseEvent).clientX === 0 && (event as MouseEvent).clientY === 0 && lastKeyboard) return;
- lastKeyboard = false;
- }, true);
+ // No contextmenu listener: a right-click always presses the mouse first,
+ // while Shift+F10 / the Menu key leave the keyboard flag set, so a menu
+ // opened from a contextmenu event already knows which one it was.
}
trackModality();