[英]How to spy stopPropagation with Jasmine?
basically, I have a link that stops propagation when clicked. 基本上,我有一个链接,单击该链接即可停止传播。
html HTML
<a href="#" id="myLink">My Link</a>
JavaScript JavaScript的
const a = document.querySelector('#myLink');
a.addEventListener('click', (e) => { e.stopPropagation(); });
I want to test if my link will really stop the propagation. 我想测试我的链接是否真的会停止传播。 However, my unit test is failing...
但是,我的单元测试失败了...
Jasmine 茉莉花
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();
});
Please, let me know if you have a more elegant answer. 请让我知道您是否有一个更优雅的答案。 After some tries, I figured out how to solve this problem.
经过一些尝试,我想出了解决该问题的方法。
Jasmine 茉莉花
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.