mercredi 6 novembre 2019

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 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. 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. below is the code i am trying to do and the mocking that I have tried.

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'; which is an axios call:

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

export { fetchBundlesFromApi };

This is the mocking I have tried.

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. Jest/Enzyme Error: "Method 'setState' is only meant to run on a single node. 3 found instead." https://jestjs.io/docs/en/asynchronous

Aucun commentaire:

Enregistrer un commentaire