用于单元测试的假文件删除事件

Ced*_*Ced 5 javascript jasmine angular angular-unit-test

我正在尝试模拟一个文件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)] } }));
    // ...
});
Run Code Online (Sandbox Code Playgroud)

我不确定如何处理第二个参数才能将我的文件放在那里。

小智 8

不确定这是否仍然相关,但我今天遇到了这个问题并解决了。只是将其留在这里,以防其他人遇到同样的问题。

这是我试图在指令内测试的方法

@HostListener('drop', ['$event'])
onDrop(evt: any) {
    evt.preventDefault()
    evt.stopPropagation()
    this.fileOver = false
    const files = evt.dataTransfer.files
    if (files.length > 0) {
        this.fileDropped.emit(files)
    }
}
Run Code Online (Sandbox Code Playgroud)

我根据 Angulars 文档创建了测试套件,创建了一个假组件,并将指令添加到了 div 元素中。

@Component({
  template: `<div id="file-drop-area" filedrop (fileDropped)="handleDrop($event)"></div>`
})
class TestFileDropComponent {
    handleDrop(files: FileList) {}
}

describe('FileDropDirective', () => {
    let directive: FileDropDirective
    let component: TestFileDropComponent
    let fixture: ComponentFixture<TestFileDropComponent>;
    beforeEach(async () => {
        await TestBed.configureTestingModule({
          declarations: [ TestFileDropComponent, FileDropDirective ]
        })
        .compileComponents();
     });

     beforeEach(() => {
         fixture = TestBed.createComponent(TestFileDropComponent);
         component = fixture.componentInstance

         fixture.detectChanges();
     })
})
Run Code Online (Sandbox Code Playgroud)

然后,我创建了一个新的 DataTransfer 实例,将我的假文件推送到 items 数组,并将 DataTransfer 实例传递给随后调度的事件。

    it('should verify the directive recognises a drop event', () => {
        spyOn(component, 'handleDrop')

        const el: HTMLElement = fixture.nativeElement
        const fileDropArea: HTMLDivElement = el.querySelector('#file-drop-area')!
        const file = new File([''], 'dummy.txt')
        
        const dataTransfer = new DataTransfer()
        dataTransfer.items.add(file)

        const event = new DragEvent("drop", { dataTransfer })
        fileDropArea.dispatchEvent(event)
        fixture.detectChanges()
        
        expect(component['handleDrop']).toHaveBeenCalledWith(dataTransfer.files)
    })
Run Code Online (Sandbox Code Playgroud)

  • IMO 这应该被标记为问题的答案。 (2认同)

Ced*_*Ced 4

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

首先是掉落

it('should call onFileDrop when there is a drop event', () => {
    spyOn(directive, 'onFileDrop');
    dest.triggerEventHandler('drop', new DragEvent('drop'));
    expect(directive.onFileDrop).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)

然后在函数中进行处理

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);
});
Run Code Online (Sandbox Code Playgroud)