This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In


    Unit test a React Render Prop component

    Andrew Del PreteAndrew Del Prete

    In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integration tests are perfect for components that consume a Render Prop component. Likewise, unit tests are important to write for the Render Prop component itself, as you can test each individual unit that makes up the component.



    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


    Instructor: 00:00 I have a simple render prop component for a counter I'd like to unit test. The component accepts an initial value, has state, has two methods for incrementing and decrementing. I passed all these things to the render prop function, which has children in this instance.

    00:14 This render prop component is considered renderless because it doesn't actually render anything to the dom. But it holds the state and logic for other components to consume.

    00:22 We should test those other components that consume this with integration tests. But it's a good idea to thoroughly test your render prop component itself. Let's do that. For this example, I'm going to use enzyme ingest for testing. But can use whatever your heart desires as long as it asserts.

    00:36 The first thing I'm going to do is test that the render prop function gets called on mount. I'll use Jest to mock a function that returns null. We'll do that by writing const render function equals Jest function mock return value null.

    00:52 Next, we'll need to mount our counter and pass our mock function to it. Let's do it by const wrapper equals mount. Pass the counter to it. We'll say render function.

    01:03 The first assertion that we'll make is that the render function has been called. We'll say expect render function mock call's length to be one. For good measure, I'll go ahead and test our state, as well. We'll say expect wrapper state counter to be zero. Check our test. Looks like it's passing.

    01:26 Onto our next test. We'll go ahead and copy this. We'll test the initial value. We'll say accepts an initial value. We'll go ahead and pass an initial value to our counter. We'll adjust our assertion here to say our counter state should be 2,012. Save it. Check our test. It looks like it's passing. Good.

    01:50 Next, we'll copy our test again. We'll test our first method which is increment. We'll just say increments. I'll remove our initial value since we don't need that. I'll manually invoke our increment method, wrap our instance, increment. Now to adjust our assertion. We'll say to be one. Save it. Check our tests. Looks like they're passing.

    02:14 Now, I'll go ahead and copy this, change this to be decrements, invoke our decrement method, and change this to negative one. Save it, check our tests. All of our tests are passing.

    02:27 Remember, it's a good idea to thoroughly test your render prop component, especially if you're publishing a library that others may consume. Integration tests are great for components that consume render prop components like this. But having unit tests can help you cover your bases...