I want to test useEffect with two api calls as shown below. How can I mock api call or in this case how can I spy on fetchCountry & fetchStates functions and then how to test setCountry and setStates functions
using Jest & Enzyme
import React, { useEffect, useState } from "react";
import fetchCountry from "../fetchCountry";
import fetchState from '../fetchState';
const App = () => {
const [country, setCountry] = useState();
const [state, setStates] = useState();
useEffect(() => {
loadData()
}, []);
const loadData = async()=>{
const country = await fetchCountry('Japan');
const states = await fetchState(country.code)
setCountry(country);
setStates(states)
}
fetchCountry.js (dummy implementation)
const fetchCountry = (countryName) => {
const requestOptions = {
method: "GET",
headers: { "Content-Type": "application/json" }
};
return fetch(`https://restcountries.com/v2/name/${countryName}`, requestOptions).then(res=>res.ok && res.json());
};
export default fetchCountry;
fetchState.js
const fetchState = (countryCode) => {
const requestOptions = {
method: "GET",
headers: { "Content-Type": "application/json" }
};
return fetch(`https://restcountries.com/v2/name/${countryCode}`, requestOptions).then(res=>res.ok && res.json());
};
export default fetchState;