1

I am trying to simulate an onChange event on file input to be able to test my upload function. I know how to file onChange however I do not know what the parameter for onChange should be. I know it's probably an event however I don't know how to create a file input event. My react dom element:

<input className='attachmentInput' style={{ display: 'none' }} ref={(input) => this.attachmentInput = input} type='file' onChange={upload()}/>

My test code:

let file = new File(['some content'], 'filename.txt'
this.subject.find('.attachmentInput').props().onChange(?)
Ela
  • 3,142
  • 6
  • 25
  • 40

1 Answers1

0

Your question lacks any context about what is it you are trying to achieve. However, this may be a good place to start: https://developer.mozilla.org/en/docs/Using_files_from_web_applications

this is probably the answer you're looking for:

How can I trigger an onchange event manually?

Community
  • 1
  • 1
e-neko
  • 1,226
  • 9
  • 13
  • I have updated the question to specify that I need it for testing my onChange function. – Ela Sep 22 '16 at 23:02
  • https://developer.mozilla.org/en-US/docs/Web/Events/change The change event is quite simple and doesn't contain anything interesting. The file info/content is not part of it as far as I know. – e-neko Sep 22 '16 at 23:10
  • This is probably what you need: http://stackoverflow.com/questions/2856513/how-can-i-trigger-an-onchange-event-manually – e-neko Sep 22 '16 at 23:13
  • the link specifies indeed how to fire the event but it doesn't tell me how to specify the file that I'm simulating input off – Ela Sep 22 '16 at 23:24
  • That's what I was afraid of. The file is not a part of event, it is contained in FileList property of the input element. It could be possible to create a File object from scratch, depending on browser (not IE/Edge), or from Canvas (an image file), but inserting it into input element will probably fail (you can try). You may have to provide mock replacement for input element instead, containing Files array with this File object as its member. – e-neko Sep 22 '16 at 23:33