简体   繁体   English

使用@testing-library/user-event 进行虚假点击

[英]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.

相关问题 为什么 @testing-library/user-event 上不存在 clear 方法 - Why clear method not exist on @testing-library/user-event 反应测试 | 从“@testing-library/user-event”导入 userEvent 会生成错误“toBeInTheDocument 不是函数” - React Testing | importing userEvent from '@testing-library/user-event' generates error "toBeInTheDocument is not a function" 如何使用@testing-library/user-event 版本 14 在 React 中测试 `onKeyDown` 道具? - How can I test `onKeyDown` prop in React with @testing-library/user-event version 14? 为什么@testing-library/user-event 不能与输入元素交互? - Why can't @testing-library/user-event interact with input elements? 假计时器不适用于最新版本的用户事件? - Fake timers doesn't work with latest version of user-event? 使用 fireEvent 传递自定义事件属性(testing-library 和 jest) - pass custom event properties with fireEvent (testing-library and jest) 在使用 testing-library 进行测试时使用 getByRole 时无法获取输入的名称 - Unable to get the name of the input when using getByRole while testing with testing-library 使用 Jest 和 @testing-library/react-hooks 在 TypeScript 中单元测试自定义 React Hook - Unit Testing Custom React Hooks in TypeScript using Jest and @testing-library/react-hooks useFakeTimers 在玩笑/测试库中不起作用 - useFakeTimers not working in jest/testing-library 测试库自定义查询打字稿错误 - Testing-library custom query Typescript error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM