Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.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 在React组件中测试API调用_Javascript_Reactjs_Unit Testing_Jestjs - Fatal编程技术网

Javascript 在React组件中测试API调用

Javascript 在React组件中测试API调用,javascript,reactjs,unit-testing,jestjs,Javascript,Reactjs,Unit Testing,Jestjs,我想在我的React组件中测试API调用,并通过上网尝试了几种方法 MyComponent: export const API = { getData() { return fetch(localStorage.blobUrl).then(res => res.blob()) } } const downloadArchiveFile = () => { localStorage.setItem('blobUr

我想在我的React组件中测试API调用,并通过上网尝试了几种方法

MyComponent:

    export const API = {
      getData() {
        return fetch(localStorage.blobUrl).then(res => res.blob())
      }
    }

    const downloadArchiveFile = () => {
    localStorage.setItem('blobUrl',bloburl);
    API.getData().then((blob) => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = url;
      // the filename you want
      a.download = templateName;
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
    })
  };
MyComponent.test.js

it('api call', async () => {
        const fakeUserResponse = {
            'myResponse': {
                'a': '1',
                'b': '2' }
        };
        var apiFunc = jest.spyOn(API, 'getData').mockImplementationOnce(() => {
            return Promise.resolve({
                json: () => Promise.resolve(fakeUserResponse)
            })
        })
    })
测试用例正在通过。但在看到代码覆盖率时,此代码未被覆盖。

请参阅代码覆盖率报告

我所指的链接:

    export const API = {
      getData() {
        return fetch(localStorage.blobUrl).then(res => res.blob())
      }
    }

    const downloadArchiveFile = () => {
    localStorage.setItem('blobUrl',bloburl);
    API.getData().then((blob) => {
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.style.display = "none";
      a.href = url;
      // the filename you want
      a.download = templateName;
      document.body.appendChild(a);
      a.click();
      window.URL.revokeObjectURL(url);
    })
  };


您使用哪个库来测试react组件<代码>反应测试库或
?我使用了酶库