ag-grid - 使用 @testing-library/react 测试 ag-grid
问题描述
我正在尝试测试使用 ag-grid 的组件。
当我进行断言以检查是否呈现具有给定文本的单元格时,我收到以下错误:
expect(received).toBeInTheDocument() received value must be an HTMLElement or an SVGElement. Received has type: object
Received has value: HERE IS EXPECTED HTML FOR THE CELL I'VE BEEN LOOKING FOR
我已经为这个问题创建了代码沙箱(请参阅grid.test.tsx
)https://codesandbox.io/s/weathered-silence-4l1s1
解决方案
看起来 ag-grid 对 DOM 做了一些事情。在测试中渲染 ag-grid 后,它会在代码和框中使用时破坏所有检查 dom 的断言(即使是网格外的元素!)。我知道codeandbox 使用的是真正的浏览器dom 而不是jsdom。
我设法让它在我的本地机器上工作,但我不得不切换到 jsdom 14(检查https://github.com/ianschmitz/jest-environment-jsdom-fourteen)。
我还需要在我的测试设置中添加此代码(ag-grid 使用innerText
而不是textContent
,没有它我在渲染的单元格中没有文本)
Object.defineProperty(global.window.Element.prototype, 'innerText', {
set(value) {
this.textContent = value;
},
configurable: true
})
推荐阅读
- amazon-web-services - 批量替换 Terraform 中的文件内容
- c# - 有没有一种简单的方法来表示子组件中发生的数据更改?
- python - 如何在不使用和拆分测试集的情况下将我的数据集拆分为训练和验证?
- python - 在 Dataframes (Python) 中使用“For”和“If”的问题
- python - Python 3.6.1 找不到包和模块 [import]
- python - 将元组中的日期时间值转换为 YMD
- excel - 将此 vba 输出到单元格中
- objective-c - 将 Interface Builder 设置转换为 Objective-C
- r - 是否有一个 R 函数可以通过每天重复每月值来从每月数据到每日数据?
- node.js - 使用 mongodb 在用户的所有帖子中获取用户的点赞数