In Mastering Ember.js helpers: Arguments you learned how to use Ember.js helper arguments.

Let's look at our helper again:

// app/helpers/regal-portrait.js
import Ember from 'ember';

export function regalPortrait([person], { width, height }) {  
  return `A portrait of ${person} which is ${width} wide and ${height} high`;
}

export default Ember.Helper.helper(regalPortrait);  

You may have noticed that the helper blueprint exporting both the helper and the helper function. This let's us easily unit test the helper function. Thankfully ember g helper automatically created a unit test for us.

// tests/unit/helpers/regal-portrait-test.js
import { regalPortrait } from '../../../helpers/regal-portrait';  
import { module, test } from 'qunit';

module('Unit | Helper | regal portrait');

// Replace this with your real tests.
test('it works', function(assert) {  
  let result = regalPortrait(42);
  assert.ok(result);
});

Let's change this test so that it checks that the helper correctly outputs the values we pass in.

Remember that the first argument to a helper is always an array? When calling the helper in a template, Ember.js automatically converts the parameter list into an array and passes it to our helper. In the unit test we have to do it ourselves:

test('it outputs the name', function(assert) {  
  let result = regalPortait(['Richard III'], { width: '4 feet', height: '15 feet' });
  assert.ok(result.indexOf("Richard III") !== -1);
  assert.ok(result.indexOf("4 feet") !== -1);
});

If you want to learn more about assertions with QUnit, check out the API docs.

Now go forth and impress your petty bourgeois co-workers.

Tags: Helpers, Testing