DEV Community

Uzeyr OZ
Uzeyr OZ

Posted on

1

Test Driven Development with React

React is a popular JavaScript library for building user interfaces, and using a test-driven development (TDD) approach can help ensure that your code is working correctly and catch bugs early in the development process. This article will show you how to implement TDD in a React app with three buttons: Add, Remove, and Reset.

First, let's create a simple React component to display a count and the three buttons.

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const handleAdd = () => {
    setCount(count + 1);
  };

  const handleRemove = () => {
    setCount(count - 1);
  };

  const handleReset = () => {
    setCount(0);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={handleAdd}>Add</button>
      <button onClick={handleRemove}>Remove</button>
      <button onClick={handleReset}>Reset</button>
    </div>
  );
}

export default Counter;

Enter fullscreen mode Exit fullscreen mode

Next, let's write test scripts to verify that the buttons and count are functioning as expected. We'll use Jest as the testing framework.

import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Counter from './Counter';

describe('Counter', () => {
  it('displays the count', () => {
    const { getByText } = render(<Counter />);
    const countElement = getByText('Count: 0');
    expect(countElement).toBeInTheDocument();
  });

  it('increases the count when the Add button is clicked', () => {
    const { getByText } = render(<Counter />);
    const addButton = getByText('Add');
    fireEvent.click(addButton);
    const countElement = getByText('Count: 1');
    expect(countElement).toBeInTheDocument();
  });

  it('decreases the count when the Remove button is clicked', () => {
    const { getByText } = render(<Counter />);
    const removeButton = getByText('Remove');
    fireEvent.click(removeButton);
    const countElement = getByText('Count: -1');
    expect(countElement).toBeInTheDocument();
  });

  it('resets the count to 0 when the Reset button is clicked', () => {
    const { getByText } = render(<Counter />);
    const resetButton = getByText('Reset');
    fireEvent.click(resetButton);
    const countElement = getByText('Count: 0');
    expect(countElement).toBeInTheDocument();
  });

  it('does not go below 0 when the count is decremented', () => {
    const { getByText } = render(<Counter />);
    const removeButton = getByText('Remove');
    fireEvent.click(removeButton);
    let countElement = getByText('Count: -1');
    expect(countElement).toBeInTheDocument();
    fireEvent.click(removeButton);
countElement = getByText('Count: -2');
expect(countElement).toBeInTheDocument();
fireEvent.click(removeButton);
countElement = getByText('Count: -3');
expect(countElement).toBeInTheDocument();

const addButton = getByText('Add');
fireEvent.click(addButton);
countElement = getByText('Count: -2');
expect(countElement).toBeInTheDocument();
fireEvent.click(addButton);
countElement = getByText('Count: -1');
expect(countElement).toBeInTheDocument();
fireEvent.click(addButton);
countElement = getByText('Count: 0');
expect(countElement).toBeInTheDocument();




Enter fullscreen mode Exit fullscreen mode

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay