Getting Started with React Testing Library

abdulkareemtpm

ABDULKAREEM

Posted on April 4, 2023

Getting Started with React Testing Library

coverimage

React Testing Library is a popular testing utility that helps developers test their React applications more effectively. It allows developers to write tests that simulate real user interactions and ensure that their applications behave as expected. In this article, we'll walk through the basics of React Testing Library and learn how to write tests for React components using this library.

Before we start, make sure that you have a basic understanding of React and its ecosystem. You should also have some experience with writing tests using a testing framework like Jest.

Installing React Testing Library:
To install React Testing Library, you can use npm or yarn. Simply run the following command in your terminal:

npm install @testing-library/react
Enter fullscreen mode Exit fullscreen mode

or

yarn add @testing-library/react
Enter fullscreen mode Exit fullscreen mode

Writing Your First Test:
Let's start by writing a simple test for a React component. Suppose we have a component called Greeting that takes a name prop and displays a greeting message. Here's what the component looks like:

import React from 'react';

function Greeting(props) {
  return (
    <div>
      <h1>Hello, {props.name}!</h1>
    </div>
  );
}

export default Greeting;
Enter fullscreen mode Exit fullscreen mode

To test this component, create a new file called Greeting.test.js and add the following code:

import React from 'react';
import { render } from '@testing-library/react';
import Greeting from './Greeting';

test('renders greeting message', () => {
  const { getByText } = render(<Greeting name="John" />);
  const greetingElement = getByText(/Hello, John!/i);
  expect(greetingElement).toBeInTheDocument();
});

Enter fullscreen mode Exit fullscreen mode

Let's break down this code:

We import the render function from @testing-library/react and the Greeting component from ./Greeting.
We define a new test case using the test function provided by Jest. This test case checks whether the component renders a greeting message with the correct name.
We use the render function to render the Greeting component with the name prop set to "John". The render function returns an object with various query functions that we can use to find elements in the rendered component.
We use the getByText query function to find an element that contains the text "Hello, John!". The getByText function searches for an element that matches the given text, ignoring case.
We use the toBeInTheDocument matcher provided by Jest to check whether the greetingElement is present in the document.
When you run this test using Jest, it should pass.

Simulating User Interactions:
One of the key features of React Testing Library is its ability to simulate user interactions. Let's say we have another component called Counter that displays a counter value and two buttons to increment and decrement the value. Here's what the component looks like:

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const handleIncrement = () => {
    setCount(count + 1);
  };

  const handleDecrement = () => {
    setCount(count - 1);
  };

  return (
    <div>
      <h1>Count: {count}</h1>
      <button onClick={handleIncrement}>+</button>
      <button onClick={handleDecrement}>-</button>
    </div>
  );
}

export default Counter;
Enter fullscreen mode Exit fullscreen mode

To test this component, we can use React Testing Library to simulate user clicks on the buttons and verify that the counter value updates correctly.

💖 💪 🙅 🚩
abdulkareemtpm
ABDULKAREEM

Posted on April 4, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related