DEV Community

jamie
jamie

Posted on

Mocha Chai unit test for React state variable change

I want to write a Mocha unit test for the react state variable changes. For example,

 import React, { useState, useEffect, useMemo } from 'react'; const RefillComponent = () => { const [refillResult, setRefillResult] = useState({ status: 'notStarted', successfulIds: [], failedIds: [], }); const onRequestRefills = async () => { const response = await MyAPICall(list); const failedIds = response?.failedIds || []; const successfulIds = response?.successfulIds || []; setRefillResult({ status: failedIds.length > 0 ? 'failed' : 'success', failedIds, successfulIds, }); } }; const failedNotification = () => { return ( <div>API request failed</div> ); }; return ( <div> {(refillResult.status === 'failed') && failedNotification()} </div> ); }; export default RefillComponent; 
Enter fullscreen mode Exit fullscreen mode

I want to write the unit test when refillResult.status === 'failed' the text API request failed should be displayed.

Top comments (0)