This lesson is for PRO members.

Unlock this lesson NOW!
Already subscribed? sign in

React Testing: Conditional className with Shallow Rendering

2:16 React lesson by

Often our components have output that shows differently depending on the props it is given; in this lesson, we go over how to compare the className prop element tree output based on conditional input.

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

Often our components have output that shows differently depending on the props it is given; in this lesson, we go over how to compare the className prop element tree output based on conditional input.

Avatar
Zachary Klein

I don't want to be judgy, but this doesn't strike me as a very illuminating example. I was hoping there might be some support for variable conditions in tests (so the same test could be run with different inputs/expected output). Of course it's no fault of the lesson that this (apparently) isn't possible, but I'm not sure what I learned from this video. :(

Here we have an app that lets us add quotes to a list and then like and unlike those quotes. We like to test that the className gets applied correctly based on the conditions of this like counter component here. Inside of my like counter test file I've set up the imports for React and the React test library as well as my assertion library and my component itself. I've also set up the basic describe block for the test.

Now to write our test let's first write an IT-Block and say that it should show the like count is active. Next, let's create our shallow render. Then we can render out our component, so our like counter component. It accepts a count, which is the count of likes that are currently in the component as well as the active state.

We'll say "is active," and for this test we're going to test that the like count renders correctly when it is active. So we'll say "True" inside of "Is active." Now I'll set up my assertions, so we'll expect the actual value is going to equal the expected value. Now my actual value is going to be whether or not the rendered element contains the className that we're expecting.

We'll check the shallow render output and then look at the className prop. We want to make sure that that includes the like counter active string. We're expecting that when the like counter component gets rendered that, depending on this prop, this "Is active" prop, that it will render the correct className.

Because, in this situation that we passed in "True" to "Is active," we're going to say that our expected value is going to be true. Now this test is complete, but we still want to check and make sure that if "is active" is false that the class gets rendered correctly as well, so let's create a similar test.

Here I've added a new test, and our original test is here. In the new test, you can see that the main differences are that "Is active" is false instead of true and that we expect the like counter active class string to not be inside of the className prop. That is how you can test conditional ClassNames.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?