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

Conversation

ef4
Copy link
Contributor

@ef4 ef4 commented Sep 30, 2015

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');
    })

@@ -188,6 +188,18 @@ export default TestModule.extend({
}
hook.apply(module, Array.prototype.slice.call(arguments, 1));
};
context.register = function() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, these belong in the main test-module file, as they are not really related to components...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Will do.

@pixelhandler
Copy link

This looks nice! I've been reaching into the container and using register for a while in tests but it will be great to have a blessed path to doing so 👍

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');
    })
@ef4
Copy link
Contributor Author

ef4 commented Oct 1, 2015

I moved the methods into the base test-context so they're available in all test types, and updated some tests to show that they work.

rwjblue added a commit that referenced this pull request Oct 1, 2015
Add public API for injection & registration
@rwjblue rwjblue merged commit 5c887f5 into emberjs:master Oct 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants