javascript - EmberJS: Unit test class-based helpers -
i have several class-based helpers in app, can include i18n service. works nicely, however, can't figure out way test them.
the auto generated test not working, expects function exported , complains, undefined not constructor
:
module('unit | helper | format number'); // replace real tests. test('it works', function(assert) { let result = formatnumber([42]); assert.ok(result); });
so tried using modulefor
worked testing mixins, failed:
modulefor('helper:format-number', 'unit | helper | format number', { needs: ['service:i18n'] }); test('it works', function(assert) { let result = formatnumber.compute(42); assert.ok(result); });
i tried different versions of instantiating helper object , calling compute on nothing worked. in end either returned null
or failed undefined
error.
did manage succeed failed?
the issue in example you're trying call compute
static method on class itself, when need instance of class.
here's example class-based helper i'm testing. note; uses mocha
, not qunit
, of concepts same.
import { expect } 'chai'; import { beforeeach, describe, } 'mocha'; import shareimage 'my-app/helpers/share-image'; describe('shareimagehelper', function() { beforeeach(function() { this.helperclass = shareimage.create({ location: { hostwithprotocolandport: '' } }); this.helper = this.helperclass.compute.bind(this.helperclass); }); it('calculates `assetpath` correctly', function() { const assetpath = this.helperclass.get('assetpath'); expect(assetpath).to.equal('/assets/images/social/'); }); it('calculates path image correctly', function() { const value = this.helper(['foo', 'bar', 'baz']); expect(value).to.equal('/assets/images/social/foo/bar/baz.png'); }); });
the key here on each test run (in beforeeach
callback) create new instance of helper, , create function tests can call same way template helper called (this.helper
). allows tests real code possible, while still giving me ability modify helper class during tests, can see in beforeeach
callback.
Comments
Post a Comment