Instructor: 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.
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.
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.
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.
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.
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.
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.
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.
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.
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...