Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add public API for injection & registration #105

Merged
merged 1 commit into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,22 @@ export default Klass.extend({
container: this.container,
registry: this.registry,
factory: factory,
dispatcher: null
dispatcher: null,
register: function() {
var target = this.registry || this.container;
return target.register.apply(target, arguments);
},
inject: {}
});

this.context = getContext();
var context = this.context = getContext();

Object.keys(Ember.inject).forEach(function(typeName) {
context.inject[typeName] = function(name, opts) {
var alias = (opts && opts.as) || name;
Ember.set(context, alias, context.container.lookup(typeName + ':' + name));
};
});
},

setupTestElements: function() {
Expand Down
57 changes: 56 additions & 1 deletion tests/test-module-for-integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,61 @@ moduleForComponent('Component Integration Tests: implicit views are not deprecat

test('the toplevel view is not deprecated', function () {
expect(0);
(this.registry || this.container).register('component:my-toplevel', this.container.lookupFactory('view:toplevel'));
this.register('component:my-toplevel', this.container.lookupFactory('view:toplevel'));
this.render("{{my-toplevel}}");
});


moduleForComponent('Component Integration Tests: register and inject', {
integration: true
});

test('can register a component', function() {
this.register('component:x-foo', Ember.Component.extend({
classNames: ['i-am-x-foo']
}));
this.render("{{x-foo}}");
equal(this.$('.i-am-x-foo').length, 1, "found i-am-x-foo");
});

test('can register a service', function() {
this.register('component:x-foo', Ember.Component.extend({
unicorn: Ember.inject.service(),
layout: Ember.Handlebars.compile('<span class="x-foo">{{unicorn.sparkliness}}</span>')
}));
this.register('service:unicorn', Ember.Component.extend({
sparkliness: 'extreme'
}));
this.render("{{x-foo}}");
equal(this.$('.x-foo').text().trim(), "extreme");
});

test('can inject a service directly into test context', function() {
this.register('component:x-foo', Ember.Component.extend({
unicorn: Ember.inject.service(),
layout: Ember.Handlebars.compile('<span class="x-foo">{{unicorn.sparkliness}}</span>')
}));
this.register('service:unicorn', Ember.Component.extend({
sparkliness: 'extreme'
}));
this.inject.service('unicorn');
this.render("{{x-foo}}");
equal(this.$('.x-foo').text().trim(), "extreme");
this.set('unicorn.sparkliness', 'amazing');
equal(this.$('.x-foo').text().trim(), "amazing");
});

test('can inject a service directly into test context, with aliased name', function() {
this.register('component:x-foo', Ember.Component.extend({
unicorn: Ember.inject.service(),
layout: Ember.Handlebars.compile('<span class="x-foo">{{unicorn.sparkliness}}</span>')
}));
this.register('service:unicorn', Ember.Component.extend({
sparkliness: 'extreme'
}));
this.inject.service('unicorn', { as: 'hornedBeast' });
this.render("{{x-foo}}");
equal(this.$('.x-foo').text().trim(), "extreme");
this.set('hornedBeast.sparkliness', 'amazing');
equal(this.$('.x-foo').text().trim(), "amazing");
});
10 changes: 4 additions & 6 deletions tests/test-module-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ moduleFor('component:x-foo', 'component:x-foo -- setup context', {
name: 'Max'
});

var thingToRegisterWith = this.registry || this.container;
thingToRegisterWith.register('service:blah', Ember.Object.extend({
this.register('service:blah', Ember.Object.extend({
purpose: 'blabering'
}));
}
Expand All @@ -85,9 +84,8 @@ test("subject can be initialized in setup", function() {
});

test("can lookup factory registered in setup", function() {
var service = this.container.lookup('service:blah');

equal(service.get('purpose'), 'blabering');
this.inject.service('blah');
equal(Ember.get(this, 'blah.purpose'), 'blabering');
});

moduleFor('component:x-foo', 'component:x-foo -- callback context', {
Expand Down Expand Up @@ -195,7 +193,7 @@ test('gets the default by default', function() {
});

test('can override the default', function() {
this.registry.register('foo:thing', Ember.Object.extend({
this.register('foo:thing', Ember.Object.extend({
notTheDefault: true
}));
var thing = this.container.lookup('foo:thing');
Expand Down