Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何模拟在同一函数中进行的多个提取调用_Javascript_Reactjs_Jestjs_Enzyme - Fatal编程技术网

Javascript 如何模拟在同一函数中进行的多个提取调用

Javascript 如何模拟在同一函数中进行的多个提取调用,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,我有一个函数,如下所示,它在同一个函数中执行两个fetch调用 getNames() { var qs = require("qs"); fetch(<URL>, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, body: qs

我有一个函数,如下所示,它在同一个函数中执行两个fetch调用

getNames() {
    var qs = require("qs");
    fetch(<URL>,
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        },
        body: qs.stringify({
          firstName: this.state.firstName,
          lastName: this.state.lastName
        })
      })
      .then(response => response.json()).then((data) => {
        console.log(data)
      });

    var url = new URL(<someURL>)

    fetch(<someURL>).then(response => response.json()).then((data) => {
     ...do something...
      }
     })
      .catch(error => {
        alert("no response");

        console.log(error);
      });
  }
getNames(){
var qs=要求(“qs”);
获取(,
{
方法:“POST”,
标题:{
“内容类型”:“application/x-www-form-urlencoded;charset=UTF-8”
},
正文:qs.stringify({
名字:this.state.firstName,
lastName:this.state.lastName
})
})
.then(response=>response.json()).then((数据)=>{
console.log(数据)
});
var url=newurl()
fetch().then(response=>response.json()).then((data)=>{
…做点什么。。。
}
})
.catch(错误=>{
警报(“无响应”);
console.log(错误);
});
}
我正在用Jest和React上的酶来测试这一点。以上内容属于GetName组件。下面是我的测试用例:

describe('getName', () => {
    const wrapper = shallow(<GetName />).instance();

    beforeEach(() => {
        global.fetch.resetMocks();
    });

    it('positive flow', () => {
        global.fetch.mockResolvedValue(
            new Response(JSON.stringify({data: "mockData"}))
        );
        const state = {
            firstName: "don",
            lastName: "Lee"
        };
        wrapper.setState(state);
        const actualValue = wrapper.getNames();
        expect(actualValue).toBeUndefined();
    });
 });
description('getName',()=>{
const wrapper=shallow().instance();
在每个之前(()=>{
global.fetch.resetMocks();
});
它('正流',()=>{
global.fetch.mockResolvedValue(
新响应(JSON.stringify({data:“mockData”}))
);
常量状态={
名字:“唐”,
姓:“李”
};
包装器。设置状态(状态);
const actualValue=wrapper.getNames();
expect(实际值).toBeUndefined();
});
});
一旦我这样做,我会得到一个错误,该错误
TypeError:body已经用于:undefined

我知道这里的fetch用于POST调用,但是如何确保可以在函数中模拟这两个fetch调用呢

我还尝试了
fetch.mockResponse
fetch.mockResponses
以及
fetch.mockResponseOnce
。它们中似乎没有一个能帮助我不止一次地模仿它们,我在提到的所有函数中都遇到了这个错误


有没有其他方法来模拟这两个提取调用

我所做的是用两个fetch将函数拆分为两个单独的方法,这样我可以分别模拟它们。我所做的是用两个fetch将函数拆分为两个单独的方法,这样我可以分别模拟它们。