Sometimes our React components render different outputs depending on their local state. In this lesson we will use Enzyme’s setState method to test that our component’s rendered output is as expected.
Duration
1 min
Access
Pro
Transcript
Available
Sometimes our React components render different outputs depending on their local state. In this lesson we will use Enzyme’s setState method to test that our component’s rendered output is as expected.
Video Transcript
Instructor: Sometimes, our React components use local state to conditionally update our component attributes. If we wanted to simulate and test different component contexts, we can manually update the rendered component inside of our testing environment.
For our new test, we'll say it updates class name with new state. Inside of this block, we'll do const wrapper equals shallow app component. We'll expect that we find the blue class with a length on one and our red class zero. Until we update our state, we'll put main color to red. Now, we'll expect that our blue is zero and red is one.
Now, inside of our app file, let's add our main color property, initially to be blue. Our H3 class name is this.state.mainColor. We'll say, "Everyone is welcome." Close H3 and save it. Perfect.
Now, the only failing tests we have are snapshots. If we update these, we're going to see that our test now pass. Now, as you can imagine, when we use this setState method on our wrapper, it will invoke setState on the root component and cause it re-render.
This is useful for testing our components in different states.
Duration
1 min
Access
Pro
Transcript
Available
Course lesson
Test New Component State with setState in Enzyme
Sometimes our React components render different outputs depending on their local state. In this lesson we will use Enzyme’s setState method to test that our component’s rendered output is as expected.
Access required
This lesson is available with an active egghead membership.