import React from "react";
import { render, cleanup } from "@testing-library/react";
import "@testing-library/jest-dom/extend-expect";
import userEvent from "../../src";
afterEach(cleanup);
describe("userEvent.click", () => {
it.each(["input", "textarea"])(
"should fire the correct events for <%s>",
type => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
React.createElement(type, {
"data-testid": "element",
onMouseOver: eventsHandler,
onMouseMove: eventsHandler,
onMouseDown: eventsHandler,
onFocus: eventsHandler,
onMouseUp: eventsHandler,
onClick: eventsHandler
})
);
userEvent.click(getByTestId("element"));
expect(events).toEqual([
"mouseover",
"mousemove",
"mousedown",
"focus",
"mouseup",
"click"
]);
}
);
it('should fire the correct events for ', () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
);
userEvent.click(getByTestId("element"));
expect(events).toEqual([
"mouseover",
"mousemove",
"mousedown",
"mouseup",
"click",
"change"
]);
expect(getByTestId("element")).toHaveProperty("checked", true);
});
it('should fire the correct events for ', () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
);
userEvent.click(getByTestId("element"));
expect(events).toEqual([]);
expect(getByTestId("element")).toHaveProperty("checked", false);
});
it('should fire the correct events for ', () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
);
userEvent.click(getByTestId("element"));
expect(events).toEqual([
"mouseover",
"mousemove",
"mousedown",
"mouseup",
"click",
"change"
]);
expect(getByTestId("element")).toHaveProperty("checked", true);
});
it('should fire the correct events for ', () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
);
userEvent.click(getByTestId("element"));
expect(events).toEqual([]);
expect(getByTestId("element")).toHaveProperty("checked", false);
});
it("should fire the correct events for
", () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
);
userEvent.click(getByTestId("div"));
expect(events).toEqual([
"mouseover",
"mousemove",
"mousedown",
"mouseup",
"click"
]);
});
it("toggles the focus", () => {
const { getByTestId } = render(
);
const a = getByTestId("A");
const b = getByTestId("B");
expect(a).not.toHaveFocus();
expect(b).not.toHaveFocus();
userEvent.click(a);
expect(a).toHaveFocus();
expect(b).not.toHaveFocus();
userEvent.click(b);
expect(a).not.toHaveFocus();
expect(b).toHaveFocus();
});
it.each(["input", "textarea"])(
"gives focus to <%s> when clicking a