简体   繁体   English

在 promise API 调用 jest 中测试 then()

[英]Test then() inside of a promise API call jest

I am trying to test a function which is an api call / promise so I can check state inside then using jest but cant seem to figure out what to do. I am trying to test a function which is an api call / promise so I can check state inside then using jest but cant seem to figure out what to do. I have tried mocking the file and the function to return a promise but getting an error TypeError: (0, _dataAccess.fetchBundlesFromApi) is not a function I've tried following the docs on jests website and also the many different answers from stack overflow but none seem to work. I have tried mocking the file and the function to return a promise but getting an error TypeError: (0, _dataAccess.fetchBundlesFromApi) is not a function I've tried following the docs on jests website and also the many different answers from stack overflow but似乎没有一个工作。 Here is the code I want to test.这是我要测试的代码。 I want to be able to call that and then say if okay check state or if error do something else.我希望能够调用它,然后说是否可以检查 state 或者如果错误执行其他操作。 below is the code i am trying to do and the mocking that I have tried.下面是我正在尝试执行的代码和我尝试过的 mocking。

getLatestPrices = params => {
    const { updateBundles } = this.props;

    fetchBundlesFromApi(params)
      .then(({ data: { bundles } }) => {
        updateBundles(bundles);
        this.setState({ showUpdatingPrices: false });
        window.TemplateCalculator.reload();
      })
      .catch(() => goToUrl(bundlesUrl));
  };`

fetchBundlesFromApi is import { fetchBundlesFromApi } from '../../../../dataAccess'; fetchBundlesFromApiimport { fetchBundlesFromApi } from '../../../../dataAccess'; which is an axios call:这是一个 axios 调用:

const fetchBundlesFromApi = params => axios(`${bundleRoute}/bundles${params}`);

export { fetchBundlesFromApi };

This is the mocking I have tried.这是我试过的mocking。

jest.mock('../../../../dataAccess', () => ({
      fetchBundlesFromApi: new Promise(resolve => resolve({ data: mockBundles })),
    }));

I have also tried these websites: https://binarapps.com/blog/test-ajax-calls-in-react-component-lifecycle .我也尝试过这些网站: https://binarapps.com/blog/test-ajax-calls-in-react-component-lifecycle Jest/Enzyme Error: "Method 'setState' is only meant to run on a single node. 3 found instead." Jest/Enzyme 错误:“方法 'setState' 仅用于在单个节点上运行。找到了 3 个。” https://jestjs.io/docs/en/asynchronous https://jestjs.io/docs/en/asynchronous

I worked out I had to import my api call function like so:我发现我必须像这样导入我的 api 调用 function:

import { fetchBundlesFromApi } from '../../../../dataAccess';

As that function was using axios I had to mock that.由于 function 正在使用 axios 我不得不嘲笑它。 I did that like so:我是这样做的:

jest.mock('axios', () => jest.fn(() => Promise.resolve({ data: mockBundles })));

The in my test I made it async and I could await that function response.在我的测试中,我让它异步,我可以等待 function 响应。

const fetchedBundles = await fetchBundlesFromApi(
  'params',
);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM