Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Doc: Testing

Ben Limmer edited this page Nov 19, 2015 · 14 revisions

Testing

Acceptance Tests

Ember-I18n should work just fine with tests that call startApp.

Integration Tests

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:

  1. set i18n.locale
  2. 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);
  }
});

Unit Tests

Ember-I18n requires many things to be available in the Registry, which makes it difficult to use in unit tests. You would have to declare

import tHelper from "ember-i18n/helper";
import localeConfig from 'ember-i18n/config/xy';

moduleForComponent('my-component', 'MyComponent', {
  needs: [
    'service:i18n',
    'locale:xy/translations',
    'locale:xy/config',
    'util:i18n/missing-message',
    'util:i18n/compile-template',
    'config:environment'
  ],
  beforeEach() {
    // set the locale and the config
    this.container.lookup('service:i18n').set('locale', 'xy');
    this.registry.register('locale:xy/config', localeConfig);
    
    // register t helper
    this.registry.register('helper:t', tHelper);
  }
});

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!