admin管理员组文章数量:1315979
I made a custom hook, useFileListState, that basically encapsulates the logic to add and remove files from a FileList.
To remove (or add) it, it uses the DataTransfer
constructor, as well as the property files
and the method items.add()
:
function deleteFile(fileToDelete: File) {
setData((prevData) => {
if (!prevData) return null;
const fileToDeleteId = getFileToken(fileToDelete);
const dt = new DataTransfer();
for (let i = 0; i < prevData.length; i++) {
const file = prevData[i];
const id = getFileToken(file);
if (id !== fileToDeleteId) dt.items.add(file);
}
return dt.files;
});
}
The problem is, Jest doesn't recognize the DataTransfer
constructor, so I need to mock it somehow, which I tried to do this way:
class mockDataTransfer {
files: File[] = [];
items = {
add: (file: File) => {
this.files.push(file);
},
};
}
jest.spyOn(global, "DataTransfer").mockImplementation(() => mockDataTransfer as unknown as DataTransfer);
But this fails because Property 'DataTransfer' does not exist in the provided object
.
Can someone help me mocking the DataTransfer
constructor?
本文标签: javascriptHow to mock DataTransfer constructor in JestStack Overflow
版权声明:本文标题:javascript - How to mock DataTransfer constructor in Jest? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741994515a2409772.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论