javascript - 使用@testing-library/user-event 进行虚假点击
问题描述
我想测试单击反应中的链接时是否发生跟踪事件。但是,我确实得到了Error: Not implemented: navigation
单击链接时发生的情况的影响。我正在用 jest 和@testing-library/user-event 进行测试。
我现在想模拟或存根,以便在触发 userEvent 时不会发生导航尝试,因为我只想看到正在发生的跟踪事件。我怎样才能做到这一点?
userEvent.click(item);
expect(Ga.trackEvent).toHaveBeenCalledTimes(1);
expect(Ga.trackEvent).toHaveBeenCalledWith('myModal', 'open');
解决方案
我认为您的测试实际上是在尝试使用 window.location 进行导航
在尝试用户事件之前,您需要模拟它
let assignMock = jest.fn();
delete window.location;
window.location = { assign: assignMock };
afterEach(() => {
assignMock.mockClear();
});
推荐阅读
- python - Python - 必需参数之前的可选参数
- css - Bootstrap 3 - 如何使用切换更改“.active”按钮的颜色?
- wpf - 如何检索从 wpf 应用程序打开的已保存文件
- java - Bitstamp API:始终接收 API0000(缺少密钥、签名和随机数参数。)
- julia - Julia 不更新其软件包
- javascript - 如何开玩笑地检查消极和积极的情况?
- javascript - 为什么实例化的对象即使在原型被重新分配给空对象后仍然具有原型的属性?
- php - 从 json 数据中获取特定值
- php - PHP数组删除元素或减法
- reactjs - 嵌套类在带有 webpack 的 CSS 模块中不起作用