Site icon Whizzystack

HOW TO TEST YOUR REACT APPS WITH THE REACT TESTING LIBRARY

In this article we will discuss about the automatic testing of written software projects with some types of common testing process. First of all we will build to- do list app by Test-Driven Development (TDD) approach. Here I will use combination of RTL and Jest which were pre- installed in any new project created by Create-React-App (CRA).

At first you need to know the working process of new React project and how to set up and explore it and how to work with yarn package manager (or npm). One should know about Axios and React- Router.

Testing of Code

To hand over a software to the customer you should confirm that whether it satisfy all the specification or not as customer expectance.

Not only at the time of shipping, testing our code is essential for lifetime. There are many reason behind it:

For this reason testing is important throughout lifetime of a project.

Types of Automated Testing

This test verify the unit of our application which works isolately. For example it test particular function for known input and expected output.

It check the system whether up and running or not. For example in a React app we just need to render our main app components which would fairly render the browser.

It check two or more module works together or not. For example server and database work together or not.

The test is done to verify functional specifications of the system.

This test is done by business owner whether for the system verification.

It verify how the system work under load which specify how fast app load in a browser.

Importance of React Testing Library?

The most common React Testing options are Enzyme and React Testing Library (RTL).

RTL

It is very simple that the user need not to care whether you use redux or context for state management. They just fix your app in certain way you want. It just need to usual testing of app.

Advantages

Project Setup

# start new react project and start the server
npx create-react-app start-rtl && cd start-rtl && yarn start

While a project running open separate terminal to run yarn test and then press a. Run all the project in watch mode which indicates that when detect changes it automatically run the file. In terminal picture is looked like

There are some packages for testing specifications like:

Testing And Building The To-Do List Index Page

The components specifications for the system are:

import React from “react”;
import “./App.css”;
export const TodoList = () => {
return (

);
};

import React from “react”;
import axios from “axios”;
import { render, screen, waitForElementToBeRemoved } from “./custom-render”;
import { TodoList } from “./TodoList”;
import { todos } from “./makeTodos”;

describe(“”, () => {
it(“Renders component”, async () => {
render();
await waitForElementToBeRemoved(() => screen.getByText(/Fetching todos/i));

expect(axios.get).toHaveBeenCalledTimes(1);
todos.slice(0, 15).forEach((td) => {
expect(screen.getByText(td.title)).toBeInTheDocument();
});
});
});

The source of to do which can be used in list

const makeTodos = (n) => {
// returns n number of todo items
// default is 15
const num = n || 15;
const todos = [];
for (let i = 0; i < num; i++) {
todos.push({
id: i,
userId: i,
title: `Todo item ${i}`,
completed: [true, false][Math.floor(Math.random() * 2)],
});
}
return todos;
};

export const todos = makeTodos(200);

This function generated complete list, from where it is choosen by true and false.

Testing And Building The Single To-Do Page

import React from “react”;
import { useParams } from “react-router-dom”;

import “./App.css”;

export const TodoItem = () => {
const { id } = useParams()
return (

);
};

The test is finished by adding position where it is expected.

Here we expected to see our name and who created this, but it is not sure about to-do status. Here we can create a switch block, if it is not work we can throw it like an error.

Conclusion

To understand this you need to write tests for React app, no matter how small it is. For better implementation you can use CRA’s testing docs and RTL’s documentation. For small tests make sure that all the components should render. You can continuously add more tests over that time.

We will be happy to answer your questions on designing, developing, and deploying comprehensive enterprise web, mobile apps and customized software solutions that best fit your organization needs.

As a reputed Software Solutions Developer we have expertise in providing dedicated remote and outsourced technical resources for software services at very nominal cost. Besides experts in full stacks We also build web solutions, mobile apps and work on system integration, performance enhancement, cloud migrations and big data analytics. Don’t hesitate to get in touch with us!

Exit mobile version