...

/

Introduction to queries and getBy Variant

Introduction to queries and getBy Variant

Learn about queries and it’s ‘getBy’ variant in React Testing Library.

The starter project for this lesson contains a PersonPage component that renders a person’s name and scores for a given id. The person’s name and scores are retrieved from an asynchronous function called getPerson. This simulates a function that would retrieve the data from a web service.

A copy of the project is in the code widget below.

export default "test-file-stub";
Simple app to display details on a webpage

Start the app by entering npm start into the terminal. If you are using the code widget, the app will start automatically when the “Run” button is clicked.

The PersonPage component is displayed, which shows a person’s name, email address, and scores.

What is a query?

A query in React Testing Library is a function that finds an element that we want to check in a test. There are different types of queries that find the element in different ways. The query type we have used so far is ByText, which finds the element by its text content. We’ll learn about other ...