Unit Testing React Form with Jest and Enzyme

Jest is a JavaScript unit testing framework created by Facebook. I usually use mocha for unit testing Node.js code, but when it comes to testing React, Jest combined with Enzyme is pretty much the best. Enzyme is a JavaScript utility library created by Airbnb. It lets you simulate DOM rendering and actions on the components and integrates well with Jest.

In this post, I am focusing on testing React form. When I first started writing unit tests on React form components, I spent a bit of time investigating and experimenting different ways. I especially struggled to write a unit test on multi-select and checkbox component. So, here it is.

Focus

  • Testing a simple text input
  • Testing checkbox
  • Testing Multi-select options.
  • Testing alert() function on form submit.

Complete Solution

This blog focuses purely on unit test code. You can check out the complete solutions here: react-form-unit-test-example.

Unit Test Code

Here is the main Form component to be tested.

(1) Testing Text Input

Testing text input is relatively simple. First mount the component with Enzyme’s mount function, then set the value and simulate the change. When there are multiple input fields, we can use .at(0) to find the input element by index.

(2) Testing Checkbox

For checkbox, assign the value for checked instead of value and then simulate change.

(3) Testing Multi-Select

To test multi-select, you need to set the individual option’s selected property to true. This solution works.

Simulating change on the select element as below doesn’t work.

(4) Testing alert() on Submit

On submit, it will call the alert function with all the input values (see here). To test this logic, we need to mock window.alert function and check if it is called with the correct argument, including all the input values. First, set the state and simulate submit. Then, check to see if mocked alert function is called with the value from the state.

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …