import { isFocusable, getClosestFocusable, closest } from "reakit-utils"; import { warning } from "reakit-warning"; import { fireEvent } from "./fireEvent"; import { focus } from "./focus"; import { hover } from "./hover"; import { blur } from "./blur"; import "./mockClientRects"; function getClosestLabel(element: Element) { if (!isFocusable(element)) { return closest(element, "label"); } return null; } function getInputFromLabel(element: HTMLLabelElement) { const input = element.htmlFor ? element.ownerDocument?.getElementById(element.htmlFor) : element.querySelector("input,textarea,select"); return input as | HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | null | undefined; } function clickLabel(element: HTMLLabelElement, options?: MouseEventInit) { const input = getInputFromLabel(element); const isInputDisabled = Boolean(input?.disabled); if (input) { // JSDOM will automatically "click" input right after we "click" the label. // Since we need to "focus" it first, we temporarily disable it so it won't // get automatically clicked. input.disabled = true; } const defaultAllowed = fireEvent.click(element, options); if (input) { // Now we can revert input disabled state and fire events on it in the // right order. input.disabled = isInputDisabled; if (defaultAllowed && isFocusable(input)) { focus(input); // Only "click" is fired! Browsers don't go over the whole event stack in // this case (mousedown, mouseup etc.). fireEvent.click(input); } } } function setSelected(element: HTMLOptionElement, selected: boolean) { element.setAttribute("selected", selected ? "selected" : ""); element.selected = selected; } function clickOption( element: HTMLOptionElement, eventOptions?: MouseEventInit ) { // https://stackoverflow.com/a/16530782/5513909 const select = closest(element, "select") as HTMLSelectElement & { lastOptionSelectedNotByShiftKey?: HTMLOptionElement; }; if (!select) { fireEvent.click(element, eventOptions); warning( true, "[reakit-test-utils/click]", "You're trying to click on an