Javascript 用于单元测试的伪文件丢弃事件

Javascript 用于单元测试的伪文件丢弃事件,javascript,angular,jasmine,angular-unit-test,Javascript,Angular,Jasmine,Angular Unit Test,我试图模拟一个fileDrop事件来测试角度指令,但我无法创建DragEvent it('should output file drop when file droped', () => { const file = new File([''], 'dummy.txt'); // got my file right there, want to drop it on "des" des.dispatchEvent(new DragEvent('drop', { dataTr

我试图模拟一个file
Drop
事件来测试角度指令,但我无法创建
DragEvent

it('should output file drop when file droped', () => {
    const file = new File([''], 'dummy.txt'); // got my file right there, want to drop it on "des"
    des.dispatchEvent(new DragEvent('drop', { dataTransfer: { items: [ new DataTransferItem(file)] } }));
    // ...
});

我不确定如何处理第二个参数以将我的文件保存在其中。

我最终将测试分为两部分:

首先是下降

it('should call onFileDrop when there is a drop event', () => {
    spyOn(directive, 'onFileDrop');
    dest.triggerEventHandler('drop', new DragEvent('drop'));
    expect(directive.onFileDrop).toHaveBeenCalled();
});
然后是函数中的处理

it('should output the files that when onFileDrop is called', () => {
    spyOn(directive.fileDrop, 'emit').and.callThrough();
    const file = new File([''], 'dummy.jpg');
    const fileDropEvent = { preventDefault: () => {}, dataTransfer: { files: [file, file, file] }};
    let outputFiles;

    directive.fileDrop.pipe(first()).subscribe(f => outputFiles = f);
    directive.onFileDrop(fileDropEvent);
    expect(directive.fileDrop.emit).toHaveBeenCalled();
    expect(outputFiles.length).toBe(3);
    expect(outputFiles[0]).toBe(file);
});

什么是
指令