@@ -7,9 +7,8 @@ it('returns a Promise', async () => {
7
7
expect ( fromEvent ( evt ) ) . toBeInstanceOf ( Promise ) ;
8
8
} ) ;
9
9
10
- it ( 'should return an empty array if the passed event is not a DragEvent' , async ( ) => {
11
- const evt = new Event ( 'test' ) ;
12
- const files = await fromEvent ( evt ) ;
10
+ it ( 'should return an empty array if the passed arg is not what we expect' , async ( ) => {
11
+ const files = await fromEvent ( { } ) ;
13
12
expect ( files ) . toHaveLength ( 0 ) ;
14
13
} ) ;
15
14
@@ -33,14 +32,19 @@ it('should return the evt {target} {files} if the passed event is an input evt',
33
32
expect ( file . path ) . toBe ( name ) ;
34
33
} ) ;
35
34
36
- it ( 'should return {files} from DataTransfer if {items} is not defined (e.g. IE11)' , async ( ) => {
35
+ it ( 'should return an empty array if the evt {target} has no {files} prop' , async ( ) => {
36
+ const evt = inputEvtFromFiles ( ) ;
37
+ const files = await fromEvent ( evt ) ;
38
+ expect ( files ) . toHaveLength ( 0 ) ;
39
+ } ) ;
40
+
41
+ it ( 'should return files if the arg is a list of FileSystemFileHandle' , async ( ) => {
37
42
const name = 'test.json' ;
38
- const mockFile = createFile ( name , { ping : true } , {
43
+ const [ mockFile , mockHandle ] = createFileSystemFileHandle ( name , { ping : true } , {
39
44
type : 'application/json'
40
45
} ) ;
41
- const evt = dragEvt ( [ mockFile ] ) ;
42
46
43
- const files = await fromEvent ( evt ) ;
47
+ const files = await fromEvent ( [ mockHandle ] ) ;
44
48
expect ( files ) . toHaveLength ( 1 ) ;
45
49
expect ( files . every ( file => file instanceof File ) ) . toBe ( true ) ;
46
50
@@ -53,12 +57,32 @@ it('should return {files} from DataTransfer if {items} is not defined (e.g. IE11
53
57
expect ( file . path ) . toBe ( name ) ;
54
58
} ) ;
55
59
56
- it ( 'should return an empty array if the evt {target} has no {files} prop ' , async ( ) => {
57
- const evt = inputEvtFromFiles ( ) ;
60
+ it ( 'should return an empty array if the passed event is not a DragEvent ' , async ( ) => {
61
+ const evt = new Event ( 'test' ) ;
58
62
const files = await fromEvent ( evt ) ;
59
63
expect ( files ) . toHaveLength ( 0 ) ;
60
64
} ) ;
61
65
66
+ it ( 'should return {files} from DataTransfer if {items} is not defined (e.g. IE11)' , async ( ) => {
67
+ const name = 'test.json' ;
68
+ const mockFile = createFile ( name , { ping : true } , {
69
+ type : 'application/json'
70
+ } ) ;
71
+ const evt = dragEvt ( [ mockFile ] ) ;
72
+
73
+ const files = await fromEvent ( evt ) ;
74
+ expect ( files ) . toHaveLength ( 1 ) ;
75
+ expect ( files . every ( file => file instanceof File ) ) . toBe ( true ) ;
76
+
77
+ const [ file ] = files as FileWithPath [ ] ;
78
+
79
+ expect ( file . name ) . toBe ( mockFile . name ) ;
80
+ expect ( file . type ) . toBe ( mockFile . type ) ;
81
+ expect ( file . size ) . toBe ( mockFile . size ) ;
82
+ expect ( file . lastModified ) . toBe ( mockFile . lastModified ) ;
83
+ expect ( file . path ) . toBe ( name ) ;
84
+ } ) ;
85
+
62
86
it ( 'should return files from DataTransfer {items} if the passed event is a DragEvent' , async ( ) => {
63
87
const name = 'test.json' ;
64
88
const mockFile = createFile ( name , { ping : true } , {
@@ -373,9 +397,14 @@ function inputEvtFromFiles(...files: File[]): Event {
373
397
value : files
374
398
} ) ;
375
399
}
376
- return {
377
- target : input
378
- } as any ;
400
+ return new Proxy ( new CustomEvent ( 'input' ) , {
401
+ get ( t , p , rcvr ) {
402
+ if ( p === 'target' ) {
403
+ return input ;
404
+ }
405
+ return t [ p ] ;
406
+ }
407
+ } ) ;
379
408
}
380
409
381
410
function createFile < T > ( name : string , data : T , options ?: FilePropertyBag ) {
@@ -384,12 +413,26 @@ function createFile<T>(name: string, data: T, options?: FilePropertyBag) {
384
413
return file ;
385
414
}
386
415
416
+ function createFileSystemFileHandle < T > ( name : string , data : T , options ?: FilePropertyBag ) : [ File , FileSystemFileHandle ] {
417
+ const json = JSON . stringify ( data ) ;
418
+ const file = new File ( [ json ] , name , options ) ;
419
+ return [ file , {
420
+ getFile ( ) {
421
+ return Promise . resolve ( file ) ;
422
+ }
423
+ } ] ;
424
+ }
425
+
387
426
function sortFiles < T extends File > ( files : T [ ] ) {
388
427
return files . slice ( 0 )
389
428
. sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
390
429
}
391
430
392
431
432
+ interface FileSystemFileHandle {
433
+ getFile ( ) : Promise < File > ;
434
+ }
435
+
393
436
type FileOrDirEntry = FileEntry | DirEntry
394
437
395
438
interface FileEntry extends Entry {
0 commit comments