单元测试 React 单击外部组件使用 Jest 和 React 测试库 [英] Unit testing React click outside component using Jest and react testing library

查看:53
本文介绍了单元测试 React 单击外部组件使用 Jest 和 React 测试库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里找到了这个实现,但它使用了酶.

I found this implementation here but its using enzyme.

单元测试 React 点击组件外

向窗口添加事件也不起作用:

Adding an event to window does not work either:

window.addEventListener('click', () => {
      console.log('click on window');
    });

有没有人在上面使用 jest 和@testing-library/react"遇到过这个问题?

Has anyone came across this issue above using jest and "@testing-library/react"?

推荐答案

正如@Giorgio 提到的:

as @Giorgio mentioned:

fireEvent.click(document)

这篇关于单元测试 React 单击外部组件使用 Jest 和 React 测试库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆