Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.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 如何使用Jasmine(Angular)下载文件进行单元测试?_Javascript_Angular_Jasmine - Fatal编程技术网

Javascript 如何使用Jasmine(Angular)下载文件进行单元测试?

Javascript 如何使用Jasmine(Angular)下载文件进行单元测试?,javascript,angular,jasmine,Javascript,Angular,Jasmine,我有一个Angular 6应用程序,它有一个“导出到CSV”按钮,单击该按钮时,会在组件上运行一个方法,生成要下载的CSV文本。然后使用以下代码下载该文件: const tempLink = document.createElement('a'); tempLink.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent); tempLink.target = '_blank'; tempLink.download = 'expo

我有一个Angular 6应用程序,它有一个“导出到CSV”按钮,单击该按钮时,会在组件上运行一个方法,生成要下载的CSV文本。然后使用以下代码下载该文件:

const tempLink = document.createElement('a');
tempLink.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
tempLink.target = '_blank';
tempLink.download = 'export.csv';
tempLink.click();
我如何在不触发实际下载的情况下单击“导出到CSV”按钮进行单元测试

唯一的方法是在HTML中创建链接(不是动态的),然后在链接的单击事件上使用Jasmine spy吗?

您可以将(spyOn,spyOnProperty,Jasmine.createSpy,Jasmine.createSpyObj)与来监视和模拟行为结合使用

在这种情况下,您可以模拟
document.createElement()
以返回一个spy对象,您可以使用该对象验证是否设置了正确的属性,并调用
click()

以下是一个工作示例:


示例.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-example',
  template: '<div><button (click)="download()">Download Csv</button></div>'
})
export class ExampleComponent implements OnInit {

  constructor() { }

  ngOnInit() { }

  download() {
    const csvContent = this.generateCsv();
    const tempLink = document.createElement('a');
    tempLink.href = 'data:text/csv;charset=utf-8,' + encodeURI(csvContent);
    tempLink.target = '_blank';
    tempLink.download = 'export.csv';
    tempLink.click();
  }

  private generateCsv() {
    return 'ID,Name\n1,abc\n2,def\n';
  }

}

@太棒了!我得到“TypeError:未能在“节点”上执行“appendChild”:参数1不是“节点”类型。有什么想法吗?@MitchellBrooks我也有同样的错误,就我而言,这是因为我在测试的函数中有一个document.body.appendChild。我通过添加一个
spyOn(document[“body”],“appendChild”)解决了这个问题
import { ExampleComponent } from './example.component';

describe('ExampleComponent', () => {

  it('should download the csv file', () => {
    // create spy object with a click() method
    const spyObj = jasmine.createSpyObj('a', ['click']);
    // spy on document.createElement() and return the spy object
    spyOn(document, 'createElement').and.returnValue(spyObj);

    const comp = new ExampleComponent();
    comp.download();

    expect(document.createElement).toHaveBeenCalledTimes(1);
    expect(document.createElement).toHaveBeenCalledWith('a');

    expect(spyObj.href).toBe('data:text/csv;charset=utf-8,ID,Name%0A1,abc%0A2,def%0A');
    expect(spyObj.target).toBe('_blank');
    expect(spyObj.download).toBe('export.csv');
    expect(spyObj.click).toHaveBeenCalledTimes(1);
    expect(spyObj.click).toHaveBeenCalledWith();
  });

});