按角色区域进行描述,而不是为反应测试库工作

发布于 2025-02-11 17:23:48 字数 753 浏览 0 评论 0原文

我在页面中使用区域角色作为部分。

它描述了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 技术交流群。

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

发布评论

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

评论(1

苦笑流年记忆 2025-02-18 17:23:48

您的测试应适用于期望(getByrole('heading'))。

your test should work with expect(getByRole('heading')).toHaveTextContent("hello region");

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