Join egghead, unlock knowledge.

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
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Test React Component Props with Enzyme and Jest


    With our component rendered and our assertions testing our nodes, lets test our component’s props. We will use the shallow render methods .props and .prop to test that our component’s props are being passed through correctly. Next we’ll use .setProps to set new props within our components and test that our component handles them correctly.



    Become a Member to view code

    You must be a Pro 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
    orLog In




    Instructor: To begin writing test for component in node-props, let's create a new component. Export class link, extends components, our render method, and we'll return an a-tag with this.props.address. Click and close the a-tag. Perfect.

    Now we have a simple component called link that returns an a-tag with an href that will receive its value by a prop named address. Inside of our test, let's make a new describe block.

    We'll do describe, and then our link component. For our first it, it will be link component, accepts address prop. Inside of this block, we'll do const wrapper equals shallow our link component, within address prop. That goes to

    Let's make sure that we import this link component from our app file. We'll do expect(wrapper.instance.props.address) is

    When it comes to testing component props with Enzyme, it's important to understand which prop or the component we're trying to test. What do we mean by this is, are we trying to test the actual instance of the component? This address prop value right here, or are we trying to test the href value on the return a-tag node?

    For our first test here, we are testing the actual instance of the link address prop. We can see that this is passing our test.

    Next we'll do a-tag node renders href correctly. This block will do a new const wrapper equals shallow our link component address. It is

    We'll expect that our wrapper.props.href is Perfect. This test passes. With this test, we are making sure that our href is using the correct prop value, contrary to our first test where we used the instance, now we're just using the props method on the wrapper itself.

    This will return all the props of the components return node. In our case, we're looking at the a-tag. It's treating the href like a prop. Our two tests are testing the same prop essentially, but in different ways.

    What if our component had a conditional return value depending on the prop that's passed? What if we did this.props.height as a ternary returning null?

    Let's write a new test for this. We'll do it returns null with true height prop. Into this block, we'll do const wrapper equals shallow our link component with the height of false. Going to expect wrapper.find our a.length to be one.

    This test does pass, which means that this false value that we're passing on the height prop is actually returning our a-tag. Let's test if our null was being returned correctly.

    If we do wrapper.setProps with an object where height is true now, we can expect that our wrapper.get(0is null. It looks like our test still passes.

    Now, setProps is a method that takes an object and passes it through as new props to our component. This causes a re-render of our component. It's useful when wanting to test how components behave over time with changing props.

    This method will call, component will receive props lifecycle method on the component. Our get method simply returns the node at the given index of the current wrapper.

    To wrap it all up, we're using the toBeNull method from Jest to make sure that this node that we are returning is actually null.