首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用testing-library测试ag-grid中的内容?

如何使用testing-library测试ag-grid中的内容?
EN

Stack Overflow用户
提问于 2020-09-19 22:28:39
回答 1查看 3.1K关注 0票数 4

我正在尝试编写一些简单的测试,以便我想要呈现的头部和数据可以按预期显示出来。我创建了一个repo - https://github.com/olore/ag-grid-testing-library来重现。在浏览器中打开该表时,它看起来与我预期的一样。

代码语言:javascript
复制
<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>

以及测试

代码语言:javascript
复制
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列失败)

代码语言:javascript
复制
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}/>);
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-08 10:50:18

ag-grid使用Column Virtualisation,因此这里的解决方案似乎是通过<AgGridReact>元素的suppressColumnVirtualisation属性禁用它。

代码语言:javascript
复制
  <AgGridReact
        suppressColumnVirtualisation={true}
        ...

轰隆隆!所有的测试都通过了!

实际上,理想的做法可能是只在测试期间抑制这一点:

代码语言:javascript
复制
        suppressColumnVirtualisation={process.env.NODE_ENV === "test"}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63969843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档