-
Notifications
You must be signed in to change notification settings - Fork 251
/
upload.js
49 lines (40 loc) · 1.11 KB
/
upload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {fireEvent, createEvent} from '@testing-library/dom'
import {click} from './click'
import {blur} from './blur'
import {focus} from './focus'
function upload(element, fileOrFiles, init) {
if (element.disabled) return
click(element, init)
const input = element.tagName === 'LABEL' ? element.control : element
const files = (Array.isArray(fileOrFiles)
? fileOrFiles
: [fileOrFiles]
).slice(0, input.multiple ? undefined : 1)
// blur fires when the file selector pops up
blur(element, init)
// focus fires when they make their selection
focus(element, init)
// 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 = {
length: files.length,
item: index => files[index],
...files,
}
fireEvent(
input,
createEvent('input', input, {
target: {files: inputFiles},
bubbles: true,
cancelable: false,
composed: true,
...init,
}),
)
fireEvent.change(input, {
target: {files: inputFiles},
...init,
})
}
export {upload}