reactjs - 使用酶在反应测试中获取 html 元素
问题描述
如何在酶反应测试中获取 HTML 元素?我能够找到包装纸。但我想要 HTML 元素。因为我需要覆盖的代码块对未覆盖的 HTML 元素进行了 offsetParent 检查。下面是示例。
it('add class test', async () => {
const wrapper = mount((
<div class='ag-cell'>
<div class='ag-react-conatiner'></div>
<span anum={3} abool={false} />
<span anum="3" abool="false" />
</div>
));
const x = wrapper.find('.ag-react-conatiner').offsetParent;
console.log(wrapper.find('.ag-react-conatiner').offsetParent);
}
我能够得到 wrapper.find('.ag-react-conatiner')。我怎样才能得到它的 offsetParent?
解决方案
为什么不将属性 data-testid='something' 添加到您的元素使用中
const wrapper = mount(<MyComponent />);
expect(wrapper.find('[data-testid="something"]')).to.have.lengthOf(1);
使用上述属性可以清楚地表明它用于测试,不像在开发中可以轻松重命名等的类。
推荐阅读
- apache-spark - 当我们尝试将 Spark DataFrame 写入 Cassandra 时,Cassandra 类型如何在内部转换数据类型?
- python - 使用多个时间戳对多个用户数据进行分组
- wpf - 如果用户输入了无效的电子邮件地址,则禁用按钮 - WPF
- javascript - AJAX JSON 加载的表单按钮刷新页面而不是再次使用 AJAX
- php - PHP 致命错误:未捕获的异常:错误:1408F10B:SSL 例程:ssl3_get_record:/api.genius.com 的版本号错误
- javascript - 更新 Dynamodb 中的项目?
- python - 使用shutil和重命名时出错
- rust - 如何使 #![no_main] 属性有条件?
- javascript - 需要为多个搜索文件夹创建搜索值为 null
- google-analytics - Google Analytics(分析)命中未发送