Skip to content

Commit

Permalink
fix(upload): implement iterable on input.files (#603)
Browse files Browse the repository at this point in the history
  • Loading branch information
ph-fritsche authored Mar 19, 2021
1 parent 4f5b3ec commit 9f6355b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/__tests__/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,19 @@ test('should not trigger input event when selected files are the same', () => {
expect(eventWasFired('input')).toBe(true)
expect(element.files).toHaveLength(0)
})

test('input.files implements iterable', () => {
const {element, getEvents} = setup(`<input type="file" multiple/>`)
const files = [
new File(['hello'], 'hello.png', {type: 'image/png'}),
new File(['there'], 'there.png', {type: 'image/png'}),
]

userEvent.upload(element, files)
const eventTargetFiles = getEvents('input')[0].target.files

expect(eventTargetFiles).toBe(element.files)
expect(eventTargetFiles).not.toEqual(files)

expect(Array.from(eventTargetFiles)).toEqual(files)
})
11 changes: 10 additions & 1 deletion src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@ function upload(
// the event fired in the browser isn't actually an "input" or "change" event
// but a new Event with a type set to "input" and "change"
// Kinda odd...
const inputFiles: FileList = {
const inputFiles: FileList & Iterable<File> = {
...files,
length: files.length,
item: (index: number) => files[index],
[Symbol.iterator]() {
let i = 0
return {
next: () => ({
done: i >= files.length,
value: files[i++],
}),
}
},
}

fireEvent(
Expand Down

0 comments on commit 9f6355b

Please sign in to comment.