Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a mock FileList to test file upload components.
/** list of mock File elements to pass the input[file] */
export function getBrowsedFiles(): FileList {
return fileListFromArray([
mockFileCreator({ name: 'file-one.png', type: 'image/png', size: 234 * 1000 }),
mockFileCreator({ name: 'file-two.gif', type: 'image/gif', size: 56 * 1000 }),
]);
}
export function mockFileCreator({
name = 'file.txt',
size = 1024,
type = 'plain/txt',
lastModified = new Date(),
}) {
const blob = new Blob(['a'.repeat(size)], { type });
blob['lastModifiedDate'] = lastModified;
return new File([blob], name, { type });
}
function fileListFromArray(files: File[]): FileList {
const flMock = files.reduce(
(accumulator, curr, i) => {
accumulator[i] = curr;
return accumulator;
},
{ length: files.length },
) as any;
flMock.item = function(index: number) {
return this[index];
}.bind(flMock);
return flMock as FileList;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment