Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test of HTML5 file upload

Is there a way to do JavaScript unit test for uploading a file using the HTML 5 File API?

For example I have the code:

<form method="POST" enctype="multipart/form-data">
  <input type="file" id="fileselect" name="fileselect[]" multiple="multiple"/>
</form>

<script type="text/javascript">
   function fileSelectHandler(e) {
       var files = e.target.files || e.dataTransfer.files;
       // at this point, files is a FileList object
   }

   var fileselect = document.getElementById("fileselect");
   fileselect.addEventListener("change", fileSelectHandler, false);
</script>

So I want to automate this with JavaScript, so I can unit test it. So I need to fire the change event, and pass the files somehow. In the end, each file should be a HTML 5 File object, with a custom path that I want.

like image 896
StefanH Avatar asked Dec 19 '25 10:12

StefanH


1 Answers

That kind of test is not a unit test, it is an integration test

https://en.wikipedia.org/wiki/Unit_testing

https://www.guru99.com/integration-testing.html

There are a lot of frameworks for integration test that can do that task easily, like https://webdriver.io/ or https://www.selenium.dev/ and more

Try to sort your tests in corresponding tool and life will be better!

Good Luck!

like image 114
Cristóbal Díaz Álvarez Avatar answered Dec 21 '25 23:12

Cristóbal Díaz Álvarez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!