import * as React from "react"; import { render } from "../render"; import { type } from "../type"; import { useAllEvents } from "./useAllEvents"; test("type", () => { const values = [] as string[]; const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( values.push(event.target.value)} /> ); }; const { getByLabelText } = render(); const input = getByLabelText("input"); type("ab cd\b\bef", input); expect(values).toMatchInlineSnapshot(` Array [ "a", "ab", "ab ", "ab c", "ab cd", "ab c", "ab ", "ab e", "ab ef", ] `); expect(stack).toMatchInlineSnapshot(` Array [ "focus input", "focusin input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "input input", "keyup input", "keydown input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", ] `); }); test("type readOnly", () => { const values = [] as string[]; const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( values.push(event.target.value)} readOnly /> ); }; const { getByLabelText } = render(); const input = getByLabelText("input"); type("ab cd\b\bef", input); expect(values).toEqual([]); expect(stack).toMatchInlineSnapshot(` Array [ "focus input", "focusin input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", ] `); }); test("type preventDefault on key down", () => { const values = [] as string[]; const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( values.push(event.target.value)} onKeyDown={(event) => { if (event.key !== "e") { event.preventDefault(); } }} /> ); }; const { getByLabelText } = render(); const input = getByLabelText("input"); type("ab\be", input); expect(values).toEqual(["e"]); expect(stack).toMatchInlineSnapshot(` Array [ "focus input", "focusin input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", ] `); }); test("type preventDefault on key press", () => { const values = [] as string[]; const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( values.push(event.target.value)} onKeyPress={(event) => { if (event.key !== "e") { event.preventDefault(); } }} /> ); }; const { getByLabelText } = render(); const input = getByLabelText("input"); type("ab\be", input); expect(values).toEqual(["e"]); expect(stack).toMatchInlineSnapshot(` Array [ "focus input", "focusin input", "keydown input", "keypress input", "keyup input", "keydown input", "keypress input", "keyup input", "keydown input", "input input", "keyup input", "keydown input", "keypress input", "input input", "keyup input", ] `); }); test("type on non-focusable element", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return
; }; const { getByLabelText } = render(); const input = getByLabelText("input"); type("a", input); expect(stack).toEqual([]); }); test("type on focusable non-typeable element", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return