in react javascript testing ~ read.

Testing with React context

Last we I outlined how to use react's context feature in an application. This week I'll show you how to ensure you can test components that use react context.

Our test setup uses jsDom and Chai to render and test our components. You need to wrap any component so that it can access a mock context.

Our wrapper code ended up looking like:

This takes any component and lets you wrap it with a context. In our tests we can provide a context and then wrap the control.

This renders a DOM based on the context. We can then assert that the proper items are rendered.