Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
User Event Options
2.1.
applyAccept
2.2.
autoModify
2.3.
delay
2.4.
document
2.5.
keyboardMap
2.6.
pointerEventsCheck
2.7.
pointerMap
2.8.
skipAutoClose
2.9.
skipClick
2.10.
skipHover
2.11.
writeToClipboard
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

User Events Options

Introduction

When we interact with any page or element in a browser, it triggers an event to execute the expected action or functionality. These events are called “User Events”. The User event is a companion library for the testing library that executes user events by triggering them based on the content and the interaction between user and browser. It makes triggering events easier for users and finds bugs. You can install the user event using the following commands.

npm install --save-dev@testing-library/user-event@^14.0.0-beta
// using npm
yarn add --dev @testing-library/user-event@^14.0.0-beta
// using yarn


Let’s learn how to write a basic test using the userEvent method.

test('trigger the event on clicking the button', async () => {
   const user = userEvent.setup()
   render(<MyComponent/>)
   await user.click(screen.getByRole('button', name: /click me/))
})


We created a basic test that is an asynchronous method. The user event gets triggered when the user clicks on a button. We get the button by its role using the getByRole() method, and then the trigger is using the user.click method.

User Event Options

The options allow us to adjust the behavior of the user events APIs and can be applied per setup(). There are many options available in the user events. Let’s take a look at a few options.

applyAccept

This option discards all the files that do not match the accept property when using the upload() function. The default value of the property will be set to true.

autoModify

We automatically apply modifier keys for characters when the caps lock is not active. This option allows us to choose whether to apply the automatic modification or not. The default value of the property is set to true.

delay

The delay between subsequent inputs like typing a series of characters or printing is set using this option. The default time will be 0 seconds.

document

The owner document on the element is set to the option by default if the API is called directly without setup. Otherwise, the global document is set to this option.

default: element.ownerDocument ?? global.document

keyboardMap

This option consists of an array of the keys on the keyboard. The default keyboard is a standard QWERTY keyboard.

pointerEventsCheck

The pointer API includes a check if the pointer-events property of an element is none. This option defines how often the check should be performed by pointer events.

pointerMap

This option contains an array of all available pointer keys and allows to plug in different pointer devices.

skipAutoClose

The type() function automatically releases the keys pressed at the end of the function call. This option allows us to skip this feature and stop the release of keys. The default value is false.

skipClick

The type() method implies the click event on any element. This option allows us to skip this property of the type() method. The default value is false.

skipHover

The click() method moves the cursor to the target element first. This option can skip this feature and allows us to opt-out of it. The default value is false.

writeToClipboard

This option allows us to write the copied text or data to the clipboard. The default value is false when the APIs are called directly. The default value is true when the APIs are called on an instance from setup().

FAQs

  1. What is testing library user-event?
    The User event is a companion library for the testing library that executes user events by triggering them based on the content and the interaction between user and browser.
     
  2. What is the difference between fireEvent and user event?
    The fireEvent dispatches and triggers the events that we define, but the user event triggers the events that are bound to happen when the user interacts with the browser.
     
  3. What are examples of user events?
    The user events are triggered according to the actions performed by the user. The actions can be clicking a button, loading a page, clicking on a link, etc.
     
  4. What does a user event option do?
    The options allow us to adjust the behavior of the user events APIs and can be applied per setup().
     
  5. How to install the user events?
    The user events can be installed from the testing library using npm(Node Package Manager) or yarn by running the following commands.
     
npm install --save-dev @testing-library/user-event@^14.0.0-beta
yarn add --dev @testing-library/user-event@^14.0.0-beta

Key Takeaways

We have discussed the user events and their options. We also learned to install user events and write a basic test using user event trigger functions. Now you can perform events using the options discussed in this article.

Hey Ninjas! We hope this blog helped you understand user events and their usage. If you want to learn more, check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems. Please upvote our blog to help the other ninjas grow.

Happy Coding!

Live masterclass