有一个输入类型文件元素。在角度文件上载多次期间,该值不会被清除。因此,使用普通的JavaScript dom操作手动清除它。
以下是代码:
function removeFromQueue(item) {
vm.uploads.uploader.removeFromQueue(item);
// Clearing input file field for re-uploading
if(!vm.uploadFile) {
document.getElementById('upload-file-' + vm.type).value = null;
}
}
在这种情况下,无法模拟document.getElementById
,因此使用来自单元测试用例的vm.uploadFile
未定义变量来控制它是错误的。如何在这里模拟dom元素?
答案 0 :(得分:7)
您应该能够spyOn
document.getElementById
并返回有用的属性(例如value
)。像这样,
spyOn(document, "getElementById").and.callFake(function() {
return {
value: 'test'
}
});
如果你愿意,你可以期待它被召唤,
expect(document.getElementById).toHaveBeenCalledWith('...')