Search code examples
javascriptunit-testingember.jsember-cliember-qunit

Ember Test Initializer that Requires Model


I have an initializer that registers some modules with the application from a JSON object on the page in a script tag. Works fine in the application but the test fails because it cannot find the models that are expected.

initialzers/bootstrap-payload.js

export function initialize(container, application) {
  var store = container.lookup('service:store'),
    payloadKeys = Object.keys(BOOTSTRAP_DATA);

  payloadKeys.forEach((key) => {
    var registryKey = `bootstrap-payload:${key}`,
        model;

    model = store.createRecord(key, BOOTSTRAP_DATA[key]);
    application.register(registryKey, model, {instantiate:false});
  });
}

export default {
  name: 'bootstrap-payload',
  after: 'ember-data',
  initialize: initialize
};

tests/initializers/bootstrap-payload-test.js

import Ember from 'ember';
import { initialize } from '../../../initializers/bootstrap-payload';
import { module, test } from 'qunit';

var registry, application;

module('Unit | Initializer | bootstrap payload', {
  needs: ['model:channel'],

  beforeEach: function() {
    Ember.run(function() {
      application = Ember.Application.create();
      registry = application.registry;
      application.deferReadiness();
    });
  }
});

// Replace this with your real tests.
test('it works', function(assert) {
  initialize(registry, application);

  // you would normally confirm the results of the initializer here
  assert.ok(true);
});

tests/index.html includes an example BOOTSTRAP_DATA variable in it that contains a model that is always expected to be there called channel. When running ember test I get the following error.

at http://localhost:7357/assets/test-support.js:5604: No model was found for 'channel'

How can I inject this dependency the needs field doesn't seem to work in this case. Or is there anyway to make the initializer more testable.


Solution

  • Credit to https://github.com/taras for this answer.

    Rather than creating a unit test for this initializer we can create an acceptance test that asserts that the properties have been properly injected into our container.

    import Ember from 'ember';
    import { module, test } from 'qunit';
    import startApp from 'test-models-in-initializer/tests/helpers/start-app';
    import Channel from 'test-models-in-initializer/models/channel';
    
    var application;
    
    module('Acceptance | index', {
      beforeEach: function() {
        window.BOOTSTRAP_DATA = {
          'channel': {
            'id': 0,
            'name': 'Test Channel',
            'internalName': 'test-channel',
            'logoUrl': '//somecdn.net/test-channel/logo.png'
          }
        };
        application = startApp();
      },
    
      afterEach: function() {
        Ember.run(application, 'destroy');
      }
    });
    
    test('channel type', function(assert) {
      let channel = application.registry.lookup('bootstrap-payload:channel');
      assert.ok(channel, "is registered");
      assert.ok(channel instanceof Channel);
    });
    

    test application here https://github.com/embersherpa/test-models-in-initializer