import * as React from "react"; import { render } from "../render"; import { click } from "../click"; import { useAllEvents } from "./useAllEvents"; test("click", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ; }; const { getByText } = render(); const button = getByText("button"); click(button); expect(button).toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover button", "pointerenter button", "mouseover button", "mouseenter button", "pointermove button", "mousemove button", "pointerdown button", "mousedown button", "focus button", "focusin button", "pointerup button", "mouseup button", "click button", ] `); }); test("click disabled", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( ); }; const { getByText } = render(); const button = getByText("button"); click(button); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover button", "pointerenter button", "pointermove button", "pointerdown button", "pointerup button", ] `); }); test("click disabled after clicking non-disabled", () => { const stack = [] as string[]; const Test = () => { const ref1 = React.useRef(null); const ref2 = React.useRef(null); useAllEvents(ref1, stack); useAllEvents(ref2, stack); return ( <> ); }; const { getByText, baseElement } = render(); const button1 = getByText("button1"); const button2 = getByText("button2"); click(button1); expect(button1).toHaveFocus(); click(button2); expect(baseElement).toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover button1", "pointerenter button1", "mouseover button1", "mouseenter button1", "pointermove button1", "mousemove button1", "pointerdown button1", "mousedown button1", "focus button1", "focusin button1", "pointerup button1", "mouseup button1", "click button1", "pointermove button1", "mousemove button1", "pointerout button1", "pointerleave button1", "mouseout button1", "mouseleave button1", "pointerover button2", "pointerenter button2", "pointermove button2", "pointerdown button2", "blur button1", "focusout button1", "pointerup button2", ] `); }); test("click within focusable container", () => { const Test = () => { return (
parent
child
); }; const { getByText, baseElement } = render(); const parent = getByText("parent"); const child = getByText("child"); expect(baseElement).toHaveFocus(); click(child); expect(parent).toHaveFocus(); }); test("click disabled within focusable container", () => { const Test = () => { return (
); }; const { getByText, baseElement } = render(); const button = getByText("button"); expect(baseElement).toHaveFocus(); click(button); expect(button.parentElement).toHaveFocus(); }); test("click label htmlFor", () => { const stack = [] as string[]; const Test = () => { const label = React.useRef(null); const input = React.useRef(null); useAllEvents(label, stack); useAllEvents(input, stack); return ( <> ); }; const { getByText, getByLabelText } = render(); const label = getByText("label"); const input = getByLabelText("label"); click(label); expect(input).toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover label", "pointerenter label", "mouseover label", "mouseenter label", "pointermove label", "mousemove label", "pointerdown label", "mousedown label", "pointerup label", "mouseup label", "click label", "focus input", "focusin input", "click input", ] `); }); test("click label wrapping input", () => { const stack = [] as string[]; const Test = () => { const label = React.useRef(null); const input = React.useRef(null); useAllEvents(label, stack); useAllEvents(input, stack); return ( ); }; const { getByText, getByLabelText } = render(); const label = getByText("label"); const input = getByLabelText("label"); click(label); expect(input).toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover label", "pointerenter label", "mouseover label", "mouseenter label", "pointermove label", "mousemove label", "pointerdown label", "mousedown label", "pointerup label", "mouseup label", "click label", "focus input", "focusin input", "focusin input", "click input", "click input", ] `); }); test("click label htmlFor disabled input", () => { const stack = [] as string[]; const Test = () => { const label = React.useRef(null); const input = React.useRef(null); useAllEvents(label, stack); useAllEvents(input, stack); return ( <> ); }; const { getByText, baseElement } = render(); const label = getByText("label"); click(label); expect(baseElement).toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover label", "pointerenter label", "mouseover label", "mouseenter label", "pointermove label", "mousemove label", "pointerdown label", "mousedown label", "pointerup label", "mouseup label", "click label", ] `); }); test("click checkbox", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ; }; const { getByLabelText } = render(); const checkbox = getByLabelText("checkbox"); expect(checkbox).not.toBeChecked(); click(checkbox); expect(checkbox).toBeChecked(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover checkbox", "pointerenter checkbox", "mouseover checkbox", "mouseenter checkbox", "pointermove checkbox", "mousemove checkbox", "pointerdown checkbox", "mousedown checkbox", "focus checkbox", "focusin checkbox", "pointerup checkbox", "mouseup checkbox", "click checkbox", "input checkbox", "change checkbox", ] `); }); test("click checkbox label htmlFor", () => { const stack = [] as string[]; const Test = () => { const label = React.useRef(null); const checkbox = React.useRef(null); useAllEvents(label, stack); useAllEvents(checkbox, stack); return ( <> ); }; const { getByText, getByLabelText } = render(); const label = getByText("label"); const checkbox = getByLabelText("label"); expect(checkbox).not.toBeChecked(); click(label); expect(checkbox).toBeChecked(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover label", "pointerenter label", "mouseover label", "mouseenter label", "pointermove label", "mousemove label", "pointerdown label", "mousedown label", "pointerup label", "mouseup label", "click label", "focus checkbox", "focusin checkbox", "click checkbox", "input checkbox", "change checkbox", ] `); }); test("click label preventDefault onClick", () => { const stack = [] as string[]; const Test = () => { const label = React.useRef(null); const input = React.useRef(null); useAllEvents(label, stack); useAllEvents(input, stack); return ( <> {/* eslint-disable-next-line */} ); }; const { getByText, getByLabelText } = render(); const label = getByText("label"); const input = getByLabelText("label"); click(label); expect(input).not.toHaveFocus(); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover label", "pointerenter label", "mouseover label", "mouseenter label", "pointermove label", "mousemove label", "pointerdown label", "mousedown label", "pointerup label", "mouseup label", "click label", ] `); }); test("click button preventDefault onMouseDown", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( ); }; const { getByText } = render(); const button = getByText("button"); click(button); expect(button).not.toHaveFocus(); }); test("click button preventDefault onPointerDown", () => { const stack = [] as string[]; const Test = () => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( ); }; const { getByText } = render(); const button = getByText("button"); click(button); expect(button).not.toHaveFocus(); }); test("select", async () => { const stack = [] as string[]; const Test = ({ multiple }: { multiple?: boolean }) => { const ref = React.useRef(null); useAllEvents(ref, stack); return ( ); }; const { getByText, getByLabelText, rerender } = render(); const select = getByLabelText("select") as HTMLSelectElement; const option1 = getByText("option1") as HTMLOptionElement; const option2 = getByText("option2") as HTMLOptionElement; const option3 = getByText("option3") as HTMLOptionElement; const option4 = getByText("option4") as HTMLOptionElement; click(option2); expect(option2.selected).toBe(true); expect(Array.from(select.selectedOptions)).toEqual([option2]); rerender(); click(option2); click(option4, { shiftKey: true }); expect(Array.from(select.selectedOptions)).toEqual([ option2, option3, option4, ]); expect(stack).toMatchInlineSnapshot(` Array [ "pointerover option2", "mouseover option2", "pointermove option2", "mousemove option2", "pointerdown option2", "mousedown option2", "focus select", "focusin select", "pointerup option2", "mouseup option2", "input select", "change select", "click option2", "pointermove option2", "mousemove option2", "pointerout option2", "mouseout option2", "pointerover option2", "mouseover option2", "pointermove option2", "mousemove option2", "pointerdown option2", "mousedown option2", "pointerup option2", "mouseup option2", "input select", "change select", "click option2", "pointermove option2", "mousemove option2", "pointerout option2", "mouseout option2", "pointerover option4", "mouseover option4", "pointermove option4", "mousemove option4", "pointerdown option4", "mousedown option4", "pointerup option4", "mouseup option4", "input select", "change select", "click option4", ] `); click(option3, { ctrlKey: true }); click(option1, { ctrlKey: true }); expect(Array.from(select.selectedOptions)).toEqual([ option1, option2, option4, ]); click(option3, { shiftKey: true }); expect(Array.from(select.selectedOptions)).toEqual([ option1, option2, option3, ]); });