简体   繁体   English

在茉莉花单元测试中模拟鼠标悬停?

[英]Simulate mouseover in jasmine unit test?

Apologies if this is covered elsewhere, struggled to find comprehensive result on search. 如果在其他地方对此表示歉意,则很难在搜索中找到全面的结果。

I've got a Javascript app that has audio descriptions on mouseover for certain elements. 我有一个Javascript应用,该应用在鼠标悬停时具有某些元素的音频描述。 These are implemented using mouseover listeners like so (pseudo-code!) 这些是使用像这样的鼠标悬停监听器实现的(伪代码!)

  menuOption.addEventListener("mouseover", function(){
    audio.play("menuoption"); 
  });

In my test I've got something like: 在我的测试中,我得到了类似的东西:

  spyOn(audio, "play")

  menuOption.mouseover()

  expect(audio.play.calls.count()).toBe(1);

This mouseover doesn't seem to fire. 鼠标悬停似乎没有触发。 I'm using jasmine and phantomjs. 我正在使用茉莉花和phantomjs。 When I change mouseover to click it works ok so I'm inclined to think my tests are fine and its mouseover thats a problem? 当我将鼠标悬停更改为单击时,它可以正常工作,因此我倾向于认为我的测试很好,并且其鼠标悬停就是问题吗?

Just as element.click() is non-standard, so is element.mouseover() . 正如element.click()是非标准的,所以是element.mouseover() You can try to explicitly generate the event (based on this answer by torazaburo): 您可以尝试显式生成事件(基于torazaburo的回答 ):

function mouseover(el){
    var ev = document.createEvent("MouseEvent");
    ev.initMouseEvent(
        "mouseover",
        true /* bubble */, true /* cancelable */,
        window, null,
        0, 0, 0, 0, /* coordinates */
        false, false, false, false, /* modifier keys */
        0 /*left*/, null
    );
    el.dispatchEvent(ev);
}
mouseover(menuOption);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM