reactjs - React:使用 react-measure 的测试组件
问题描述
我正在尝试测试一个使用 react-measure 的组件。
组件非常简单
class AdjustableIframe extends React.Component {
props: {
children: node
};
render () {
return (
<Measure
bounds
onResize={(contentRect) => {
console.log("I want to handle resize")
}
}
>
{({ measureRef }) => (
<div ref={measureRef}>
{ this.props.children }
</div>
)
}
</Measure>
)
}
}
我正在尝试为这个组件编写一个测试(因为我想对 onResize 方法有一个复杂的逻辑)。但我不知道如何在我的 Jest 测试中模拟调整大小。
我试图调用 window.resizeTo 但这没有帮助。
谁能设法使它工作?
解决方案
你应该能够做这样的事情:
const windowSize = 500;
Object.assign(window, {
innerWidth: windowSize,
innerHeight: windowSize,
outerWidth: windowSize,
outerHeight: windowSize,
}).dispatchEvent(new Event('resize'));
这对我react-media
有用,希望它对你也有用!
推荐阅读
- javascript - 在javascript中使用带有数组的startsWith()
- c# - 构建 WCF CRUD 操作项目的问题
- python-3.x - 使用 lxml 查找所有数字属性
- linux - 如果我得到标准错误或标准输出,如何返回真/假?
- json - 发生模式时如何删除 Json 的一部分
- azure - 将 Service Fabric TCP 侦听器配置为侦听 1 个以上的端口
- java - 如何重构:外部类 SLList
内部类分配 . 内部类可以移动到自己的 .java 文件吗? - typescript - 如果密钥存在于对象 1 中,如何将值从对象 2 传输到对象 1
- excel - 提取特定域扩展的电子邮件 ID
- spring - 为 put 和 patch Spring data rest 分离验证器