Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Find Nodes from a Shallow Rendered Component

    Tyler ClarkTyler Clark

    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.

    reactReact
    ^16.0.0
    jestJest
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    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.

    Discuss

    Discuss