DEV Community

Tim Kamanin πŸš€
Tim Kamanin πŸš€

Posted on β€’ Originally published at timonweb.com

6 2

How to test if element exists/doesn't exist with Jest and react-testing-library

At first, let's create a simple avatar component:

function Avatar({ username, src, usernameIsHidden = false }) {
  return (
    <div>
      <img src={src} />
      {!usernameIsHidden && <h4 data-testid="username">{username}</h4>}
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

The usernameIsHidden flag allows us to toggle visibility of a username.

We'll test username element for existence and non-existence.

1. Here's how to test if the element exists and its content matches our expectation:

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

test("username exists", () => {
  const { getByTestId } = render(
    <Avatar username="Timonweb" src="https://example.com/avatar.jpg" />
  );
  expect(getByTestId(/username/i).textContent).toBe("Timonweb");
});
Enter fullscreen mode Exit fullscreen mode

2. We can also test if the element exists at all:

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

test("username exists", () => {
  const { queryByTestId } = render(
    <Avatar username="Timonweb" src="https://example.com/avatar.jpg" />
  );
  expect(queryByTestId(/username/i)).toBeTruthy();
});
Enter fullscreen mode Exit fullscreen mode

3. Finally, this is how to test for the non-existence of the element:

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

test("username doesn't exist", () => {
  const { queryByTestId } = render(
    <Avatar
      username="Timonweb"
      src="https://example.com/avatar.jpg"
      usernameIsHidden
    />
  );
  expect(queryByTestId(/username/i)).toBeNull();
});
Enter fullscreen mode Exit fullscreen mode

In cases 2 and 3, we use queryByTestId instead of getByTestId. queryByTestId doesn't fail when the queried element doesn't exist, instead, it returns either a value or null and that's what we test with expect().toBeTruthy() and expect().toBeNull().

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay