1
votes

I can't find a single example in google for unit test of views in Ember CLI that renders the view (without renders all app).

I wanna this for test events registered inside of didInserElement hook.

For components i can find docs very easy. For render the component in a test with moduleForComponent just do:

test("component test", function(){
  var component = this.subject(),
  element = this.append();
  ok(element.is('.clickable'), 'has the clickable class');
});

But how i do this for views?

1

1 Answers

2
votes

I use this way to render only the view in unit tests:

One important thing to note, is that you need to needs all templates, partials and helpers explicitly. Otherwise the test will fail due to lookup errors.

tests/unit/views/main-test.js:

import Ember from 'ember';
import { test, moduleFor } from 'ember-qunit';

var view;

moduleFor('view:main', 'MainView', {
  needs: ['template:main'], // won't find the 'main' template without this
  setup: function() {
    var controller = Ember.ObjectController.extend({
      //mockController if needs
    }).create();
    view = this.subject({
      controller: controller,
      templateName: 'main',
    });
    Ember.run(function() {
      view.appendTo('#ember-testing');
    });
  },
  teardown: function() {
    Ember.run(function() {
      view.destroy();
    });
  },
});

test("didInsertElement", function(){
  var element = Ember.$('.main');
  var controller = view.get('controller');

  var eventForPressCtrlAltM = Ember.$.Event( "keydown", { which: 77, altKey: true, ctrlKey: true } );

  Ember.run(function() {
    element.trigger(eventForPressCtrlAltM);
  });
  strictEqual(/* ... */);
});