我最终将测试分为两部分:
首先是滴
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);
});