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
Published 7 months ago
Updated 6 months ago

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.

egghead
egghead
~ 5 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today