Mock FileList
I am trying to write a unit test (Angular5) that requires a FileList. I have looked everywhere for any hint at a solution. I am wondering if this is even possible because of the security nature of FileList and my quest has been doomed from the start.
If this is possible any pointers would be greatly appreciated.
Mock FileList
I am trying to write a unit test (Angular5) that requires a FileList. I have looked everywhere for any hint at a solution. I am wondering if this is even possible because of the security nature of FileList and my quest has been doomed from the start.
If this is possible any pointers would be greatly appreciated.
Share Improve this question edited May 6, 2019 at 23:53 asked Mar 26, 2018 at 2:04 user9298624user9298624 5 |1 Answer
Reset to default 19Option 1: Using DataTransfer Constructor
describe('Component', () => {
const getFileList = () => {
const dt = new DataTransfer();
dt.items.add(new File([], 'file.csv'));
return dt.files;
};
it('should mock fileList', () => {
component.fileList = getFileList();
});
});
Option 2: Mocking Filelist with Blob
describe('Component', () => {
const getFileList = () => {
const blob = new Blob([""], { type: "text/html" });
blob["lastModifiedDate"] = "";
blob["name"] = "filename";
const file = <File>blob;
const fileList: FileList = {
0: file,
1: file,
length: 2,
item: (index: number) => file
};
return fileList;
};
it('should mock fileList', () => {
component.fileList = getFileList();
});
});
Happy coding!
DataTransfer
constructor (currently only available in Blink) . See stackoverflow.com/questions/47119426/… for a demo. – Kaiido Commented Mar 26, 2018 at 5:53