avatarJen-Hsuan Hsieh (Sean)

Summary

This article provides a comprehensive guide on testing Redux components with different middleware, such as Redux-thunk and Redux-saga, within a Next.js project using Jest and Enzyme.

Abstract

The article titled "Test Components in the Next.js- Part 2" is a technical tutorial aimed at developers looking to achieve 100% unit test coverage for Redux components in Next.js/React projects. It outlines the necessary steps for setting up a test environment, including the installation of packages like redux-mock-store, @testing-library/react, and @testing-library/jest-dom. The author, Sean, explains how to verify reducers, actions, and sagas, as well as how to test React components, with a focus on the differences between Redux-thunk and Redux-saga middleware. The article emphasizes the importance of testing for correct state management and provides code examples and test cases for both middlewares. Additionally, it touches on the aspects of Redux architecture that do not require testing, such as types, states and selectors, and the store configuration. The article concludes with a call for feedback and shares resources for further learning, including the author's LinkedIn profile, a Facebook page, and a side project called Daily Learning.

Opinions

  • The author believes that understanding how to test Redux components is crucial for global state management in Next.js/React projects.
  • Sean suggests that there is no need to test certain aspects of the Redux architecture, such as types and store configuration, as they are straightforward and do not affect the application's logic.
  • The article conveys that Redux-saga's use of generators and yield for handling asynchronous operations provides an advantage over Redux-thunk in terms of testability.
  • The author encourages the use of data-testid for easier React component testing and recommends using the toEqual and toHaveTextContent matchers for assertions.
  • Sean provides a personal reflection on the importance of feedback and continuous learning, offering links to additional resources and his own work for those interested in expanding their knowledge.

Test Components in the Next.js- Part 2. Test Redux Components in different middlewares (Redux-thunk and Redux-saga)

Introduction

For achieving the target of 100% coverage for the unit test, a part of the basic work is to learn how to test Redux components which are the most commonly used for the global states managements in the Next.js/React project.

In this article, we will learn how to test Redux components with different middleware. Assume that you have known how to use Redux and Jest. You can also read the following articles for the pre-required knowledge.

This article contains the following topics

  • Preparations
  • Verify reducers
  • Verify actions
  • Verify saga and API
  • Verify the React component
  • Rest components

Preparations

1. Environment setting and packages installation

Set up the test environment for the Next.js project with Jest and Enzyme from the following article.

There are a few packages we have to install for testing Redux components.

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

2. Create components for Redux

The middlewares most commonly used for Redux are Redux-thunk and Redux-saga. Feel free to review the difference between them if you are not so familiar with them

3. Create the test case

  • Create Redux.test.js in the __test__folder
Copy right@A Layman

4. Clones the test cases by the npx command (optional)

  • You can use the following npx command to create a default project folder for the Next.js project which also provides some test cases.
  • The report for React-thunk
Copy right@A Layman
  • The report for React-saga
Copy right@A Layman

Verify reducers

There is an example code in two different middlewares.

Copy right@A Layman

Add the code in the Redux.test.js. It is not too much difference in the reducer between Redux-saga and Redux-thunk for the component or test cases.

We can create two test cases for the single action, one is for the initial states, another one is for testing the states after triggering the action. We expect that the reducer will return the correct state and they can be compared by the following code.

expect().toEqual()
Copy right@A Layman

Verify actions

There is an example code in two different middlewares.

Copy right@A Layman

Add the code in the Redux.test.js. The difference in the action between Redux-saga and Redux-thunk is that the first one will return an object and the second one will modify the state.

We also compare the outputs of the two sides but for different expectations.

Copy right@A Layman

Verify saga and API

There is an example code in two different middlewares.

Copy right@A Layman
Copy right@A Layman

Create Saga.test.js in the __test__folder. The advantage of Redux-saga compared to Redux-thunk is that the saga uses a generator and yield so we can verify it step by step.

Copy right@A Layman

Verify the React component

There is an example code in two different middlewares.

Copy right@A Layman

Create Saga.test.js in the __test__folder. It’s easy to test the component by getting a specific text to verify and it’s important to add data-testid in the component before testing. We expect that it will return the correct text and they can be compared by

expect().toHaveTextContent()
Copy right@A Layman

Rest components

Types (won’t test it)

This file is only used to export constants we need so I believe there is no needs to test.

Copy right@A Layman

States and selectors (won’t test it)

This file is only used to export the initial state and the selector function.

Copy right@A Layman

Store (won’t test it)

Copy right@A Layman

References

Summary

Thanks for your patient. I am Sean. I work as a software engineer.

This article is my note. Please feel free to give me advice if any mistakes. I am looking forward to your feedback.

  • The Facebook page for articles
  • The latest side project: Daily Learning

Related topics

How to use the two-way binding in Knout.js and ReactJS?

Learn how to use SignalR to build a chatroom application

My reflection of :

IT & Network:

Database:

Software testing:

Debugging:

DevOps:

Software Development
Software Testing
React
JavaScript
Jest
Recommended from ReadMedium
avatarSonika | @Walmart | FrontEnd Developer | 10 Years
Web Page Performance | Core Web Vitals | LightHouse

Topics Covered::

13 min read