Search code examples
ember.jsintegration-testingember-cliqunitember-qunit

Acceptance test for file uploading in ember cli


I'd like to create a basic acceptance test in ember that uploads a file. I can mock the server with Pretender, but I need to know how to fill the input type="file" field with a file from my filesystem. So the questions are basically:

  1. How to fill the input file field with ember test helpers, do I use fillIn helper?
  2. How to add sample files to a folder and get them from my acceptance test. Is it possible to get the current path of my Ember project from the acceptance test to select a file from the filesystem to be uploaded? In Rails we use to use Rails.root for this purpose.

Solution

  • I solved it differently: I don't upload a file from the file system, but create a Blob manually and use triggerHandler on the input element:

    let inputElement = $('input[type=file]');
    
    let blob = new Blob(['foo', 'bar'], {type: 'text/plain'});
    blob.name = 'foobar.txt';
    inputElement.triggerHandler({
      type: 'change',
      target: {
        files: {
          0: blob,
          length: 1,
          item() { return blob; }
        }
      }
    });
    

    This triggers the upload.