Skip to content

Instantly share code, notes, and snippets.

@baamenabar
Created September 13, 2020 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baamenabar/9e9c5d64a003a85db7cf2d3acc6faa00 to your computer and use it in GitHub Desktop.
Save baamenabar/9e9c5d64a003a85db7cf2d3acc6faa00 to your computer and use it in GitHub Desktop.
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