liveBook · Manning

User-event

As explained in React in Depth

user-event is a Testing Library library that simulates realistic user interactions in automated tests.

user-event is a Testing Library library for simulating user interactions in automated React tests. It replaces direct DOM methods with interactions such as clicking, typing, hovering, and dragging. section 10.2.1

Use user-event to:

  • Create a user with userEvent.setup(), locate controls by accessible roles, and await interactions such as user.click(button). section 10.2.1
  • Type into inputs with user.type(), exercising focus, character input, and change handling rather than updating component state directly. section 10.2.3
  • Prefer user-event for ordinary interactions; use React Testing Library’s lower-level fireEvent for specialized cases it does not cover. section 10.2.3

Testing a counter

Render Counter, create a user, find the Increment button by role, await user.click(increment), and assert that the heading reads “Counter: 1”. The same pattern tests decrementing while keeping assertions focused on visible behavior. section 10.2.1

Testing a timer form

Find the minutes and seconds spinbuttons and the Start button, type values into both fields, click Start, and verify that the callback receives the resulting duration. Avoid asserting the component’s internal state representation. section 10.2.3

Listing 10.5 Clicking a button
import { render, screen } from "@testing-library/react";
import userEvent
  from "@testing-library/user-event";     #1
import "@testing-library/jest-dom";
import { Counter } from "./Counter";
test(     #2
  "Counter should start at the given value",    #2
  () => {...},   #2
);    #2
test(    #2
  "Counter should start at 0 if no value is given",   #2
  () => {...},    #2
);    #2
test(    #3
  "Counter should increment when button is clicked",   #3
  async () => {    #3
  // ARRANGE
  render(<Counter />);
  // ACT
  const user = userEvent.setup();    #4
  const increment = screen.getByRole(     #5
    "button",    #5
    { name: "Increment" },    #5
  );    #5
  await user.click(increment);     #6
  // ASSERT
  const heading = screen.getByRole(    #7
    "heading",    #7
    { name: "Counter: 1" },    #7
  );    #7
  expect(heading).toBeInTheDocument();
});

Shows the complete setup-and-click pattern, including accessible queries, userEvent.setup(), asynchronous interaction, and a visible assertion. section 10.2.1

How React in Depth covers it

How deep it goes
Explains setup, accessible queries, asynchronous clicks, typing event sequences, and the fireEvent fallback.
Examples
Uses counter buttons and a timer form with typed minutes, seconds, submission, and callback assertions.
What it assumes
Assumes React Testing Library rendering, accessible role queries, asynchronous tests, and basic component behavior.
What you can do after
Apply it via @testing-library/user-event to test clicks, typing, and submissions without asserting implementation details.
How it connects
Connects user-event with React Testing Library, accessible queries, fireEvent, and Arrange-Act-Assert testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React in Depth. The section links point to the parts of the book it draws on.