[英]Dispatch an "oninput" event with a custom { target : { value } } data
I'm trying to use dispatchEvent
on an <input>
element in a unit test to trigger its "oninput" handler with a custom {target: {value: "data"}}
我试图在单元测试中的
<input>
元素上使用dispatchEvent
来触发其带有自定义{target: {value: "data"}}
“oninput”处理程序
This guide only tells you how to trigger a CustomEvent . 本指南仅告诉您如何触发CustomEvent 。 But I don't think that'll work as it only lets you specify a "details" property.
但我认为这行不通,因为它只允许您指定“详细信息”属性。 I'm actually trying to trigger it with something like
{target: {value: "data"}}
我实际上是在尝试用
{target: {value: "data"}}
东西来触发它
There's also InputEvent but it too seems to take the same "details" property only.还有InputEvent但它似乎也只采用相同的“详细信息”属性。
Here's my <input>
:这是我的
<input>
:
<input
oninput={handleOnInput} // jsx
>
...
function handleOnInput(e){
const data = e.target.value; // << needs to be mocked
}
And this is my test code:这是我的测试代码:
getThatInputEl().dispatchEvent(what can trigger "input" with {target: {value: "data"}} ?)
Is there a way you can dispatch an "input" event with a custom {target: value: "data"}
?有没有办法可以使用自定义
{target: value: "data"}
调度“输入”事件?
When you dispatch input
event on actual input
element e.target
is always set to the element, you can't override it - you can only pass object with additional data.当您在实际
input
元素上调度input
事件时, e.target
始终设置为该元素,您无法覆盖它 - 您只能传递带有附加数据的对象。 To solve your problem you just have to set value of input before dispatching event or you can test event handler itself要解决您的问题,您只需在调度事件之前设置输入值,或者您可以测试事件处理程序本身
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.