Skip to content

Commit 6ebf792

Browse files
committed
fix(upload): make accept filter opt-in
1 parent 985d1a7 commit 6ebf792

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

src/__tests__/upload.js

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -164,54 +164,38 @@ test('should call onChange/input bubbling up the event when a file is selected',
164164
expect(onInputForm).toHaveBeenCalledTimes(1)
165165
})
166166

167-
test('should upload file with accepted format', () => {
168-
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
169-
const {element} = setup('<input type="file" accept="image/png" />')
170-
171-
userEvent.upload(element, file)
172-
173-
expect(element.files).toHaveLength(1)
174-
})
175-
176-
test('should upload multiple files with accepted format', () => {
177-
const files = [
178-
new File(['hello'], 'hello.png', {type: 'image/png'}),
179-
new File(['there'], 'there.jpg', {type: 'audio/mp3'}),
180-
new File(['there'], 'there.csv', {type: 'text/csv'}),
181-
new File(['there'], 'there.jpg', {type: 'video/mp4'}),
182-
]
183-
const {element} = setup(`
167+
test.each([
168+
[true, 'video/*,audio/*', 2],
169+
[true, '.png', 1],
170+
[true, 'text/csv', 1],
171+
[true, '', 4],
172+
[false, 'video/*', 4],
173+
])(
174+
'should filter according to accept attribute applyAccept=%s, acceptAttribute=%s',
175+
(applyAccept, acceptAttribute, expectedLength) => {
176+
const files = [
177+
new File(['hello'], 'hello.png', {type: 'image/png'}),
178+
new File(['there'], 'there.jpg', {type: 'audio/mp3'}),
179+
new File(['there'], 'there.csv', {type: 'text/csv'}),
180+
new File(['there'], 'there.jpg', {type: 'video/mp4'}),
181+
]
182+
const {element} = setup(`
184183
<input
185184
type="file"
186-
accept="video/*,audio/*,.png" multiple
185+
accept="${acceptAttribute}" multiple
187186
/>
188187
`)
189188

190-
userEvent.upload(element, files)
189+
userEvent.upload(element, files, undefined, {applyAccept})
191190

192-
expect(element.files).toHaveLength(3)
193-
})
191+
expect(element.files).toHaveLength(expectedLength)
192+
},
193+
)
194194

195-
test('should not upload file with unaccepted format', () => {
196-
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
197-
const {element} = setup('<input type="file" accept="image/jpg" />')
198-
199-
userEvent.upload(element, file)
200-
201-
expect(element.files).toHaveLength(0)
202-
})
203-
204-
test('should not upload multiple files with unaccepted formats', () => {
205-
const files = [
206-
new File(['hello'], 'hello.txt', {type: 'text/plain'}),
207-
new File(['there'], 'there.pdf', {type: 'application/pdf'}),
208-
new File(['there'], 'there.png', {type: 'image/png'}),
209-
]
210-
const {element} = setup(`
211-
<input id="files" type="file" accept="video/*" multiple />
212-
`)
213-
214-
userEvent.upload(element, files)
195+
test('should not trigger input event for empty list', () => {
196+
const {element, eventWasFired} = setup('<input type="file"/>')
197+
userEvent.upload(element, [])
215198

216199
expect(element.files).toHaveLength(0)
200+
expect(eventWasFired('input')).toBe(false)
217201
})

src/upload.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {click} from './click'
33
import {blur} from './blur'
44
import {focus} from './focus'
55

6-
function upload(element, fileOrFiles, init) {
6+
function upload(element, fileOrFiles, init, {applyAccept = false} = {}) {
77
if (element.disabled) return
88

99
click(element, init)
1010

1111
const input = element.tagName === 'LABEL' ? element.control : element
1212

1313
const files = (Array.isArray(fileOrFiles) ? fileOrFiles : [fileOrFiles])
14-
.filter(file => isAcceptableFile(file, element.accept))
14+
.filter(file => !applyAccept || isAcceptableFile(file, element.accept))
1515
.slice(0, input.multiple ? undefined : 1)
1616

1717
// blur fires when the file selector pops up

0 commit comments

Comments
 (0)