Skip to content

Instantly share code, notes, and snippets.

@chrism
Last active May 22, 2016 19:00
Show Gist options
  • Save chrism/a9f273094a12f5bac89dc9dc62915add to your computer and use it in GitHub Desktop.
Save chrism/a9f273094a12f5bac89dc9dc62915add to your computer and use it in GitHub Desktop.
File Chooser Tests Native Events - Ember 2.5.1
import Ember from 'ember';
export default Ember.Component.extend({
change(event) {
const files = event.target.files;
if (files.length === 0) {
Ember.Logger.log('you must add a file');
} else if (files.length > 0) {
this.sendAction('action', files);
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
firstFile: null,
actions: {
addFiles(files) {
this.set('firstFile', files[0].name);
}
}
});
{{file-chooser action="addFiles"}}
<br>
<p>first file: <span id="first-file-text">{{firstFile}}</span></p>
{{input type="file" id="file-chooser"}}
<label for="file-chooser" >choose file</label>
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('Acceptance: File Upload');
test('upload file', function(assert) {
visit('/');
uploadFile('#file-chooser', ['test text'], {});
andThen(function() {
assert.equal(currentURL(), '/');
assert.equal(find('#first-file-text').text(), 'test.txt', {});
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
}
});
}
import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;
import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
import uploadFile from './utils/upload-file';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
/* global Blob */
import Ember from 'ember';
function createFile(content = ['test'], options = {}) {
const {
name,
type
} = options;
const file = new Blob(content, {type : type ? type : 'text/plain'});
file.name = name ? name : 'test.txt';
return file;
}
export default Ember.Test.registerAsyncHelper('uploadFile', function(app, selector, content, options) {
const file = createFile(content, options);
return triggerEvent(
selector,
'change',
{ target: { files: [file] } }
);
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.8.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.5.1",
"ember-template-compiler": "2.5.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment