Skip to content

Commit

Permalink
Add public API for injection & registration
Browse files Browse the repository at this point in the history
In an app, we encourages users to use injection over container.lookup,
and we encourage `application.register` over `registry.register`. But in
our integration tests, there is no good alternative to direct
`this.container.lookup` and `this.registry.register`.

So I added `register` and `inject` capability directly to the
integration test context. `register` is fairly trivial:

    test('registering something', function(assert) {
      this.register('component:x-foo', Ember.Component.extend({
        classNames: ['i-am-x-foo']
      }));
      this.render(hbs`{{x-foo}}`);
      assert.equal(this.$('.i-am-x-foo').length, 1, 'found x-foo');
    });

`inject` lets you inject anything into the test context:

    test('injecting a service', function(assert) {
      this.inject.service('notifications');
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context,
      // so we can access it here and tell it to do something.
      this.get('notifications').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })

You can also give the injected thing an alternate name using `as`:

    test('injecting a service with aliased name', function(assert) {
      this.inject.service('notifications', { as: 'messages' });
      this.render(hbs`{{my-notification-viewer}}`);

      // The notifications service has been injected into our test context
      // as this.get('messages').
      this.get('messages').receivedNotification("Hello world");

      assert.equal(this.$('.notification').text(), 'Hello world');
    })
  • Loading branch information
ef4 committed Sep 30, 2015
1 parent 8f0f08a commit 66640ee
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ export default TestModule.extend({
}
hook.apply(module, Array.prototype.slice.call(arguments, 1));
};
context.register = function() {
var target = this.registry || this.container;
return target.register.apply(target, arguments);
};

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

setupContext: 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");
});

0 comments on commit 66640ee

Please sign in to comment.