测试Javascript文件下载实用程序函数

测试Javascript文件下载实用程序函数,javascript,unit-testing,file,jestjs,sinon,Javascript,Unit Testing,File,Jestjs,Sinon,我已经编写了实用程序函数来下载以路径为输入的文件,并根据路径下载名为的文件。这是: static downloadFile(path) { return fetch(path) .then(resp => resp.blob()) .then(blob => { const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href =

我已经编写了实用程序函数来下载以路径为输入的文件,并根据路径下载名为的文件。这是:

static downloadFile(path) {
  return fetch(path)
  .then(resp => resp.blob())
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    const fileName = path.includes('file1') ? 'file.csv' : 'file2.csv';
    a.setAttribute('download', fileName);
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
  })
 }
下面是我的测试功能,它不工作。帮我找出我做错了什么

function jsonOk (body) {
  var mockResponse = new window.Response(JSON.stringify(body), {
    status: 200,
    headers: {
      'Content-type': 'application/json',
      'Accept': 'text/csv'
    }
  });

  return Promise.resolve(mockResponse);
}

const MOCK_JSON = {
  'key1' : 'value1'
};

describe('Test utils', () => {
  beforeEach(() => {
      let stub = sinon.stub(window, 'fetch');
      stub.onCall(0).returns(jsonOk(MOCK_JSON));
   });
  afterEach(() => {
      window.fetch.restore();
   });
  it('test csv download', () => {
    const link = {click: sinon.spy();}
    global.URL.createObjectURL = sinon.fake.returns('https://something.com');
    global.URL.revokeObjectURL = sinon.fake();
    global.Blob = function(content, options) {
       return {content, options};
     };
    sinon.Spy(document, 'createElement', () => link);
    // my actual fn call
    Utils.downloadFile('https://something.com/file1');
    expect(link.download).to.equal('file1.csv'); // Chai check
    expect(link.click).should.have.callCount(1);
  });
});

谢谢你不工作是什么意思?有什么错误吗。显示“expect”失败,因为下载/单击未定义。当我调试它时,控件并没有进入第二个“then”块中,实际的文件下载代码出现在该块中。