View on GitHub

react-testing-library-cheatsheet

A cheatsheet for basic methods of @testing-library/react

Following are few examples of methods supported by React Testing Library(RTL) for easy access.

Table of Contents

Fetch elements by

aria-label

Source code

<input aria-label="givenName">

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const givenName = wrapper.getByLabelText("givenName");

input placeholder

Source code

<input placeholder="Enter user name">

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const givenName = wrapper.getByPlaceholderText("Enter user name");

role

Source code

<Spinner role="status" />

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const spinnerDisplay = wrapper.getByRole("status");

NOTE: It is recommended to use valid roles with non-abstract ARIA role.

test id

Source code

<button data-testId="buttonTest">Click Me!</button>

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const buttonElement = wrapper.getByTestId("buttonTest");

NOTE: It is recommended to avoid relying too heavily on implementation details like specific data-testid attributes. Instead, it’s best to utilize available attributes and search methods that reflect how users interact with the application.

id

Source code

<button id="clickButton">Click</button>

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const buttonElement = wrapper.container.querySelector("#clickButton");

class name

Source code

<a class="text-info">Home</a>
<a class="text-info">About</a>
<a class="text-info">Contact</a>

Test code

const { container } = render(
    <DOMElement
    data=
    />);

expect(container.getElementsByClassName("text-info").length).toBe(3);

text

Source code

<button>Connect with us</button>

Test code

const wrapper = render(
    <DOMElement
    data=
    />);

const elementFull = wrapper.getByText("Connect with us");
const elementIgnoreCase = wrapper.getByText("connect with us", {exact: false});
const elementSubstring = wrapper.getByText("with us", {exact: false});

Assertion

expect(wrapper.container.textContent).toMatch("Hello World");

Fire click event on an element

render(<DOMElement data=/>);

<!-- Fire click event on an element containing a rendered text "Submit" -->
fireEvent.click(screen.getByText("Submit"));

OR

const wrapper = render(
    <DOMElement
    data=
    />);

<!-- Fetch DOM element -->
const buttonElement = wrapper.getByText("Submit");
<!-- Fire click event on the element -->
fireEvent.click(buttonElement);

Fire change event on an input field

const wrapper = render(
    <DOMElement
    data=
    />);

<!-- Fetch the input field by label text -->
const givenName = wrapper.getByLabelText("givenName");
<!-- Fire change event on the above field -->
fireEvent.change(givenName, { target: { value: "testValue" } });
<!-- Assert the value entered in DOM -->
expect(givenName.value).toBe("testValue");

Fire key press event

const wrapper = render(
    <DOMElement
    data=
    />);

<!-- Fetch the input field by placeholder text -->
const passwordField = wrapper.getByPlaceholderText("Enter password");
<!-- Fire change event on the above field -->
fireEvent.change(passwordField, { target: { value: "Test@123" } });
<!-- Fire key press event of "Enter" key -->
fireEvent.keyPress(passwordField, { key: "Enter", code: 13, charCode: 13 });
<!-- Similar with keyUp, keyDown -->

Wait for expectation to pass

await waitFor(() => expect(wrapper.container.textContent).toMatch("Click Me"));

The default timeout is 1000ms.

The examples provided offer a glimpse into a few methods. For a deeper dive into the library, please explore the following official link, which provides additional details: react-testing-library/cheatsheet

Using debug method

import { render, screen } from "@testing-library/react";

const wrapper = render(<DOMElement
    data=
    />);
screen.debug();

Debug Print Limit

Debug statements have a print limit of 7000 by default, you will see ... in the console, when the DOM content is stripped off. To print more content when the DOM size is really large:

In MAC/Linux:

DEBUG_PRINT_LIMIT=10000 npm test

In Windows:

Install cross-env

npm install --save-dev cross-env

Update the script for test to append

"scripts": {
  "test": "cross-env DEBUG_PRINT_LIMIT=10000 react-scripts test"
}