@@ -164,31 +164,55 @@ test('should call onChange/input bubbling up the event when a file is selected',
164
164
expect ( onInputForm ) . toHaveBeenCalledTimes ( 1 )
165
165
} )
166
166
167
- test ( 'should not upload file with invalid unaccepted format' , ( ) => {
167
+ test ( 'should upload file with accepted format' , ( ) => {
168
168
const file = new File ( [ 'hello' ] , 'hello.png' , { type : 'image/png' } )
169
+ const { element} = setup ( '<input type="file" accept="image/png" />' )
169
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 ( `
184
+ <input
185
+ type="file"
186
+ accept="image/*,audio/*,text/csv" multiple
187
+ />
188
+ ` )
189
+
190
+ userEvent . upload ( element , files )
191
+
192
+ expect ( element . files ) . toHaveLength ( 3 )
193
+ } )
194
+
195
+ test ( 'should not upload file with unaccepted format' , ( ) => {
196
+ const file = new File ( [ 'hello' ] , 'hello.png' , { type : 'image/png' } )
170
197
const { element} = setup ( '<input type="file" accept="image/jpg" />' )
171
198
172
199
userEvent . upload ( element , file )
173
200
174
- expect ( element . files [ 0 ] ) . toBeUndefined ( )
175
- expect ( element . files . item ( 0 ) ) . toBeNull ( )
176
201
expect ( element . files ) . toHaveLength ( 0 )
177
202
} )
178
203
179
- test ( 'should not upload multiple files with invalid unaccepted formats' , ( ) => {
204
+ test ( 'should not upload multiple files with unaccepted formats' , ( ) => {
180
205
const files = [
181
206
new File ( [ 'hello' ] , 'hello.txt' , { type : 'text/plain' } ) ,
182
207
new File ( [ 'there' ] , 'there.pdf' , { type : 'application/pdf' } ) ,
208
+ new File ( [ 'there' ] , 'there.png' , { type : 'image/png' } ) ,
209
+ new File ( [ 'there' ] , 'there.mp4' , { type : 'video/mp4' } ) ,
183
210
]
184
-
185
211
const { element} = setup ( `
186
- <input id="files" type="file" accept="image/jpeg,image/png " multiple />
212
+ <input id="files" type="file" accept="video/* " multiple />
187
213
` )
188
214
189
215
userEvent . upload ( element , files )
190
216
191
- expect ( element . files [ 0 ] ) . toBeUndefined ( )
192
- expect ( element . files . item ( 0 ) ) . toBeNull ( )
193
- expect ( element . files ) . toHaveLength ( 0 )
217
+ expect ( element . files ) . toHaveLength ( 1 )
194
218
} )
0 commit comments