按角色区域进行描述,而不是为反应测试库工作
我在页面中使用区域角色作为部分。
它描述了ARIA标签,其中该部分的作用。
我正在编写单元测试,以确保存在如 codesandbox 。
您好组件:
export default ({ name }) => <h1>Hello {name}!</h1>;
测试:
test("region described by hello region should exists in the dom", () => {
const { getByText, getByRole } = render(<Hello name="Jill" />);
expect(
getByRole("region", { description: "hello region" })
).toBeInTheDocument();
});
I am using region role for a section in my page.
It has describedby aria label which speak of what that section does.
I am writing unit test to make sure that section exists as shown in codesandbox.
hello component:
export default ({ name }) => <h1>Hello {name}!</h1>;
test:
test("region described by hello region should exists in the dom", () => {
const { getByText, getByRole } = render(<Hello name="Jill" />);
expect(
getByRole("region", { description: "hello region" })
).toBeInTheDocument();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的测试应适用于
期望(getByrole('heading'))。
your test should work with
expect(getByRole('heading')).toHaveTextContent("hello region");