Testing 1.13 Ember.Helper Objects

Does anyone have an example of how to test the new 1.13 Ember.Helper objects?

import { module, test } from 'qunit';
import slugifyStringHelper from '../../../helpers/slugify-string';

module('helper:slugify-string');

test('#compute', function(assert) {
  assert.expect(2);
  assert.equal(slugifyStringHelper.compute([' Hello World! ']), 'hello-world');
  assert.equal(slugifyStringHelper.compute(['<script>']), 'script');
});

This technique works for both types of helper: Ember.Helper.helper and Ember.Helper.extend

I’m unable to get the above approach to work for Ember.Helper.extend. I get the below error:

TypeError: slugifyStringHelper.default.compute is not a function

This error makes sense to me, because it feels like I should need to instantiate an instance of the helper before I can call compute. Am I missing something?

With Ember.Helper.extend you will be testing an object, so you you’ll have to create an instance first.

Often you’ll have been using Ember.Helper.extend because you’ve needed to inject a service or something into your helper, and therefore when testing you’ll probably need that service available too.

import LocaliseHelper from '../../../helpers/localise'
var localise;
module('helper:localise', {
  beforeEach: function() {
    var register = new Ember.Registry();
    var container = registry.container();
    registry.register('helper:localise', LocaliseHelper);
    registry.register('service:foo, FooService);
    localise = container.lookup('helper:localise');
  }
});

// localise.compute(...
1 Like

Thanks @amk, that works!

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

moduleFor('helper:foo');

test('It can calculate the result', function(assert) {
  assert.expect(1);
  var subject = this.subject();
  assert.equal(subject.compute(), 'bar');
});

The best way to test helpers is to actually invoke then as you would in a template. The newish style integration tests are the best for this.

Example:

https://github.com/aptible/dashboard.aptible.com/blob/master/tests/integration/helpers/eq-test.js

1 Like