我想测试一下,当点击react中的一个链接时,是否会发生跟踪事件。但是,我确实得到了一个Error: Not implemented: navigation,作为单击链接时发生的事情的效果。我正在使用jest和@testing-library/user-event进行测试。
我现在想模拟或存根,这样当userEvent被触发时不会发生导航尝试,因为我只对跟踪事件发生感兴趣。我该怎么做呢?
userEvent.click(item);
expect(Ga.trackEvent).toHaveBeenCalledTimes(1);
expect(Ga.trackEvent).toHaveBeenCalledWith('myModal', 'open'); 发布于 2020-12-04 18:50:41
我认为您的测试实际上是在尝试使用window.location导航
在尝试user事件之前,您需要对其进行模拟
let assignMock = jest.fn();
delete window.location;
window.location = { assign: assignMock };
afterEach(() => {
assignMock.mockClear();
});https://stackoverflow.com/questions/65141303
复制相似问题