Skip to content

Instantly share code, notes, and snippets.

@phillipkregg
Last active June 28, 2017 18:05
Show Gist options
  • Save phillipkregg/b5a8746f92d2722d5c85d3f21f7f53b4 to your computer and use it in GitHub Desktop.
Save phillipkregg/b5a8746f92d2722d5c85d3f21f7f53b4 to your computer and use it in GitHub Desktop.
Override Methods on Integration Tests
import Ember from 'ember';
export default Ember.Component.extend({
comment: '',
actions: {
submitComment() {
this.get('submitComment')({ comment: this.get('comment') });
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({
text: '',
didInsertElement() {
this.set('text', 'Inserted');
},
// EXTRACTING ALLOWS OVERRIDING METHODS IN ACTIONS HASH
buttonClick() {
this.set('text', 'This is my click handler.');
},
actions: {
buttonClick() {
this.buttonClick();
}
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
body {
margin: 12px 16px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 12pt;
}
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{my-component
}}
{{comment-form}}
<form {{action "submitComment" on="submit"}}>
<label>Comment:</label>
{{textarea value=comment}}
<input type="submit" value="Submit"/>
</form>
<h3>My Component</h3>
<h5>{{text}}</h5>
<button class="my-button" {{action "buttonClick"}}>Click Me</button>
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';
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;
}
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('comment-form', 'TODO: put something here', {
integration: true
});
test('should trigger external action on form submit', function(assert) {
// test double for the external action
this.set('externalAction', (actual) => {
let expected = { comment: 'You are not a wizard!' };
assert.deepEqual(actual, expected, 'submitted value is passed to external action');
});
this.render(hbs`{{comment-form submitComment=(action externalAction)}}`);
// fill out the form and force an onchange
this.$('textarea').val('You are not a wizard!');
this.$('textarea').change();
// click the button to submit the form
this.$('input').click();
});
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('my-component', 'TODO: put something here', {
integration: true
});
test('it renders', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
this.render(hbs`{{my-component}}`);
assert.ok(true);
});
test('Insert function changed', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
this.render(hbs`{{my-component}}`);
assert.equal(this.$().find('h5').text().trim(), 'Inserted', 'Inserted text default');
this.set('newInsert', function() {
this.set('text', 'Insert overridden');
});
this.render(hbs`{{my-component didInsertElement=newInsert}}`);
assert.equal(this.$().find('h5').text().trim(), 'Insert overridden', 'Inserted text overridden');
});
test('Click changes text', function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
/* IF YOU WANT TO OVERRIDE
this.set('externalAction', () => {
this.set('text', 'Click Override!');
this.render(hbs`{{my-component text=text}}`);
assert.equal(this.$().find('h5').text().trim(), 'Click Override!', 'Text changed after click');
});
*/
this.render(hbs`{{my-component}}`);
this.$().find('.my-button').click();
assert.equal(this.$().find('h5').text().trim(), 'This is my click handler.', 'Text changed after click');
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.12.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.12.0",
"ember-template-compiler": "2.12.0",
"ember-testing": "2.12.0"
},
"addons": {
"ember-data": "2.12.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment