Tag: react

Jest: How to mock a single hook from a module

crysfel Programming

Today I needed to write a test for a component that uses a hook from our common library, but at the same time I needed to use other components in the test. Initially I mocked the whole module and got all previous tests to fail, the issue was that I mocked everything and no other component was working as expected. Here's the code I wanted to test. import { Text, File, useToast } from '@my-library/ui'; import axios from 'axios'; export default function Uploader({ userId ) { const showToast = Continue reading

How to test redirections and history state with Jest

crysfel Programming

Testing redirections in jest using react router is not a straight forward process, specially when using hooks. In this tutorial I'm going to show you how to test a redirect, in addition we are going to test the state in the history. The code we will be testing is a button, when clicked we will redirect the user to a new page using react router, but we are also going to set some data into the history state. import React from 'react'; import { useNavigate, useLocation } from 'react-router-dom'; Continue reading