用于按键事件的 AngularJS 单元测试 [英] AngularJS unit test for keypress event

查看:27
本文介绍了用于按键事件的 AngularJS 单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个指令,为 keydown 和 keypress 事件绑定一个函数.当输入快捷键时,该指令为表单上的输入设置焦点.

I set up a directive that binds a function for the keydown and keypress events. The directive sets the focus for an input on a form when a shortcut key is entered.

<input type="text" id=txtField1" focus-key="a" />
<input type="text" id=txtField2" focus-key="b" />
<input type="text" id=txtField3" focus-key="c" />

是否可以触发按键事件以对我的指令进行单元测试?在此先感谢您的帮助.

Is it possible to trigger the keypress event for unit testing my directives? Thanks in advance for your help.

推荐答案

您可以将 jQuery 与 AngularJS 一起使用,并且您可以在 jQuery 中使用 trigger() API 调用.您可以将事件传递给触发器,在这种情况下,事件是

You can use jQuery with AngularJS, and you can do this fairly easily in jQuery with the trigger() API call. You can pass an event into trigger, in this case the event is the

var aEvent = jQuery.Event("keydown");
aEvent.which = 40; //this is the ASCII value of the key you want to press
$("input").trigger(aEvent);

然后对其他字符重复.

这篇关于用于按键事件的 AngularJS 单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆