![](/img/trans.png)
[英]How can I effectively use Jasmine to test javascript assets packaged via Jammit?
[英]How can I test this JavaScript effectively?
我編寫了一段代碼,該代碼在用戶突出顯示頁面上的某些文本時起作用。 該代碼可以正常工作(如下),但是我的問題是如何有效地對其進行測試? 有沒有一種方法可以模擬用戶選擇文本(特別是涉及mouseup
事件)。
可能的問題是,當發生mouseup
事件時檢查是否選擇了文本不是最好的方法嗎? 任何見解均表示贊賞。
var note = {
mouseHandler : function(e){
selection = window.getSelection();
if (selection.toString() !== '') {
note.selection = selection;
note.setAttributes();
note.hideOverlay();
note.placeOverlay();
}
}
}
理想情況下,我希望能夠使用測試代碼觸發此操作,以便確保note.placeOverlay()
發生
因此,在Jasmine中,您將監視window.getSelection
並在一種情況下返回一個字符串,而在另一種情況下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
功能,所以我可以完成答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.