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

Level Up!

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

Autoplay

    Test a Component That Uses a React Context Provider

    Dave CeddiaDave Ceddia
    reactReact
    ^16.6.0

    Providing a Context from a component can make it tricky to test, because it requires that you use a matching Consumer in your tests. In this lesson you’ll learn how to test a component that uses a Context Provider.

    Code

    Code

    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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Our userContext file has this userProvider component as a wrapper around the context provider. We like to test that this userProvider is providing the right values and working as expected. In our terminal, we'll run yarn add react-testing-library. We can run yarn test to start up Jest, which'll start watching for tests.

    We'll create a new directory underneath source called tests. Inside this directory, we'll create a new file called userContext.test.js. Inside here, we'll import react, because we're going to be using JSX, and we'll import render and fireEvent from react-testing-library.

    Then we'll import the userProvider that we want to test, along with the userConsumer from userContext. For our first test, we'll check that the default value is undefined. To do this, we'll declare a variable called actualValue, and set it equal to replaceMe.

    Then we'll call render, and render out our userConsumer, with no provider around it. As its render function, we'll take the value that it gets from context, and save that in actualValue. Now, we can write our expectation by saying expect actualValue.toBeUndefined.

    After saving, we can see our test is passing. Now, over in our userContext file, we can see that we're initializing the current user to this fake user that we're getting from API. Let's test that. We'll write another test that is initial user is fake user.

    We're going to need to import fake user from our API. Here, we can call render. This time, we'll render out a userProvider. Inside it, we'll render the userConsumer. The consumer's render function will destructure the user and render out a div with the user's username.

    The render function here returns an object, and one of the properties is container. We'll destructure container from render. Now, the setup is done, and we can write expect container.textContent to equal fakeUser.username. We'll save this, and now, this test is passing.

    We could have written the actual username here, but by depending on the imported value, we can change that value in API without breaking this test. Now, back over to userContext. We can see that one of the properties it provides is this onLogin callback.

    When it's called, it should update the user. Let's test that that works. We'll write a test, that onLogin sets the user. Here, we'll destructure the container from render, and we'll render the userProvider, wrapped around a userConsumer.

    In the render function, we'll destructure user and onLogin, and render out a div. Inside here, we'll have a span that renders the user's username. Under that, we'll have a button with an onClick prop, which will call onLogin with an object with a username property.

    Now, under render, we can call fireEvent.click, and pass in the button element, which we can find with container.querySelector button. Then we can write expect container.querySelector span.textContent to equal the username. After we save, we can see this test is passing now, too.

    userContext also passes down an onLogout callback that should clear out the user. Let's test that now. We'll write a test that onLogout clears the user. This test is going to be very similar to the login test. I'm going to copy and paste that down below.

    Instead of onLogin, we'll destructure onLogout. In the button's onClick function, we can call onLogout. Since the user's going to be null, we don't want to render out the user's username. We'll check if user is null, and render out the toString of that.

    If user is null, undefined, true, or false, all of those things would render as empty. To make sure that it's null, we'll check that, and turn it into a string, which should render out true. Now, we can change our expectation down below, that the textContent should be the string "true." If we save that, the test is passing.