olo*_*ore 4 ag-grid ag-grid-react react-testing-library testing-library
我正在尝试编写一些简单的测试,让我想要呈现的标题和数据按预期显示。我创建了一个 repo - https://github.com/olore/ag-grid-testing-library来重现。该表在浏览器中打开时看起来就像我期望的那样。
<AgGridReact
columnDefs={ /* First 2 always findable, others never */
[
{ field: "make" },
{ field: "model" },
{ field: "price" },
{ field: "color" },
]}
rowData={
[{
make: "Toyota",
model: "Celica",
price: "35000",
color: "blue"
}]
}
pagination={true}></AgGridReact>
Run Code Online (Sandbox Code Playgroud)
和测试
test('renders all the headers', async () => {
const { getByText } = render(<GridExample />);
expect(getByText("Make")).toBeInTheDocument(); // PASS
expect(getByText("Model")).toBeInTheDocument(); // PASS
expect(getByText("Price")).toBeInTheDocument(); // FAIL
expect(getByText("Color")).toBeInTheDocument(); // FAIL
});
Run Code Online (Sandbox Code Playgroud)
在本地,前 2 列标题和数据是可访问的,但没有呈现其他列,正如我在 testing-library 的输出中看到的那样。我正在--env=jsdom-fourteen按照其他帖子的推荐使用。
奇怪的是,当在这个 repo 的代码和框中时,没有为测试呈现标题或数据,与本地一样,浏览器看起来是正确的。
https://codesandbox.io/s/gallant-framework-e54c7。然后我尝试等待gridReady
https://codesandbox.io/s/intelligent-minsky-wl17y,但没有任何区别。
编辑:还尝试直接调用 onGridReady 中的函数,同样的问题(前 2 列通过,第二个 2 失败)
test('renders all the headers', async (done) => {
let page;
const onReady = () => {
expect(page.getByText("Make")).toBeInTheDocument(); // PASS
expect(page.getByText("Model")).toBeInTheDocument(); // PASS
expect(page.getByText("Price")).toBeInTheDocument(); // FAIL
expect(page.getByText("Color")).toBeInTheDocument(); // FAIL
done();
}
page = render(<GridExample ready={onReady}/>);
});
Run Code Online (Sandbox Code Playgroud)
ag-grid 使用Column Virtualisation,所以这里的解决方案似乎是通过元素上的suppressColumnVirtualisation属性禁用它<AgGridReact>。
<AgGridReact
suppressColumnVirtualisation={true}
...
Run Code Online (Sandbox Code Playgroud)
繁荣!所有测试通过!
实际上,在测试期间只抑制它可能是理想的:
suppressColumnVirtualisation={process.env.NODE_ENV === "test"}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2284 次 |
| 最近记录: |