0

我编写了一段代码,它作用于用户突出显示页面上的某些文本的事件。代码工作正常(如下),但我的问题是如何有效地测试它?有没有一种方法可以模拟用户选择文本(特别是涉及mouseup事件)。

也许问题是在事件发生时检查是否选择了文本mouseup不是最好的方法?任何见解都值得赞赏。

var note = {
  mouseHandler : function(e){
    selection = window.getSelection();
      if (selection.toString() !== '') {
       note.selection = selection;
       note.setAttributes();
       note.hideOverlay();
       note.placeOverlay();
    }
 }
}

理想情况下,我希望能够用测试代码触发它,这样我就可以确保note.placeOverlay()发生

4

1 回答 1

1

因此,在 Jasmine 中,您会监视window.getSelection并在一种情况下返回一个字符串,而在另一种情况下则不返回。然后你会检查这应该发生的note.placeOver事情。

spyOn(window, 'getSelection').andReturn('someString')
note.mouseHandler();
//test what you expect here


spyOn(window, 'getSelection').andReturn('')
note.mouseHandler();
//test that nothings happens here

也许你可以展示什么note.placeOver,所以我可以完成答案。

于 2013-02-28T08:15:01.650 回答