Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(upload): implement iterable on input.files #603

Merged
merged 2 commits into from Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/__tests__/upload.js
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
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