reactjs - Can RegEx be used with React Testing Library to check classnames?
问题描述
Here's a line from one of my React Testing Library tests:
expect(queryByTestId('tile-sample-service')).toHaveClass('regularTile-0-2-24', 'btn', 'btn-outline-secondary');
While it works, the test is fragile because every time the structure of the component changes, I need to go back and fix the numbers, which have changed.
Is there a way to use toHaveClass
with RegEx queries or is there some other way to check if classes are present but avoid having to add things like "0-2-24" ?
解决方案
是的,对于 JS 中的某些 CSS 生成的类名,有时数字后缀会发生变化。
像这样的东西应该工作:
const currentLinkAnchorEl = getByText(container, 'My Currently Active Link').closest('a');
expect(currentLinkAnchorEl.className).toMatch(/mySelectedActiveClassName/)
推荐阅读
- c# - 如果我不关闭 StardandInput,C# 重定向其他控制台应用程序 StandardOutput 失败
- python - 保留 JSON obj 中的某些值,使其结构更深
- python - 检查 2D numpy 数组中的每个元素是否都在集合中
- .net - 将带有 OdbcConnection 的 .Net/C# 项目迁移到带有 Informix DB 的 SqlConnection
- python-3.x - DRF 标注作者有书的所有店铺
- python - 有没有一种巧妙的方法可以从这个字符串列表中提取日期,其中一些字符串没有日期模式?
- angular - 带有远程 URL 的 SimpleLightbox
- arrays - 将文件中的多种数据类型扫描到c中的一个数组中
- sequence - 考虑整数溢出的重新排序序列
- angular - Angular 12 http请求返回未定义的参数