DEV Community

Cover image for Testing the Waters: A Comedic Take on TDD with React Testing Library
rardooba
rardooba

Posted on

Testing the Waters: A Comedic Take on TDD with React Testing Library

Test-Driven Development (TDD) is a software development process that emphasizes writing tests before writing the actual code. This process helps developers to build high-quality, well-designed software that is easy to maintain and less prone to bugs. In this article, we will cover the process of setting up TDD with Jest on a modal component.

Jest is a popular JavaScript testing library that makes it easy to write and run tests. It is well-suited for TDD and provides a wide range of features for testing React components, including snapshot testing and mocking.

Here are the steps to set up TDD with Jest on a modal component:

  1. Install React & Testing library : Projects created with Create React App have out of the box support for React Testing Library.

  2. Create a simple modal component.

import React from 'react';

const Modal = ({isOpen, onClose}) => {
  return (
    <div>
      {isOpen && (
        <div>
          <p>This is a modal</p>
          <button onClick={onClose}>Close</button>
        </div>
      )}
    </div>
  );
};

export default Modal;
Enter fullscreen mode Exit fullscreen mode
  1. Create a test file: Create a new file in the same directory as your modal component and call it Modal.test.js. This is where you will write your tests.

  2. Write your first test: To write your first test, you will need to import the modal component and Jest's render method. The render method will allow you to render the component in a testing environment and interact with it.

import React from 'react';
import { render } from '@testing-library/react';
import Modal from './Modal';

describe('Modal', () => {
  it('renders the modal when it is open', () => {
    const { getByText } = render(<Modal isOpen />);
    expect(getByText('This is a modal')).toBeInTheDocument();
  });
});
Enter fullscreen mode Exit fullscreen mode
  1. Run the test: To run the test, you can use the following command in your terminal:
npm run test
Enter fullscreen mode Exit fullscreen mode
  1. Write additional tests: Now that you have a basic test in place, you can write additional tests to cover all the functionality of your modal component. For example, you can write a test to ensure that the modal is not rendered when it is not open, or that the close button works as expected.

  2. Implement the modal component: With your tests in place, you can now implement the modal component with confidence. As you write the code, you can run the tests to ensure that everything works as expected.


By following these steps, you will have set up TDD with Jest on your modal component. This process will help you to write better, more maintainable code and reduce the risk of bugs. Additionally, as your component evolves, you can continue to write tests to ensure that it continues to work as expected.

And that's it ! See U next level Doobs !

Top comments (0)