1. 1
    Render a Component Using the React Testing Library
    2m 17s

Render a Component Using the React Testing Library

Daniel Afonso
InstructorDaniel Afonso
Share this video with your friends

Social Share Links

Send Tweet
Published a year ago
Updated a year ago

One thing is important to test your component, and that is rendering!

In this lesson, we will learn about a function that the React Testing Library gives you to do called render.

We will also look at some of the utilities that render gives you to use in your tests.

Instructor: [0:00] We want to start with a test called shouldRenderPage. In this scenario, we want to render a page. Obviously, we're going to be needing our app, which is going to be a page where our code is. For us to be able to render this, we need to use a function from the React testing library called render. Let's import it.

[0:15] In order to use render we need to pass it to our component. For us to do so we need to call render and then pass it to our app component. What this will do is render our component into a container which will be appended to a document body. Let us now check this test running.

[0:29] As you can see the test has passed because there's no assertion being done, so Jest will just assume that everything is OK with your test. Besides allowing you to render your components, the render function also gives you some utils that you can use to improve your testing experience.

[0:44] Let's see which things we can destructure from render. The first thing we are destructuring from render is the container. Now this is the container where the render function will render our component after we invoke it. Container will be pretty much useful when we get to querying later on.

[0:58] The second thing we destructure from render is the unmount function. The unmount function allows you to pretty much simulate and mount events, to unmount your component. This is pretty useful when you have scenarios that you want to assert that all the things that you are doing when your component unmounts are actually working.

[1:14] The third thing we destructure from render is the re-render function. The re-render function allows you to force your component to re-render manually. The use-case for this would be if you have a scenario that you want to check when props change, what happens to your component.

[1:28] Now, you have to be careful if you decide to do this because this is considered to be an implementation detail, and this is actually something that the testing library doesn't encourage.

[1:37] Last but not least, we are now destructuring debug. Debug is the thing that we'll be seeing now how it works, so let's call it. As you can see, debug allows you to see a pretty printed version of how your UI looks. Finally, we don't want our test to end without an assertion, so let us add one.

[1:53] What we're going to do is leverage the container where the render function rendered our component, and by using a just DOM matcher called toBeEmptyDomElement, we're going to check if this container is not empty.

[2:04] If we re-run our test, you're going to still see a pretty printed version of the UI because the debug function is still running, but you're going to see that our test has passed. This means that indeed the render function is working successfully.

egghead
egghead
~ 11 seconds 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