I have a component where inside useEffect
hook calls a function that is imported from another file.
How I can mock this function in tests?
Also, how can I check that useEffect
is called in the component?
Minimal code:
//utils.js
export const doSomething = async () => {
return await fetch('url')
}
//Component.js
import React, {useEffect, useState} from 'react';
import {CardImg} from 'reactstrap';
import {doSomething} from "../../services/utils";
const Component = ({type}) => {
const [answer, setAnswer] = useState('');
useEffect(() => {
doSomething(type)
.then(res => setAnswer(res))
}, [type]);
if (!answer) {
return <span>Loading ...</span>;
}
return <span>{answer}</span>
};
export default Component;