基本上,我有一个链接可以在点击时停止传播。
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();
});
答案 0 :(得分:0)
如果你有更优雅的答案,请告诉我。经过一番尝试,我想出了如何解决这个问题。
<强>茉莉强>
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();
});