1. 3
    Use the queryBy Query to get Elements that Won't Always be on the Page
    59s

Use the queryBy Query to get Elements that Won't Always be on the Page

Daniel Afonso
InstructorDaniel Afonso
Share this video with your friends

Social Share Links

Send Tweet

In order for you to interact with the elements in your tests, you need to query for them.

In this lesson, we will learn about one of the query variants that allows you to search for something that you expect not to be in your UI: the queryBy*.

Instructor: [0:00] As you just saw, getByQueries throw an error when they don't find what they're looking for. For scenarios where we want to check if something is not in our UI, we can use the QueryByQuery.

[0:10] QueryBy works just like getByQueries do, but with one exception. They return "No," when they don't find what they're looking for. For this lesson, let us assert that some text is not in our UI.

[0:20] First, let us destructure the QueryByQuery from Render. Since we'll be looking for text, we use the QueryByText query. Now, onto the querying. Let's call QueryByText and pass it a string saying, "This text does not exist."

[0:35] When this query runs, it won't find this given text and return . This value will be assigned to our TextConst here. Just to be sure this works, let us do an assertion.

[0:45] Let us resort to the toBeInTheDocument just on assertion again, but in this scenario, we expect that our TextConst won't be in the document. If we run our test, we should see that it has passed, which means that indeed, this text is not in our UI.