gpt4 book ai didi

javascript - 如何用 Jasmine 监视 stopPropagation?

转载 作者:行者123 更新时间:2023-12-03 02:29:09 24 4
gpt4 key购买 nike

基本上,我有一个链接,单击后会停止传播。

html

<a href="#" id="myLink">My Link</a>

JavaScript

const a = document.querySelector('#myLink');

a.addEventListener('click', (e) => { e.stopPropagation(); });

我想测试我的链接是否真的会停止传播。但是,我的单元测试失败了......

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();
});

最佳答案

如果您有更优雅的答案,请告诉我。经过一番尝试,我找到了解决这个问题的方法。

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();
});

关于javascript - 如何用 Jasmine 监视 stopPropagation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48809622/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com