-
Notifications
You must be signed in to change notification settings - Fork 183
Doc: Testing
Ember-I18n should work just fine with tests that call startApp
.
When testing components that rely on translated content, the recommended approach is to use an integration-style component test:
moduleForComponent("my-component", "MyComponent", {
integration: true,
beforeEach: function() {
this.set('foo', 'bar');
this.render('{{#my-component}}{{t "some.foo" foo=bar}}{{/my-component}}');
};
}
});
These tests don't run initializers, though, so there are two things you may have to do to get such a test working:
- set
i18n.locale
- register the
t
helper
// choose one:
import tHelper from "ember-i18n/legacy-helper"; // Ember 1.12
import tHelper from "ember-i18n/helper"; // Ember 1.13+
moduleForComponent('my-component', 'MyComponent', {
beforeEach() {
// set the locale:
this.container.lookup('service:i18n').set('locale', 'xy');
// register the helper:
this.registry.register('helper:t', tHelper);
}
});
If using Ember 1.13+, another option is to run the initializer rather than registering the helper:
import initializer from "my-app/instance-initializers/ember-i18n";
module("foo", {
integration: true,
setup() {
initializer.initialize(this);
}
});
Ember-I18n requires many things to be available in the Registry
, which makes it difficult to use in unit tests. At a minimum, you would have to declare
needs: [
'service:i18n',
'helper:t',
'locale:xy/translations'
]
and then set i18n.locale
and register the t
helper.
If you find yourself writing lots of unit tests that rely on ember-i18n and find a way to ease this pain, this project would welcome contributions!