Reputation: 348
I am trying to test a component that makes 2 fetch calls inside the used hook:
useEffect(() => {
let viewCodePromise = getCode(id);
viewCodePromise.then(data => {
if (data.success === 1) {
setCode(data.code);
} else {
setError(data.error);
}
});
let getCommentsPromise = getComments(id);
getCommentsPromise.then(data => {
if (data.success === 1) {
setComments(data.comments);
} else {
setError(data.error);
}
});
}, []);
the functions getCode() and getComments() are making the fetch calls. How can I mock tests with jest both fetch calls?
Upvotes: 3
Views: 726
Reputation: 1984
You could use the jest mock function.
const getCode = jest.fn(() =>
Promise.resolve({
success: 1
})
)
const getComments = jest.fn(() =>
Promise.resolve({
success: 1
})
)
Upvotes: 1