How to provide mock files to change event of <input type='file'> for unit testing

前端 未结 4 430
死守一世寂寞
死守一世寂寞 2021-01-03 21:21

I\'m having difficulties with a unit test in which I want to verify the processing of a file, which would usually be selected in the view via

4条回答
  •  一向
    一向 (楼主)
    2021-01-03 21:51

    Here is an example spec for input file/image using angular2+.

    it('should call showError on toastService Api on call of onSaveOfImage() method', () => {
    
        spyOn(component.commonFacade.fileIOApi, 'uploadFile');
        let file = new File([new ArrayBuffer(2e+5)], 'test-file.jpg', { lastModified: null, type: 'image/jpeg' });
        let fileInput={ files: [file] };
        component['onSaveOfImage'](fileInput,"",null,"","");
        expect(component.commonFacade.fileIOApi.uploadFile).toHaveBeenCalledTimes(1);
        expect(component.uploadedFileData).toBeUndefined();
        expect(component.commonFacade.employeeApi.toastService.showError).toHaveBeenCalledTimes(1);
      })
    

提交回复
热议问题