const fakeFiles = Symbol ( 'files and value properties are mocked' ) ; declare global { interface HTMLInputElement { [ fakeFiles ] ? : { restore : ( ) => void ; } ; } } export function setFiles ( el , files ) { el [ fakeFiles ] ?. restore ( ) ; const objectDescriptors = Object . getOwnPropertyDescriptors ( el ) ; const prototypeDescriptors = Object . getOwnPropertyDescriptors ( Object . getPrototypeOf ( el ) ) ; function restore ( ) { Object . defineProperties ( el , { files : { ... prototypeDescriptors . files , ... objectDescriptors . files , } , value : { ... prototypeDescriptors . value , ... objectDescriptors . value , } , type : { ... prototypeDescriptors . type , ... objectDescriptors . type , } , } ) ; } el [ fakeFiles ] = { restore } ; Object . defineProperties ( el , { files : { ... prototypeDescriptors . files , ... objectDescriptors . files , get : ( ) => files , } , value : { ... prototypeDescriptors . value , ... objectDescriptors . value , get : ( ) => ( files . length ? `C:\\fakepath\\${ files [ 0 ] . name }` : '' ) , set ( v ) { if ( v === '' ) { restore ( ) ; } else { objectDescriptors . value . set ?. call ( el , v ) ; } } , } , type : { ... prototypeDescriptors . type , ... objectDescriptors . type , set ( v ) { if ( v !== 'file' ) { restore ( ) ; ( el as HTMLInputElement ) . type = v ; } } , } , } ) ; }