Site icon Paradite

Testing React Apollo components with Enzyme

Guide on How to Test React Apollo components with Enzyme

Guide on How to Test React Apollo components with Enzyme

Update on 12 Sept 2018:

React Apollo introduced official documentation on testing in June 2018:

https://www.apollographql.com/docs/guides/testing-react-components.html

You can still read my original post below, which includes Enzyme specific techniques that is not covered in official docs.


Despite its popularity, React Apollo client has no good documentations on how to do testing for React Apollo components with Enzyme.

The only documentation that I found online for testing React Apollo component is a comment from React Apollo team on GitHub, and it is not using Enzyme.

So how do you test React Apollo components with Enzyme? And perhaps more importantly how do you get access to the inner component wrapped by React Apollo as a higher-order component (HOC) to mock its functions and verify its behaviours?

Method 1: Using React Apollo’s MockedProvider (HOC)

If you are following the guide for React Apollo query when writing your components, you probably have something like this in the file for your component:

By default, this would export the higher-order component wrapped with Apollo’s GraphQL query. This allows the inner component to have access to the query result via  props.data.

If you just want to test the higher-order component with the React Apollo logic, you can use the MockedProvider inside react-apollo/test-utils package. Here is an example of how the test file would look like:

A few things to note here:

Method 2: Export Plain Component Separately from HOC

Instead of just exporting the HOC in your component file, you can additionally export the plain component, along with the HOC. An example on how to do that was provided by the React Apollo team in this comment.

Apply this technique to rewrite MyComponent file in method 1, we have:

Notice that we are exporting both MyComponent , and the HOC wrapped by React Apollo.

In order to import both the default export and the named export of the component, you can do:

Where MyComponentWithGQL is the HOC, and MyComponent is the plain component. If you are just importing MyComponent for the test, you can ignore the first part.

With the separately exported plain component, we can test it directly without the MockedProvider wrapper and set the props manually:

Compared to the first method, there are a few key differences:

 

Conclusion

So that was two ways to test React Apollo components using Enzyme. The test framework I used here is jest, but the idea should be applicable to other frameworks as well.

I personally prefer the second method of testing because it is simpler to write test. However, the first method does allow some form of integration testing for the component, so it might be useful in certain cases as well.


Cover image from https://medium.com/@reyhansofian/react-native-with-react-apollo-df8fd712f129

Exit mobile version