[英]Fake click using @testing-library/user-event
I want to test that a tracking event occurs when clicking a link in react.我想测试单击反应中的链接时是否发生跟踪事件。 However I do get a
Error: Not implemented: navigation
as an effect of what happens when the link is clicked.但是我确实得到了一个
Error: Not implemented: navigation
是单击链接时发生的结果。 I am testing with jest and @testing-library/user-event .我正在用 jest 和@testing-library/user-event 进行测试。
I now want to mock or stub so that no navigation attempt takes place when the userEvent is fired as I am only interested to see the tracking event taking place.我现在想模拟或存根,以便在触发 userEvent 时不会发生导航尝试,因为我只想看到正在发生的跟踪事件。 How can I do this?
我怎样才能做到这一点?
userEvent.click(item);
expect(Ga.trackEvent).toHaveBeenCalledTimes(1);
expect(Ga.trackEvent).toHaveBeenCalledWith('myModal', 'open');
I think your test is actually trying to navigate using window.location我认为您的测试实际上是在尝试使用 window.location 进行导航
You will need to mock that out before trying the user event在尝试用户事件之前,您需要模拟它
let assignMock = jest.fn();
delete window.location;
window.location = { assign: assignMock };
afterEach(() => {
assignMock.mockClear();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.