在我的Ember应用程序中,我有一个简单的表单组件,具有以下行为(摘要):
...
let searchText = this.get('searchText') &&
this.get('searchText').trim().toLowerCase();
this.sendAction('searchTextChanged', searchText);
...
如何在qunit测试中对该功能进行单元测试?我无法弄清楚如何听取动作结果。我正在使用ember-cli附带的测试功能-qunit,helpers等。
恩伯(Ember)在此处提供了有关如何执行此操作的指南。我将代码放在这里,以防指南更改。
鉴于此组件:
App.MyFooComponent = Ember.Component.extend({
layout:Ember.Handlebars.compile("<button {{action 'doSomething'}}></button>"),
actions: {
doSomething: function() {
this.sendAction('internalAction');
}
}
});
您将像这样测试操作:
moduleForComponent('my-foo', 'MyFooComponent');
test('trigger external action when button is clicked', function() {
// tell our test to expect 1 assertion
expect(1);
// component instance
var component = this.subject();
// component dom instance
var $component = this.append();
var targetObject = {
externalAction: function() {
// we have the assertion here which will be
// called when the action is triggered
ok(true, 'external Action was called!');
}
};
// setup a fake external action to be called when
// button is clicked
component.set('internalAction', 'externalAction');
// set the targetObject to our dummy object (this
// is where sendAction will send its action to)
component.set('targetObject', targetObject);
// click the button
click('button');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句