Back to Course

Learn React

0% Complete
0/0 Steps
Lesson 28 of 32
In Progress

Writing and Running Tests for React Components

Testing is an essential part of the development process. It helps to ensure that your code is working as intended and that it is reliable. In this article, we will cover how to set up and use Jest and Enzyme to test React components.

Setting Up Jest and Enzyme

Jest is a JavaScript testing framework that is often used with React. It is fast and easy to use, making it a popular choice for testing React components. Enzyme is a library that makes it easier to test React components. It provides a way to render components, simulate events, and make assertions about the output.

To set up Jest and Enzyme, you will need to install them as dependencies in your project. You can do this by running the following command in your terminal:

npm install --save-dev jest enzyme enzyme-adapter-react-16

Next, you will need to create a setup file for Enzyme. Create a file called setupTests.js in the root of your project with the following code:

import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

Finally, update your package.json file to include the following lines in the scripts section:

"test": "jest",
"test:watch": "jest --watch"

Writing and Running Tests for React Components

Now that you have Jest and Enzyme set up, you are ready to start writing tests for your React components. To do this, you will need to create a __tests__ directory in your project. This is where you will store your test files.

Let’s create a test for a simple Button component. Create a file called Button.test.js in the __tests__ directory and add the following code:

import React from 'react';
import { shallow } from 'enzyme';
import Button from '../Button';

describe('Button', () => {
  it('renders the text of the button', () => {
    const wrapper = shallow(<Button>Click me</Button>);
    expect(wrapper.text()).toBe('Click me');
  });
});

This test uses the shallow method from Enzyme to render the Button component. It then makes an assertion about the text of the button using the text method and the toBe matcher from Jest.

To run this test, you can use the test script that you set up earlier. Simply run the following command in your terminal:

npm run test

Conclusion

In this article, we learned about the importance of testing in React and how to set up Jest and Enzyme for testing our React components. We also learned about the different types of tests we can write for our components, including snapshot tests, rendering tests, and behavior tests. By following these steps and best practices, we can ensure that our React applications are reliable and maintainable over time.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Write a snapshot test for the Welcome component from the previous example.

import React from 'react';
import { shallow } from 'enzyme';
import Welcome from './Welcome';

it('renders correctly', () => {
  const wrapper = shallow(<Welcome />);
  expect(wrapper).toMatchSnapshot();
});

Write a rendering test for the Welcome component to check that it renders the correct text.

import React from 'react';
import { shallow } from 'enzyme';
import Welcome from './Welcome';

it('renders the correct text', () => {
  const wrapper = shallow(<Welcome />);
  expect(wrapper.text()).toEqual('Welcome to our app!');
});

Write a behavior test for the Button component to check that it calls the onClick prop when clicked.

import React from 'react';
import { shallow } from 'enzyme';
import Button from './Button';

it('calls the onClick prop when clicked', () => {
  const onClick = jest.fn();
  const wrapper = shallow(<Button onClick={onClick} />);
  wrapper.simulate('click');
  expect(onClick).toHaveBeenCalled();
});

Write a test for the Form component to check that it calls the onSubmit prop with the correct form data when submitted.

import React from 'react';
import { shallow } from 'enzyme';
import Form from './Form';

it('calls the onSubmit prop with the correct form data when submitted', () => {
  const onSubmit = jest.fn();
  const wrapper = shallow(<Form onSubmit={onSubmit} />);
  wrapper.find('form').simulate('submit', { preventDefault: () => {} });
  expect(onSubmit).toHaveBeenCalledWith({
    email: '',
    password: '',
  });
});

Write a test that checks that a component’s state is correctly updated when a button is clicked.

import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from './MyComponent';

describe('MyComponent', () => {
  it('should update state when button is clicked', () => {
    const wrapper = shallow(<MyComponent />);
    expect(wrapper.state('count')).toEqual(0);
    wrapper.find('button').simulate('click');
    expect(wrapper.state('count')).toEqual(1);
  });
});