/** @jsxImportSource react */ import { expect, test } from "bun:test"; import { act, useState } from "react"; import { WebCheckbox, WebSegmentedControl } from "./controls"; import { WebListView } from "./list-view"; import { Button } from "../../../../components/ui/button"; import { TextField } from "../../../../components/ui/fields"; import { ActionRow } from "../../../../components/ui/action-row"; import { Box, Text } from "../../../../ui"; import { createDomTestHarness } from "../test-utils"; const { render: renderDom } = createDomTestHarness(); test("desktop checkbox keeps browser state aligned with controlled selection on input and label clicks", async () => { const changes: boolean[] = []; const parentEvents: string[] = []; function Selection() { const [selected, setSelected] = useState(false); return
parentEvents.push("click")} onMouseDown={() => parentEvents.push("mousedown")} onKeyDown={() => parentEvents.push("keydown")}> { changes.push(value); setSelected(value); }} /> {selected ? "Study enabled" : "Study disabled"} parentEvents.push("disabled")} />
; } const container = await renderDom(); const [input, disabled] = [...container.querySelectorAll("input")]; const [label, disabledLabel] = [...container.querySelectorAll("label")]; const mouseDown = new MouseEvent("mousedown", { bubbles: true, cancelable: true }); const space = new KeyboardEvent("keydown", { key: " ", bubbles: true, cancelable: true }); await act(async () => { input!.dispatchEvent(mouseDown); input!.dispatchEvent(space); }); // Browser focus and keyboard activation must remain enabled; happy-dom does // not synthesize a native Space click, so the actual click is tested below. expect(mouseDown.defaultPrevented).toBe(false); expect(space.defaultPrevented).toBe(false); await act(async () => { input!.click(); }); expect(container.querySelector("output")!.textContent).toBe("Study enabled"); expect(input!.checked).toBe(true); await act(async () => { label!.click(); }); expect(container.querySelector("output")!.textContent).toBe("Study disabled"); expect(input!.checked).toBe(false); await act(async () => { label!.click(); }); expect(input!.checked).toBe(true); await act(async () => { input!.click(); }); expect(input!.checked).toBe(false); await act(async () => { disabled!.click(); disabledLabel!.click(); }); expect(changes).toEqual([true, false, true, false]); expect(parentEvents).toEqual([]); }); test("desktop segmented controls expose radio semantics and keyboard selection", async () => { const selected: string[] = []; const container = await renderDom( selected.push(value)} />, ); const group = container.querySelector('[role="radiogroup"]'); const radios = [...container.querySelectorAll('[role="radio"]')] as unknown as HTMLElement[]; expect(group).not.toBeNull(); expect(radios.map((radio) => radio.getAttribute("aria-checked"))).toEqual(["true", "false"]); await act(async () => { radios[0]!.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true })); radios[1]!.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true })); }); expect(selected).toEqual(["put", "put"]); }); test("shared buttons activate once by mouse or keyboard and isolate nested actions", async () => { const presses: string[] = []; const container = await renderDom( presses.push("row")}>