Eliminating an error while testing a helper from an addon project

https://github.com/stefanpenner/ember-cli/issues/2421

ember-cli: 1.2

I have a project with an additional template that has a header helper:

My helper app/helpers/title-case.js

import Ember from 'ember';

export default Ember.Handlebars.makeBoundHelper(function(string) {

    if (typeof string === 'string') {
        //replace dashes with spaces
        var str = string.dasherize().replace(/-/g, ' ');

        return str.replace(/\w\S*/g, function(word){
            return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
        });
    } else {
        return string;
    }

});

      

I Created a test for a helper using ember-cli

ember g helper-test title-case

      

This was the result:

import {
  titleCase
} from 'boilerplate/helpers/title-case';

module('TitleCaseHelper');

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

      

Tests from ember-cli are now running

ember test

      

Getting the following error:

Build failed.
File: dummy/tests/unit/helpers/title-case-test.js
ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'
Error: ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'

      

UPDATE

I tried changing the following to "dummy" instead of the auto-generated "template" and removed the curly braces.

//import {
//  titleCase
//} from 'dummy/helpers/title-case';
import titleCase from 'dummy/helpers/title-case';

      

it gets more and now in the test method, but it fails when called titleCase(42)

:

TypeError: Cannot read property 'isUnbound' of undefined

      

UPDATE # 2

I managed to get this working, but it is ugly, I needed to access the property ._rawFunction

and change the format of the import statement.

import titleCaseHelper from 'dummy/helpers/title-case';

var titleCase = titleCaseHelper._rawFunction;
module('TitleCaseHelper');

test('Title case lower case letters', function() {
      var result = titleCase('hello world');
      equal(result, 'Hello World');
});

      

I'm still confused as to why the original generated ember-cli test didn't work.

+3


source to share


1 answer


The helper test generator expects the helper file to be in a special format, which you can see if you create the helper:

ember generate helper hello

      

This will create an app / helpers / hello.js file containing

import Ember from 'ember';

export function hello(input) {
  return input;
};

export default Ember.Handlebars.makeBoundHelper(hello);

      



It also generates a unit test for the above in tests / unit / helers / hello -test.js with

import {
  hello
} from 'ember-empty/helpers/hello';

module('HelloHelper');

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

      

In other words, the helper test generator expects you to also export the raw function itself, in addition to the linked helper.

+3


source







All Articles