如何使用开玩笑和酶在内部使用多个API调用测试使用效果

发布于 2025-02-14 00:50:24 字数 1364 浏览 0 评论 0原文

我想用两个API调用测试使用效果,如下所示。我如何嘲笑API致电或在这种情况下如何监视 Fetchcountry & fetchstates 函数,然后如何

使用jest&酶

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(虚拟实施)

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2025-02-21 00:50:24

实际上,您不需要测试usestate钩子。它是React的一部分,已经由其维护者进行了测试。

让我们回到fetchcountryfetchstate

在您的测试中:

const fetchCountryMock = jest.spyOn(fetchCountry, 'default').mockImplementation();

const wrapper = mount(Component);

await flushPromises(); /* You can write your own implementation or use the library
function flushPromises() {
    return new Promise(resolve => setImmediate(resolve));
}
*/
expect(fetchCountryMock).toHaveBeenCalledWith('Japan');

You actually don't need to test useState hook. It is a part of React and it is already tested by its maintainers.

Let's come back to fetchCountry and fetchState.

In your test:

const fetchCountryMock = jest.spyOn(fetchCountry, 'default').mockImplementation();

const wrapper = mount(Component);

await flushPromises(); /* You can write your own implementation or use the library
function flushPromises() {
    return new Promise(resolve => setImmediate(resolve));
}
*/
expect(fetchCountryMock).toHaveBeenCalledWith('Japan');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文