我已为我的Chosen组件添加了自定义no_results
文本(使用Chosen jQuery插件),并且我尝试编写集成测试以查看它是否正常工作。为此,我需要找到一些方法来触发chosen:no_results
事件。
基本上,我的测试分解为这些部分:
我目前被困在2。
我已经尝试过简单地填写输入字段,如下所示:
test('user can create a new option from the dialog box', function() {
visit('/collaborate/foo/430/bar/2/edit');
andThen(function() {
click('.chosen-container > .chosen-single');
andThen(function() {
fillIn('.chosen-container > .chosen-drop > .chosen-search > input', 'New Test');
andThen(function() {
equal(find('.chosen-container > .chosen-drop > .chosen-results > .no-results').text(), 'No results found. Add a new option: "New Test"');
});
});
});
});
然而,失败了。测试返回""
而不是预期的结果。我在最后一个equal
之前设置了一个断点,#ember-testing
窗口仍显示默认下拉选项而不是no_results
文本。
所以,然后我意识到fillIn
实际上没有输入文本,我得出结论,事件是由于按键触发的。考虑到这一点,我尝试重写这样的测试:
test('user can create a new owner from the dialog box', function() {
visit('/collaborate/foo/430/bar/2/edit');
andThen(function() {
click('.chosen-container > .chosen-single');
andThen(function() {
keyEvent('.chosen-container > .chosen-drop > .chosen-search > input', 'keypress', 84);
andThen(function() {
equal(find('.chosen-container > .chosen-drop > .chosen-results > .no-results').text(), 'No results found. Add a new option: "t"');
});
});
});
});
我也尝试触发keyup
和keydown
事件。结果与第一次测试相同。
我还尝试了组件的单元测试,但似乎我无法触发事件,组件正在侦听chosen:no_results
事件。
最后,我尝试直接在测试中触发chosen:no_results
事件:
test('user can create a new option from the dialog box', function() {
visit('/collaborate/foo/430/bar/2/edit');
andThen(function() {
click('.chosen-container > .chosen-single');
andThen(function() {
fillIn('.chosen-container > .chosen-drop > .chosen-search > input', 'New Test');
andThen(function() {
triggerEvent(find('.chosen-container').parent().find('select'), 'chosen:no_results');
andThen(function() {
equal(find('.chosen-container > .chosen-drop > .chosen-results > .no-results').text(), 'No results found. Add a new option: "New Test"');
});
});
});
});
});
显然,该事件会触发,但我收到错误。似乎chosen:no_results
事件在事件选项中的Chosen对象中传递,这是我的组件运行所需的。我无法从测试本身访问它,所以我需要找到一些方法来使用测试助手来触发它,或者我必须找到一些方法从测试本身传入Chosen对象。
我将如何测试?