Skip to content

Instantly share code, notes, and snippets.

@feanor07
Created May 27, 2017 07:32
Show Gist options
  • Save feanor07/6b725a2e327cd07495c52c31f359c371 to your computer and use it in GitHub Desktop.
Save feanor07/6b725a2e327cd07495c52c31f359c371 to your computer and use it in GitHub Desktop.
so#44194590
export default Ember.Component.extend({
foo: false,
bar() {
this.set('foo', true);
},
someService: Ember.inject.service('some-service'),
didInsertElement: function () {
const someService = this.get('someService');
someService.registerComponent(this);
},
willDestroyElement: function() {
this.get('someService').unregisterComponent(this);
}
});
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
someService: Ember.inject.service(),
actions : {
click() {
this.get('someService').runBar(0);
}
}
});
export default Ember.Service.extend({
components: [],
registerComponent: function (component) {
let components = this.get('components');
components.push(component);
},
unregisterComponent: function (component) {
let components = this.get('components');
components.removeObject(component);
},
runBar(index) {
let components = this.get('components');
components[index].bar();
}
});
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{outlet}}
{{my-component}}
<button onclick={{action 'click'}}>Push</button>
<br>
<br>
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 wait from 'ember-test-helpers/wait';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('my-component', 'TODO: put something here', {
integration: true,
beforeEach: function () {
this.set('some-service', Ember.getOwner(this).lookup('service:some-service'));
}
});
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.equal(this.$().text().trim(), 'false');
Ember.run(()=>Ember.getOwner(this).lookup('service:some-service').runBar(0));
return wait().then(() => {
assert.equal(this.$().text().trim(), 'true');
});
});
import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
setResolver(resolver);
import { moduleFor, test } from 'ember-qunit';
moduleFor('service:some-service', 'TODO: put something here', {
// Specify the other units that are required for this test.
// needs: ['service:foo']
});
// Replace this with your real tests.
test('it exists', function(assert) {
let service = this.subject();
assert.ok(service);
var x = 0;
var y = 0;
let stubComponent1 = Ember.Object.create({bar: function() { x++; }});
let stubComponent2= Ember.Object.create({bar: function() { y++; }});
service.registerComponent(stubComponent1);
service.registerComponent(stubComponent2);
service.runBar(0);
assert.equal(x, 1);
service.runBar(1);
assert.equal(y, 1);
service.unregisterComponent(stubComponent1);
service.runBar(0);
assert.equal(x, 1);
assert.equal(y, 2);
service.unregisterComponent(stubComponent2);
});
{
"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