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

Trouble testing component that uses the helper with a custom config #5

Open
mjuniper opened this issue Nov 11, 2015 · 4 comments
Open

Comments

@mjuniper
Copy link
Contributor

I have a component that uses the helper with a custom config. It works perfectly in the app but now I am trying to write an integration test for it.

My first stab got me this: Failed to create an instance of 'sanitizer:links-only'. Most likely an improperly defined class or an invalid module export.

So I figured I need to run the initializer. My test now looks something like this:

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import initializer from 'my-app/initializers/setup-sanitizers';

moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true,

  setup() {
    // manually invoke the ember-sanitize initializer
    initializer.initialize(this);
    //initializer.initialize(this.container);
  }
});

test('it renders', function(assert) {
  ...
});

But now I get container.registerOptionsForType is not a function coming out of the initializer. I think I'm on the right track but I'm not sure what to pass to the initializer. Any suggestions? Thanks.

@mjuniper
Copy link
Contributor Author

Ok, this seems to work but I'll bet there's a better way:

import Ember from 'ember';
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import sanitizer from 'my-app/sanitizers/links-only';

moduleForComponent('my-component', 'Integration | Component | my component', {
  integration: true,

  setup() {
    this.container.registry.register('sanitizer:links-only', Ember.Object.extend(sanitizer));
  }
});

test('it renders', function(assert) {
  ...
});

@rlivsey
Copy link
Member

rlivsey commented Nov 11, 2015

I've done this before, but remember it being a bit evil when I did it too & having to go through the container (now registry) like you're doing.

The problem is, as you've seen, that initializers aren't run in component tests as the app isn't booted.

I'll ask around and see what the best way of doing it is. I wonder if there's a way for the addon to hook into the tests so it sets up the initializer for you automagically?

@jcano
Copy link

jcano commented May 20, 2016

I'm having this issue too, @rlivsey have you figured out the best way of testing?

@pogopaule
Copy link

pogopaule commented Jul 26, 2016

I wrote my integration test like this:

...
import sanitizeInitializer from 'my-app/initializers/setup-sanitizers';

moduleForComponent('foo-bar', 'Integration - pods/components/foo-bar', {
  integration: true,
  beforeEach() {
    sanitizeInitializer.initialize(this.container.registry);
  }
});

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

No branches or pull requests

4 participants