简体   繁体   English

如何在 jasmine 单元测试中的元素上触发 keydown

[英]How to trigger keydown on an element in jasmine unit test

I am writing a jasmine unit test for a function that receives 'keydown' event.我正在为接收“keydown”事件的 function 编写 jasmine 单元测试。 It's just a plain javascript(not using any framework)它只是一个普通的 javascript(不使用任何框架)

var event = document.createEvent('Event');
event.keyCode = 84; 
event.initEvent('keydown');
event.target = $('#selector');
myFunction(event);

When I console log the event in myFunction This is what I get:当我在 myFunction 中控制台记录事件时,这就是我得到的:

{"returnValue":true,"timeStamp":1607595872162,"eventPhase":0,"target":null,"defaultPrevented":false,"srcElement":null,"type":"keydown","cancelable":false,"currentTarget":null,"bubbles":false,"cancelBubble":false,"keyCode":84}

The 'target' property is null “目标”属性是 null

How do I set the target value?如何设置目标值? Please help请帮忙

It looks like you're looking to dispatch the event you created .看起来您正在寻找发送您创建的事件 The MDN article I linked indicates the target parameter is used to initialize target in the event.我链接的 MDN 文章指出target参数用于在事件中初始化target In your case, I would do something like:在你的情况下,我会做类似的事情:

var event = document.createEvent('Event');
event.keyCode = 84; 
event.initEvent('keydown');
$('#selector')[0].dispatchEvent(event);

This makes the assumption that you've bound myFunction to #selector 's keydown event via:这假设您已通过以下方式将myFunction绑定到#selectorkeydown事件:

document.getElementById("selector").addEventListener('keydown', myFunction);

or或者

$('#selector').on('keydown', myFunction);

If you're not binding the function with an event listener, you could leverage duck typing by building a fake event object and passing that fake event to myFunction :如果您没有将 function 与事件侦听器绑定,则可以通过构建假事件 object 并将该假事件传递给myFunction来利用鸭子类型

myFunction({
    type: 'keydown',
    keyCode: 84,
    target: $('#selector')[0]
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AngularJS - 在 Jasmine 指令单元测试中,如何触发 element.on("$destroy") 事件? - AngularJS - In Jasmine directive unit test, how can I trigger element.on("$destroy") event? 茉莉花测试模拟Tab键按下并检测新聚焦的元素 - Jasmine test simulate tab keydown and detect newly focused element 如何在 Jasmine 中对 httpParams 进行单元测试 - how to unit test httpParams in Jasmine 如何在Jasmine单元测试中触发Angular Directive的click事件? - How do I trigger a click event in a Jasmine unit test for an Angular Directive? 在Jasmine单元测试中,如何强制失败回调触发失败请求的结果? - In a Jasmine unit test, how do I force a failure callback to trigger that would result from a failed request? 如何使用茉莉花模拟对JS异步进行单元测试 - How to unit test js asynch with jasmine mocking 如何将Jasmine单元测试结果作为字符串返回? - How to return Jasmine unit test results as a string? 如何在 Jasmine 中编写单元测试到嵌套函数 - How to write unit test in Jasmine to nested function 如何使用茉莉花测试对 mapbox 弹出窗口进行单元测试? - How to unit test a mapbox popup with jasmine testing? 如何为 addEventListener 编写 Jasmine 单元测试? - How to write a Jasmine Unit Test for addEventListener?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM