简体   繁体   English

使用Jest / Enzyme测试componentDidMount中的多个提取

[英]Test multiple fetches in componentDidMount with Jest/Enzyme

I am starting to learn more about testing for React using Jest/Enzyme and I am trying to learn how to test api calls that use fetch. 我开始学习有关使用Jest / Enzyme测试React的更多信息,并且尝试学习如何测试使用fetch的api调用。

I have a fetch call in a componentDidMount() method and I am able to test that correctly. 我在componentDidMount()方法中有一个访存调用,并且能够正确测试它。 In the beforeEach() method, I have a window.fetch method that creates the mock fetch. beforeEach()方法中,我有一个window.fetch方法来创建模拟提取。 Here is an example: 这是一个例子:

beforeEach(() => {
mockData = /* mock data */
// mock fetch call  
window.fetch = jest.fn().mockImplementation(() => Promise.resolve({
  json: () => Promise.resolve({
    data: mockData,
  })
}));
component = shallow(<Component />);
});

However, I have another component that has two fetch calls in the componentDidMount() method, and I'm wondering how I can mock each fetch call individually. 但是,我有另一个组件在componentDidMount()方法中具有两个fetch调用,并且我想知道如何分别模拟每个fetch调用。 Is it possible to have two window.fetch calls or somehow pass in a url for the fetch? 是否可以有两个window.fetch调用或以某种方式传递URL以进行提取?

I am still new to this, so any help would be appreciated! 我对此还很陌生,因此不胜感激!

Thanks! 谢谢!

You can use fetch-mock library to mock each request separately. 您可以使用fetch-mock库分别模拟每个请求。 Suppose you're requesting 2 URLs and you want to return mockData in the request to the first URL, and just 404 status in the second one. 假设您要请求2个URL,并且要将请求中的mockData返回到第一个URL,而在第二个URL中仅返回404状态。 Your example code mocked with fetch-mock may look like this: 使用fetch-mock模拟的示例代码可能如下所示:

 import fetchMock from "fetch-mock"; beforeEach(() => { mockData = { /* mock data */ }; fetchMock.mock("/first/url", mockData); fetchMock.mock("/second/url", 404); component = shallow(<Component />); }); /* You should also restore the original fetch in afterEach */ afterEach(() => { fetchMock.restore(); } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 componentDidMount中的jest酶测试方法 - jest enzyme test method in componentDidMount 笑话和酵素| 在componentDidMount中测试PropTypes函数 - Jest & Enzyme | Test PropTypes Function in componentDidMount 如何使用 Jest 和 Enzyme 测试 componentDidMount() 中的方法? - How to test a method in componentDidMount() with Jest and Enzyme? 使用 jest/enzyme 在 React 上测试 ComponentDidMount 失败 - Test Failing in ComponentDidMount on React using jest/enzyme ComponentDidMount() 中 Axios 请求的 Jest/酶单元测试 - Jest/Enzyme Unit Test on Axios Requests in ComponentDidMount() 在componentDidMount中开玩笑的酶测试方法失败,但控制台显示它正在工作 - jest enzyme test method in componentDidMount fails but console shows that it is working 使用玩笑/酶测试调用setState的异步componentDidMount - Testing async componentDidMount that calls setState, with jest/enzyme 如何为 componentDidMount() 编写单元测试用例并导出默认 connect(null, updateProps)(<componentname> ) 在 React 中使用 JEST/酶?</componentname> - How to write unit test case for componentDidMount() and export default connect(null, updateProps)(<ComponentName>) with JEST/Enzyme in React? 测试使用 Jest 和 Enzyme 更改状态的异步 componentDidMount - Testing asynchronous componentDidMount that changes state with Jest and Enzyme 当 Component.prototype 返回 undefined 时,如何使用 jest/enzyme 对 componentDidMount 进行单元测试? - How to unit test componentDidMount using jest/enzyme when Component.prototype returns undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM