我正在尝试使用
Dropzone.js
将文件转换为Base64的方法进行测试。我的方法可以工作,但是测试FileReader
很困难。实际上,我创建了一个测试(见下文)来调用FileReader
,但是我无法进入onload
属性。我想测试onload
属性的内部。感谢您的回答。
我的代码覆盖率
我的组件
fileTransform(event, action): void {
const reader = new FileReader();
reader.onload = (evt) => {
const file = evt.target['result'];
if (action === 'add') {
this.filesDrop.push(file);
}
if (action === 'remove') {
const index = this.filesDrop.indexOf(file);
if (index > -1) {
this.filesDrop.splice(index, 1);
}
}
};
reader.readAsDataURL(event);
}
我的测试
const file = {
'upload': {
'progress': 0,
'total': 17343,
'bytesSent': 0,
'filename': 'TEST.jpeg'
},
'type': 'images/jpeg',
'width': 350,
'height': 200,
'size': 17343,
'name': 'TEST.jpeg',
'dataURL': 'data:image/jpeg;base64, FOO'
};
const fileFR = {
'bubbles': false,
'returnValue': true,
'target': {
'readyState': 2,
'result': 'data:image/jpeg;base64, FOO',
'onload': 'bar'
}
};
it('should fileTransform()', () => {
spyOn(<any>window, 'FileReader').and.returnValue({
readAsDataURL: function() {},
onload: function() {}
});
component.fileTransform(fileFR, 'add');
expect(FileReader).toHaveBeenCalled();
});