How to Perform Unit Testing in React Components with Examples?

Author: neptune | 25th-Jul-2024
#JavaScript #React.js

Unit testing is crucial in ensuring that individual components of your application work as expected. In React, unit testing can be efficiently performed using tools like Jest and the React Testing Library. This article will guide you through the basics of unit testing React components, complete with an example.


Prerequisites

Before you start, ensure you have the following:


1. Node.js installed.

2. A React application set up (you can use Create React App).

3. Jest and React Testing Library installed.


You can install Jest and React Testing Library with the following command:


    npm install --save-dev jest @testing-library/react @testing-library/jest-dom



Setting Up Testing Environment

Create React App comes with Jest pre-configured, so you can start writing tests without any additional setup. If you are using a custom setup, you may need to configure Jest manually by adding a `jest.config.js` file.


Writing Your First Test

Let's start with a simple React component:


    // src/components/Greeting.js

    import React from 'react';


    const Greeting = ({ name }) => {

        return <h1>Hello, {name}!</h1>;

    };


    export default Greeting;



Now, let's write a unit test for this component:


    // src/components/Greeting.test.js

    import React from 'react';

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

    import '@testing-library/jest-dom/extend-expect';

    import Greeting from './Greeting';


    test('renders greeting message', () => {

        const { getByText } = render(<Greeting name="John" />);

        expect(getByText('Hello, John!')).toBeInTheDocument();

    });



Explanation

1. Importing Dependencies: We import React, the `render` function from React Testing Library, and the `Greeting` component. The `@testing-library/jest-dom/extend-expect` library provides additional matchers for Jest.


2. Writing the Test Case: The `test` function defines a single test case. It takes a string description and a function containing the test logic.


3. Rendering the Component: We use the `render` function from React Testing Library to render the `Greeting` component.


4. Asserting the Output: We use the `getByText` query to find the text content in the rendered output and then assert that it is present in the document using the `toBeInTheDocument` matcher.


Testing User Interactions

Let's consider a more interactive component:

    // src/components/Counter.js

    import React, { useState } from 'react';


    const Counter = () => {

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


        return (

            <div>

                <p>You clicked {count} times</p>

                <button onClick={() => setCount(count + 1)}>Click me</button>

            </div>

        );

    };


    export default Counter;



Now, let's write tests for the `Counter` component:


    // src/components/Counter.test.js

    import React from 'react';

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

    import '@testing-library/jest-dom/extend-expect';

    import Counter from './Counter';


    test('renders counter with initial state', () => {

        const { getByText } = render(<Counter />);

        expect(getByText('You clicked 0 times')).toBeInTheDocument();

    });


    test('increments counter on button click', () => {

        const { getByText } = render(<Counter />);

        const button = getByText('Click me');


        fireEvent.click(button);

        expect(getByText('You clicked 1 time')).toBeInTheDocument();


        fireEvent.click(button);

        expect(getByText('You clicked 2 times')).toBeInTheDocument();

    });



Explanation

1. Rendering the Component: The `render` function renders the `Counter` component.


2. Initial State Check: We assert that the initial state of the counter is 0.


3. Simulating User Interaction: We use the `fireEvent.click` function to simulate a button click and then assert that the counter increments correctly.


Mocking API Calls

For components that make API calls, you can use Jest's mocking capabilities. Here is an example:


    // src/components/User.js

    import React, { useState, useEffect } from 'react';


    const User = () => {

        const [user, setUser] = useState(null);


        useEffect(() => {

            fetch('/api/user')

                .then(response => response.json())

                .then(data => setUser(data));

        }, []);


        if (!user) return <div>Loading...</div>;


        return <div>Hello, {user.name}</div>;

    };


    export default User;



Now, let's write tests for the `User` component:


    // src/components/User.test.js

    import React from 'react';

    import { render, waitFor } from '@testing-library/react';

    import '@testing-library/jest-dom/extend-expect';

    import User from './User';


    beforeEach(() => {

        fetch.resetMocks();

    });


    test('renders user data after fetching', async () => {

        fetch.mockResponseOnce(JSON.stringify({ name: 'John Doe' }));


        const { getByText } = render(<User />);

        expect(getByText('Loading...')).toBeInTheDocument();


        await waitFor(() => expect(getByText('Hello, John Doe')).toBeInTheDocument());

    });



Explanation

1. Mocking Fetch: We mock the fetch API using `jest-fetch-mock`.


2. Asserting Initial Loading State: We assert that the initial state shows a loading message.


3. Waiting for Data: We use `waitFor` to wait for the asynchronous fetch call to complete and then assert that the component renders the fetched data correctly.


Conclusion

Unit testing in React is an essential practice to ensure the reliability and robustness of your components. Using Jest and the React Testing Library, you can write comprehensive tests that cover rendering, user interactions, and even asynchronous operations. By following the examples provided, you can start building a solid suite of tests for your React application, leading to higher code quality and easier maintenance.




Related Blogs
Generate Fibonacci Sequence - JavaScript | Hackerank
Author: neptune | 07th-Apr-2023
#JavaScript #Hackerrank
Write a JavaScript function fibonacciSequence() to generate a FIbonacci sequence...

React: Slideshow App | Fresco Play Hackerrank
Author: neptune | 05th-Nov-2023
#React.js
One interesting project that showcases these qualities is the Slideshow App, a simple yet impactful application that allows users to navigate through a collection of slides...

To Be Or Not To Be | #2704 | LeetCode Solution
Author: neptune | 03rd-Sep-2023
#JavaScript #LeetCode
Write a function that helps developers test their code. It should take in any value and return an object with the following two functions...

Create Your First App in React with Redux | Counter app
Author: neptune | 30th-Mar-2023
#React.js
Creating your first app in React can be a daunting task, but with the right guidance, it can be a fun and rewarding experience. Will guide you to create a counter app in React with redux...

Apply Transform Over Each Element in Array | #2635 | LeetCode Solution
Author: neptune | 05th-Sep-2023
#JavaScript #LeetCode
Given an integer array `arr` and a mapping function `fn`, return a new array with a transformation applied to each element...

Function Composition | #2629 | LeetCode Solution
Author: neptune | 09th-Sep-2023
#JavaScript #LeetCode
Given an array of functions [f1, f2, f3, ..., fn], return a new function fn that is the function composition of the array of functions...

Counter | #2620 | LeetCode Solution
Author: neptune | 02nd-Sep-2023
#JavaScript #LeetCode
Given an integer n, return a counter function. This counter function returns n and then n + 1, n + 2, etc...

React.js vs React Native – What's the Difference?
Author: neptune | 26th-Mar-2023
#React.js
React.js and React Native are both frameworks developed by Facebook for building user interfaces. However, they are not the same and have different use cases...

Counter 2 | #2665 | LeetCode Solution
Author: neptune | 04th-Sep-2023
#JavaScript #LeetCode
Write function 'createCounter' It accept an initial integer 'init' It should return an object with three functions- increment() , decrement(), reset()...

Essential Topics to Master React JS
Author: neptune | 21st-Feb-2024
#React.js
A Comprehensive Guide to Components, State, JSX, Event Handling, Routing, Redux, Hooks, Testing, Performance Optimization, and Server-Side Rendering...

Array Reduce Transformation | #2626 | LeetCode Solution
Author: neptune | 09th-Sep-2023
#JavaScript #LeetCode
Given an integer array `nums` and a reducer function `fn`, and an initial value `init`, return a reduced array...

Chunk Array | #2677 | LeetCode Solution
Author: neptune | 19th-Sep-2023
#JavaScript #LeetCode
Given an array arr and a chunk `size`, return a `chunked` array...

Managing Virtual Environments in React JavaScript Projects
Author: neptune | 28th-Jun-2023
#JavaScript #React.js
Virtual environments are a valuable tool in React JavaScript projects as they allow developers to isolate dependencies, manage package versions, and maintain project consistency...

Add Two Promises | #2723 | LeetCode Solution
Author: neptune | 12th-Sep-2023
#JavaScript #LeetCode
Given two promises `promise1` and `promise2`, return a new `promise`. `promise1` and `promise2` will both resolve with a number...

Arrow Functions in JavaScript | ES6
Author: neptune | 26th-Mar-2023
#JavaScript #React.js
In this article, we will explore the syntax and usage of arrow functions in detail, along with some examples...

Different ways to handle state in React applications
Author: neptune | 21st-Jun-2023
#JavaScript #React.js
This article explores different ways to manage states in React, including local component state, context API, and state management libraries like Redux...

Filter Elements from Array | #2634 | LeetCode Solution
Author: neptune | 06th-Sep-2023
#JavaScript #LeetCode
Given an integer array `arr` and a filtering function `fn`, return a filtered array `filteredArr`...

Is Object Empty | #2727 | LeetCode | JavaScript Solution
Author: neptune | 01st-Sep-2023
#JavaScript #LeetCode
Given an object or an array, return if it is empty...

Opportunities - React Django Developer
Author: neptune | 14th-Apr-2023
#React.js #Django
React Django stack is popular for building web applications. Opportunities for React Django developers in Full Stack, Web, and Software development roles...

Why React Refuses to Die ?
Author: neptune | 01st-Jun-2023
#React.js
React's success stems from addressing UI development challenges, nurturing a vibrant ecosystem, and its demand in the job market. Challenges exist, but React continues to evolve and remain relevant...

😱 How React Kicks Off OOPs ? 😱
Author: neptune | 01st-Jun-2023
#React.js
React kicks off OOPs by replacing inheritance with composition, achieving code reuse and modularity while promoting functional programming...

From REST to GraphQL: The Future of API Design
Author: neptune | 25th-Feb-2024
#JavaScript
Unlike traditional REST APIs, GraphQL provides a more flexible and intuitive approach to data querying and retrieval...

A Guide to Writing Clean, Readable, and Maintainable Code in JavaScript
Author: neptune | 23rd-Feb-2024
#JavaScript
By incorporating these principles into your coding practices, you contribute to creating code that is not only functional but also maintainable and easily understandable by your peers...

Celebrating 10 Years of React: A Decade of Innovation
Author: neptune | 01st-Jun-2023
#React.js
React celebrates its 10th anniversary, revolutionizing frontend development with its innovative concepts, ecosystem growth, and impact on mobile development...

How I Built My Blogging Website Using React, Node.js, and Jamstack Architecture?
Author: neptune | 31st-Jul-2024
#JavaScript #API
Building a blogging website using React, Node.js, and Jamstack architecture was a rewarding experience...

Do you know ! How to manage State in Functional & Class Components in React ?
Author: neptune | 25th-Jul-2024
#JavaScript #React.js
State management in React has evolved significantly with the introduction of Hooks...

Why, What, and When: Understanding Jamstack?
Author: neptune | 05th-Jul-2024
#JavaScript #API
Jamstack represents a modern approach to web development that addresses many of the challenges faced by traditional architectures...

How to Get Started with Jamstack: A Comprehensive Guide?
Author: neptune | 05th-Jul-2024
#JavaScript #API
Getting started with Jamstack involves choosing the right tools, setting up a structured development environment...

View More