我有一个函数,该函数创建一个FileReader
对象,并为它分配load
和error
处理程序。
handleFileSelect(files:ArrayLike<File>):FileReader|null{
console.log("got file upload event: ");
console.log(" image attachment count: ",this.currentImageAttachmentCount);
if(this.currentImageAttachmentCount >= this.maxImageAttachmentCount)
{
console.log("reached max attachment size");
this.showDialog("You can't attach more files",new DialogContext("",""));
return null;
}
console.log("files selected:",files);
console.log("total selected: ",files.length);
for(let i=0;i<files.length;i++)
{
console.log("files name:",files[i].name);
console.log("files object:",files[i])
}
//working with only 1 file at the moment
let file = files[0];
console.log("file at index 0 ",file);
if (files && file) {
console.log("reading file");
let reader:FileReader = new FileReader();
reader.onload = this.handleReaderLoaded.bind(this);
reader.onerror = this.handleFileLoadError.bind(this);
reader.readAsDataURL(file);
this.currentImageAttachmentCount++;
return reader;
} else{
return null;
}
}
我要测试handleFileSelect
是否分配了正确的处理程序。我写的测试用例是
fit('should call assign correct error handler', () => {
let newComponent = component;
console.log("component is ",newComponent);
let file1 = new File(["dd"], "file1",{type:'image/png'});
let reader = newPracticeQuestionComponent.handleFileSelect([file1]);
expect(reader).toBeTruthy();
console.log("reader's onerror ",reader.onerror.name);
console.log("component's onerror ",newComponent.handleFileLoadError.name);
expect(reader.onerror.name).toEqual(newPracticeQuestionComponent.handleFileLoadError.name);
});
我注意到浏览器控制台中的打印没有功能名称。我希望看到handleFileLoadError
。
reading file
reader's onerror
为什么我没有看到handleFileLoadError
?如何测试是否分配了正确的错误处理程序?