-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathupload.ts
250 lines (209 loc) Β· 7.32 KB
/
upload.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {render, setup} from '#testHelpers'
import userEvent from '#src'
test('change file input', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {element, getEventSnapshot, user} = setup<HTMLInputElement>(
'<input type="file" />',
{focus: false},
)
await user.upload(element, file)
expect(element.files?.[0]).toStrictEqual(file)
expect(element.files?.item(0)).toStrictEqual(file)
expect(element.files).toHaveLength(1)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: input[value="C:\\\\fakepath\\\\hello.png"]
input[value=""] - pointerover
input[value=""] - pointerenter
input[value=""] - mouseover
input[value=""] - mouseenter
input[value=""] - pointermove
input[value=""] - mousemove
input[value=""] - pointerdown
input[value=""] - mousedown: primary
input[value=""] - focus
input[value=""] - focusin
input[value=""] - pointerup
input[value=""] - mouseup: primary
input[value=""] - click: primary
input[value=""] - blur
input[value=""] - focusout
input[value="C:\\\\fakepath\\\\hello.png"] - input
input[value="C:\\\\fakepath\\\\hello.png"] - change
input[value="C:\\\\fakepath\\\\hello.png"] - focus
input[value="C:\\\\fakepath\\\\hello.png"] - focusin
`)
})
test('relay click/upload on label to file input', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {
elements: [label],
getEventSnapshot,
user,
} = setup<[HTMLLabelElement]>(
`
<label for="element">Element</label>
<input type="file" id="element" />
`,
{focus: false},
)
await user.upload(label, file)
expect(getEventSnapshot()).toMatchInlineSnapshot(`
Events fired on: div
label[for="element"] - pointerover
div - pointerenter
label[for="element"] - mouseover
div - mouseenter
label[for="element"] - pointermove
label[for="element"] - mousemove
label[for="element"] - pointerdown
label[for="element"] - mousedown: primary
label[for="element"] - pointerup
label[for="element"] - mouseup: primary
label[for="element"] - click: primary
input#element[value=""] - focusin
input#element[value=""] - click: primary
input#element[value=""] - focusout
input#element[value="C:\\\\fakepath\\\\hello.png"] - input
input#element[value="C:\\\\fakepath\\\\hello.png"] - change
input#element[value="C:\\\\fakepath\\\\hello.png"] - focusin
`)
})
test('prevent file dialog per click event handler', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {
elements: [label, input],
eventWasFired,
user,
} = setup<[HTMLLabelElement]>(`
<label for="element">Element</label>
<input type="file" id="element" />
`)
input.addEventListener('click', e => e.preventDefault())
await user.upload(label, file)
expect(eventWasFired('input')).toBe(false)
})
test('upload multiple files', async () => {
const files = [
new File(['hello'], 'hello.png', {type: 'image/png'}),
new File(['there'], 'there.png', {type: 'image/png'}),
]
const {element, user} = setup<HTMLInputElement>(
'<input type="file" multiple />',
)
await user.upload(element, files)
expect(element.files?.[0]).toStrictEqual(files[0])
expect(element.files?.item(0)).toStrictEqual(files[0])
expect(element.files?.[1]).toStrictEqual(files[1])
expect(element.files?.item(1)).toStrictEqual(files[1])
expect(element.files).toHaveLength(2)
})
test('upload multiple files per label', async () => {
const files = [
new File(['hello'], 'hello.png', {type: 'image/png'}),
new File(['there'], 'there.png', {type: 'image/png'}),
]
const {
elements: [label, input],
user,
} = setup<[HTMLLabelElement, HTMLInputElement]>(`
<label for="files">files</label>
<input id="files" type="file" multiple />
`)
await user.upload(label, files)
expect(input.files?.[0]).toStrictEqual(files[0])
expect(input.files?.item(0)).toStrictEqual(files[0])
expect(input.files?.[1]).toStrictEqual(files[1])
expect(input.files?.item(1)).toStrictEqual(files[1])
expect(input.files).toHaveLength(2)
})
// TODO: should this throw?
test('do nothing when element is disabled', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {element, user} = setup<HTMLInputElement>(
'<input type="file" disabled />',
)
await user.upload(element, file)
expect(element.files?.[0]).toBeUndefined()
expect(element.files?.item(0)).toBeNull()
expect(element.files).toHaveLength(0)
})
test.each([
[true, 'video/*,audio/*', 2],
[true, '.png', 1],
[true, 'text/csv', 1],
[true, '', 4],
[false, 'video/*', 4],
])(
'filter according to accept attribute applyAccept=%s, acceptAttribute=%s',
async (applyAccept, acceptAttribute, expectedLength) => {
const files = [
new File(['hello'], 'hello.png', {type: 'image/png'}),
new File(['there'], 'there.jpg', {type: 'audio/mp3'}),
new File(['there'], 'there.csv', {type: 'text/csv'}),
new File(['there'], 'there.jpg', {type: 'video/mp4'}),
]
const {element, user} = setup<HTMLInputElement>(
`
<input
type="file"
accept="${acceptAttribute}" multiple
/>
`,
{applyAccept},
)
await user.upload(element, files)
expect(element.files).toHaveLength(expectedLength)
},
)
test('do not trigger input event when selected files are the same', async () => {
const {element, eventWasFired, clearEventCalls, user} =
setup<HTMLInputElement>('<input type="file" multiple/>')
const files = [
new File(['hello'], 'hello.png', {type: 'image/png'}),
new File(['there'], 'there.png', {type: 'image/png'}),
]
await user.upload(element, [])
expect(eventWasFired('input')).toBe(false)
expect(element.files).toHaveLength(0)
await user.upload(element, files)
expect(eventWasFired('input')).toBe(true)
expect(element.files).toHaveLength(2)
clearEventCalls()
await user.upload(element, files)
expect(eventWasFired('input')).toBe(false)
expect(element.files).toHaveLength(2)
await user.upload(element, [])
expect(eventWasFired('input')).toBe(true)
expect(element.files).toHaveLength(0)
})
test('throw error if trying to use upload on an invalid element', async () => {
const {elements, user} = setup(
'<div></div><label><input type="checkbox"/></label>',
)
await expect(
user.upload(elements[0], new File([], '')),
).rejects.toThrowErrorMatchingInlineSnapshot(
`The given DIV element does not accept file uploads`,
)
await expect(
user.upload(elements[1], new File([], '')),
).rejects.toThrowErrorMatchingInlineSnapshot(
`The associated INPUT element does not accept file uploads`,
)
})
test('uploaded file can be read with FormData', async () => {
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
const {element: form} = render<HTMLFormElement>(
'<form><input name="file" type="file" /></form>',
)
const input = form.querySelector('input') as HTMLInputElement
await userEvent.upload(input, file)
const data = new FormData(form)
const formFile = data.get('file')
if (!(formFile instanceof File)) {
throw new Error('formFile is not a File')
}
expect(formFile).toBeInstanceOf(File)
expect(formFile.name).toBe('hello.png')
expect(formFile).toBe(input.files?.[0])
})