Testing user events
In this section, we will learn how to simulate user events and test the resulting output. To test component interactions, similar to the case with users, we need methods to simulate DOM events in our tests. Numerous events caused by users can occur on the DOM. For example, a user can perform a keypress event by entering text into an input box, a click event by clicking a button, or they can view drop-down menu items with a mouseover event. The DOM Testing Library provides two libraries to simulate user actions, fireEvent
and user-event
, which we are going to see in the following sections.
Simulating user actions with fireEvent
We can use the fireEvent
module to simulate user actions on the resulting DOM output of components. For example, we can build a reusable Vote
component that renders the following DOM output:
In the preceding screenshot, the number 10 represents the likes rating. We have two...