获取 * Allby * Testing-Library查询的项目数量

发布于 2025-02-10 11:04:26 字数 223 浏览 1 评论 0原文

我熟悉的断言期望(element)。 我想测试表中的新行添加,
我可以在文档中主张一些特定项目的文本,
但是这种情况使我认为能够获得testing-library的findallby*查询的项目数量可能是有益的。
我在测试图书馆中查找了答案,但似乎没有简单的方法来检查Findallby*找到了多少个项目...

任何想法和建议都将受到赞赏

I'm familiar with the assertion of expect(element).toHaveLength(number),
I want to test for a new row addition in a table,
I could just assert some specific item's text to be in the document,
But the situation brought me to think it could be beneficial to be able to get the number of items found by testing-library's findAllby* queries.
I looked in the testing-library docs for an answer but it seems there is no easy way to check how many items were found by findAllby*...

Any ideas and suggestions will be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-02-17 11:04:26

您可以断言findAllby*请求结果之类的结果:

const rows = await findAllByText('my searched text');
expect(rows).toHaveLength(2);

react-testing-library的*all*查询返回阵列: https://testing-library.com/docs/queries/queries/about#types-of--ques-of-queries

You can assert on the length of findAllBy* request results like so :

const rows = await findAllByText('my searched text');
expect(rows).toHaveLength(2);

React-testing-library's *All* queries return arrays : https://testing-library.com/docs/queries/about#types-of-queries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文