Created
September 13, 2020 12:02
-
-
Save baamenabar/9e9c5d64a003a85db7cf2d3acc6faa00 to your computer and use it in GitHub Desktop.
Create a mock FileList to test file upload components.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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