Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 452
Ungue Avatar asked Mar 18 '15 11:03

Ungue


People also ask

How do I run ember acceptance tests?

Ember CLI comes with acceptance test support out of the box. For creating your first test, you just need to run ember generate acceptance-test <name> . In our case, ember generate acceptance-test user-can-login-via-form . Ember CLI will create a new test file under tests/acceptance/ .

How do I upload files to Ember?

ember-cli-file-picker - An addon for ember-cli that provides a component to easily add a filepicker to your ember-cli app. emberx-file-input - A tiny Ember component which does one thing and only: select files beautifully. ember-profile-upload - A simple photo upload component.


1 Answers

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.

like image 93
jonne Avatar answered Oct 12 '22 23:10

jonne