Javascript 模拟在Karma/Jasmine测试中输入时按Enter键
我试图在一个角度自定义组件中测试用户搜索功能。我尝试向指令模板中添加文本并触发搜索的输入(也是唯一的输入)是:Javascript 模拟在Karma/Jasmine测试中输入时按Enter键,javascript,jquery,angularjs,d3.js,karma-jasmine,Javascript,Jquery,Angularjs,D3.js,Karma Jasmine,我试图在一个角度自定义组件中测试用户搜索功能。我尝试向指令模板中添加文本并触发搜索的输入(也是唯一的输入)是: <input type="text" ng-model="searchInput" class="form-control search"/> 谢谢我建议查看输入框上的事件侦听器,因为输入框不一定总是由“回车”键触发。它可能正在监听的其他一些可能的事件是,“模糊”、“按键”、“更改”。根据它是一个事件监听器还是一个基于任何事件的监听器,您必须相应地触发它 抱歉,这有点含
<input type="text" ng-model="searchInput" class="form-control search"/>
谢谢我建议查看输入框上的事件侦听器,因为输入框不一定总是由“回车”键触发。它可能正在监听的其他一些可能的事件是,“模糊”、“按键”、“更改”。根据它是一个事件监听器还是一个基于任何事件的监听器,您必须相应地触发它 抱歉,这有点含糊不清,但是如果不知道连接到输入框的事件侦听器,很难说
希望这有帮助 您需要将JQuery.Event与触发控制器中最终搜索的内容相匹配,因此,如果控制器正在侦听按键,则需要确保JQuery.Event是“keypress”事件,但如果控制器正在侦听“keyup”,则需要将JQuery事件设置为“keyup”。您还需要在实际事件的回调中查找匹配项
it("should search for the specified nodes", function () {
var value = "User is not registered";
var e = jQuery.Event("keypress");
e.keyCode = 13;
// find the input
var directiveElementInput = diagramDirective.find("input");
// Set some text!
$(directiveElementInput).val(value).trigger("input");
// make sure the input has the value
expect(directiveElementInput).toHaveValue(value);
// execute the event on the input and check for the selected item
$(directiveElementInput).keypress(function () {
// do your check here for the matching item here
}).trigger(e);
});
单元测试用于测试组件的API。通过端到端测试测试UI~谢谢Phil,我知道量角器也是一个很好的工具。我很好奇为什么我不能像上面那样做。我在最近尝试的基础上添加了一些代码。
it("should search for the specified nodes", function () {
var value = "User is not registered";
var e = jQuery.Event("keypress");
e.keyCode = 13;
// find the input
var directiveElementInput = diagramDirective.find("input");
// Set some text!
$(directiveElementInput).val(value).trigger("input");
// make sure the input has the value
expect(directiveElementInput).toHaveValue(value);
// execute the event on the input and check for the selected item
$(directiveElementInput).keypress(function () {
// do your check here for the matching item here
}).trigger(e);
});