我正在尝试编写一些简单的测试,以便我想要呈现的头部和数据可以按预期显示出来。我创建了一个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>以及测试
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
});在本地,前两个列的标题和数据是可访问的,但没有呈现其他列,正如我在testing library的输出中看到的那样。我正在使用其他帖子推荐的--env=jsdom-fourteen。
奇怪的是,在这个代码库的codesandbox中,没有为测试呈现任何头部或数据,就像本地一样,浏览器看起来很正确。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}/>);
});发布于 2020-11-08 10:50:18
ag-grid使用Column Virtualisation,因此这里的解决方案似乎是通过<AgGridReact>元素的suppressColumnVirtualisation属性禁用它。
<AgGridReact
suppressColumnVirtualisation={true}
...轰隆隆!所有的测试都通过了!
实际上,理想的做法可能是只在测试期间抑制这一点:
suppressColumnVirtualisation={process.env.NODE_ENV === "test"}https://stackoverflow.com/questions/63969843
复制相似问题