Skip to content

Instantly share code, notes, and snippets.

@zdennis
Last active March 7, 2017 03:11
Show Gist options
  • Save zdennis/52d88c2bed544959aaa5994f65dbcac3 to your computer and use it in GitHub Desktop.
Save zdennis/52d88c2bed544959aaa5994f65dbcac3 to your computer and use it in GitHub Desktop.
New Twiddle
import Ember from 'ember';
export default Ember.Component.extend({
actions: {
clicked() {
this.$().append("Clicked");
}
}
});
import Ember from 'ember';
export default Ember.Component.extend({});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{my-component}}
{{my-sub-component clicked=(action 'clicked')}}
<button {{action clicked}}>Click me</button>
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 { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('my-component', 'three different ways to test component actions in a component integration test', {
integration: true
});
test('it interacts with the action thru the real my-sub-component', function(assert) {
this.render(hbs`{{my-component}}`);
assert.equal(this.$().text().trim(), 'Click me');
this.$("button").click();
// "Clicked" comes from the action defined in
// components/my-component.js
assert.equal(this.$().text().trim(), 'Click meClicked');
this.$("button").click();
assert.equal(this.$().text().trim(), 'Click meClickedClicked');
});
test('it uses fake a fake component and template to create a simplified DOM element we can interact with', function(assert) {
// Override component and template for this test specifically
// This may work well if you want to "stub" out nested components
// because they are overly complex
this.register('component:my-sub-component', Ember.Component.extend());
this.register('template:components/my-sub-component',
hbs`<div {{action clicked}}>Click me</div>`
);
this.render(hbs`{{my-component}}`);
assert.equal(this.$().text().trim(), 'Click me');
// The 'div' here shows us that we are not rendering the
// button in templates/components/my-sub-component.hbs but
// instead using the div defined a few lines up in this test.
this.$("div").click();
// "Clicked" is still coming from the action defined in
// components/my-component.js
assert.equal(this.$().text().trim(), 'Click meClicked');
this.$("div").click();
assert.equal(this.$().text().trim(), 'Click meClickedClicked');
});
//
// This is my favorite of all of the approaches listed.
//
test('it tests an action by defining the action in the test and calling the action directly', function(assert) {
let clicked = () => { this.$().append("WAT"); };
this.set('actions.clicked', clicked);
this.render(hbs`{{my-component clicked=(action 'clicked')}}`);
assert.equal(this.$().text().trim(), 'Click me');
clicked();
// The term WAT shows that we are defining the action
// a few lines up in this test. Even though the real
// my-sub-component is used this shows that we are using
// the action defined in the test.
assert.equal(this.$().text().trim(), 'Click meWAT');
clicked();
assert.equal(this.$().text().trim(), 'Click meWATWAT');
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
{
"version": "0.11.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.11.0",
"ember-data": "2.11.0",
"ember-template-compiler": "2.11.0",
"ember-testing": "2.11.0"
},
"addons": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment