Skip to content

Instantly share code, notes, and snippets.

@chrism
Last active March 21, 2016 11:13
Show Gist options
  • Save chrism/3a409c5428055c6de6c0 to your computer and use it in GitHub Desktop.
Save chrism/3a409c5428055c6de6c0 to your computer and use it in GitHub Desktop.
File Input Testing
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
actionState: 'not selected',
actions: {
testAction: function(file) {
this.set('actionState', file.name);
}
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{file-input action="testAction" }}
<br>
<br>
<h2 id="file-name">{{actionState}}</h2>
import Ember from 'ember';
export default Ember.Component.extend({
change: function(evt) {
const file = evt.target.files[0];
Ember.Logger.log('changed', evt.target.files);
if (file) {
this.sendAction('action', file);
}
}
});
{{input type="file" id="my-file-chooser"}}
import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('File Chooser');
test('No File Attached', function(assert) {
Ember.Logger.log('no file attached');
visit('/');
triggerEvent('#my-file-chooser', 'change');
andThen(function() {
assert.equal(find('#file-name').text(), "not selected");
});
});
test('File Attached', function(assert) {
Ember.Logger.log('file attached');
const blob = new Blob(['test'], { type: 'text/plain' });
blob["lastModifiedDate"] = "";
blob["name"] = "example.txt";
const file = blob;
// alternative File object creation chrome only also doesn't work
// const file = new File(["test"], "example.txt");
visit('/');
// seems like trying to pass { target } as an option breaks it as
// something else, like { test : "file" } at least logs change in the console
triggerEvent('#my-file-chooser', 'change', { target: { files: [file] } });
andThen(function() {
assert.equal(find('#file-name').text(), "example.txt");
});
});
import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}
import { module } from 'qunit';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
options.beforeEach.apply(this, arguments);
}
},
afterEach() {
if (options.afterEach) {
options.afterEach.apply(this, arguments);
}
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';
export default function startApp(attrs) {
let application;
let attributes = Ember.merge({rootElement: "#test-root"}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;
Ember.run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.6.5",
"EmberENV": {
"FEATURES": {}
},
"options": {
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
"ember": "2.4.3",
"ember-template-compiler": "2.4.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment