@@ -7,9 +7,8 @@ it('returns a Promise', async () => {
77 expect ( fromEvent ( evt ) ) . toBeInstanceOf ( Promise ) ;
88} ) ;
99
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 ( { } ) ;
1312 expect ( files ) . toHaveLength ( 0 ) ;
1413} ) ;
1514
@@ -33,14 +32,19 @@ it('should return the evt {target} {files} if the passed event is an input evt',
3332 expect ( file . path ) . toBe ( name ) ;
3433} ) ;
3534
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 ( ) => {
3742 const name = 'test.json' ;
38- const mockFile = createFile ( name , { ping : true } , {
43+ const [ mockFile , mockHandle ] = createFileSystemFileHandle ( name , { ping : true } , {
3944 type : 'application/json'
4045 } ) ;
41- const evt = dragEvt ( [ mockFile ] ) ;
4246
43- const files = await fromEvent ( evt ) ;
47+ const files = await fromEvent ( [ mockHandle ] ) ;
4448 expect ( files ) . toHaveLength ( 1 ) ;
4549 expect ( files . every ( file => file instanceof File ) ) . toBe ( true ) ;
4650
@@ -53,12 +57,32 @@ it('should return {files} from DataTransfer if {items} is not defined (e.g. IE11
5357 expect ( file . path ) . toBe ( name ) ;
5458} ) ;
5559
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' ) ;
5862 const files = await fromEvent ( evt ) ;
5963 expect ( files ) . toHaveLength ( 0 ) ;
6064} ) ;
6165
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+
6286it ( 'should return files from DataTransfer {items} if the passed event is a DragEvent' , async ( ) => {
6387 const name = 'test.json' ;
6488 const mockFile = createFile ( name , { ping : true } , {
@@ -373,9 +397,14 @@ function inputEvtFromFiles(...files: File[]): Event {
373397 value : files
374398 } ) ;
375399 }
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+ } ) ;
379408}
380409
381410function createFile < T > ( name : string , data : T , options ?: FilePropertyBag ) {
@@ -384,12 +413,26 @@ function createFile<T>(name: string, data: T, options?: FilePropertyBag) {
384413 return file ;
385414}
386415
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+
387426function sortFiles < T extends File > ( files : T [ ] ) {
388427 return files . slice ( 0 )
389428 . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
390429}
391430
392431
432+ interface FileSystemFileHandle {
433+ getFile ( ) : Promise < File > ;
434+ }
435+
393436type FileOrDirEntry = FileEntry | DirEntry
394437
395438interface FileEntry extends Entry {
0 commit comments