概念: 查询通过 Role
概念检查 中等
题目
为什么 prefer getByRole over getByTestId in React 测试库?
提示
Think 关于谁 uses accessibility roles — screen readers and 用户.
解释
Preferring getByRole over getByTestId encourages 测试方式用户 interact 使用 app. Roles (如 'button', 'textbox', 'heading') 是部分的 accessibility 树 and reflect 如何 screen readers and 真实用户查找元素. 如果你 能查询通过 role, 你的组件是 accessible. getByTestId 添加测试-仅 attributes 那个 don't 验证 accessibility.
相关资源
相关课程
- React 测试
测试 React 组件.
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。