[英]How to spy stopPropagation with Jasmine?
基本上,我有一個鏈接,單擊該鏈接即可停止傳播。
HTML
<a href="#" id="myLink">My Link</a>
JavaScript的
const a = document.querySelector('#myLink');
a.addEventListener('click', (e) => { e.stopPropagation(); });
我想測試我的鏈接是否真的會停止傳播。 但是,我的單元測試失敗了...
茉莉花
it('ensure that iframe\'s html links doesn\'t trigger actions', () => {
spyOn(window.event, 'stopPropagation');
const a = document.querySelector('#myLink');
a.dispatchEvent(new Event('click'));
expect(window.event.stopPropagation).toHaveBeenCalled();
});
請讓我知道您是否有一個更優雅的答案。 經過一些嘗試,我想出了解決該問題的方法。
茉莉花
it('ensure that iframe\'s html links doesn\'t trigger actions', () =>
{
const ev = new Event('click');
spyOn(ev, 'stopPropagation');
const a = document.querySelector('#myLink');
a.dispatchEvent(ev);
expect(ev.stopPropagation).toHaveBeenCalled();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.