Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function press(
key: string,
element?: Element | null,
options: KeyboardEventInit = {}
) {
if (element == null) {
element = document.activeElement || document.body;
}
if (!element) return;
// We can't press on elements that aren't focusable
if (!isFocusable(element) && element.tagName !== "BODY") return;
// If element is not focused, we should focus it
if (element.ownerDocument?.activeElement !== element) {
if (element.tagName === "BODY") {
blur();
} else {
focus(element);
}
}
// Track event.preventDefault() calls so we bail out of keydown/keyup effects
const defaultPrevented = subscribeDefaultPrevented(
element,
"keydown",
"keyup"
);
export function type(
text: string,
element?: DirtiableElement | null,
options: InputEventInit = {}
) {
if (element == null) {
element = document.activeElement;
}
if (!element || !isFocusable(element)) return;
if (!isTextField(element)) {
warning(
true,
"[reakit-test-utils/type]",
"You're trying to type on an element that is not able of being typed on a keyboard."
);
return;
}
focus(element);
// Set element dirty so blur() can dispatch a change event
element.dirty = true;
for (const char of text) {