【发布时间】:2018-02-24 17:34:16
【问题描述】:
我尝试使用 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();
});
【问题讨论】:
标签: javascript angular jasmine dropzone.js