Find Nodes from a Shallow Rendered Component

Tyler Clark
InstructorTyler Clark
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

Once we have a shallowed rendered component, we can now test that the expected nodes rendered are correct. We will use Jest’s built in assertions as well as some Enzyme shallow render methods in our tests. The render methods include .find, .exists, .children, .hasClass, and .text.

Instructor: [00:00] Now that we have a shallow rendered component, we can use some built-in methods to search for certain nodes. Then we'll use JS's assertions to make sure that everything renders correctly. Let's write expect wrapper.find, p.length to be one.

[00:19] Now, let's refactor our test, and change the wording to just contain one p element. Perfect. Now, if we go to our app file, we'll see that we only have one p tag inside of our app component. When we shallow render this with Enzyme, we want to make sure that we just see this one p element.

[00:37] Then this find method will find every node in the render tree, or a wrapper that matches the provided selector that we're passing it here. We can run our NPM test script, and make sure that this test pass. We can see that it does contain just one p element.

[00:58] Now, on our find method, let's change the .length to exists, and our toBe to be true. Our p tag has a classname called appIntro. Let's use that, to be more specific than just the generic p element. We've got our JS test script running in watch mode, so we are still passing on this test.

[01:19] We could easily break this by changing our classname, resaving it. You can see that we fail. The exists method is handy for testing for one specific node. Now, let's say that our app had a ul with a bunch of lis.

[01:38] Let's change exists to children.length, and we want that to equal three. Then inside of our app JS, let's paste in a ul with three lis. With our terminal still in watch mode, we can see that our test is still passing.

[01:55] The children method is handy when scoping tests to a specific parent-child hierarchy. For example, if we added another outlying li, and saved this off, we can see that our tests still pass, because it's only looking for the children of the ul.

[02:11] If our ul had a class called Tyler, we could use the hasClass method to search for specific classes. The hasClass method simply returns true or false if the class exists. Then finally, let's check that our h1 has the correct text, which is, "Welcome to React." It does, perfect.

~ an hour 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

Be on-Topic

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

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