我正在尝试模拟文件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)] } }));
// ...
});
我不确定要使用第二个参数来保存文件。
答案 0 :(得分:0)
我最终将测试分为两个部分:
首先下降
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);
});