如何在单元测试键盘可访问性时切换到非标准元素?

huangapple go评论96阅读模式
英文:

How do I tab to a non-standard element while unit-testing for keyboard accessibility?

问题

  1. ## 背景
  2. 我有一个React/TypeScript项目,我使用JestReact Testing Library (RTL)进行测试。我有一个通过的测试,测试了RTL`userEvent.tab()`,通过按Tab键来测试按钮的可访问性。
  3. ```javascript
  4. test('that the button is keyboard accessible', () => {
  5. // 安排
  6. const page = render(<BrowserRouter><Page/></BrowserRouter>).container;
  7. const button: HTMLElement = within(page).getByText('Form button');
  8. // 行动
  9. for (let i = 0; i < 5; ++i) {
  10. userEvent.tab();
  11. }
  12. // 断言
  13. expect(button).toHaveFocus();
  14. });

<br>

问题

我有另一个类似的键盘可访问性测试失败了:

  1. test('that My button is keyboard accessible', () => {
  2. // 安排
  3. const nextPage: HTMLElement = render(<NextPage />).container;
  4. const button = screen.getByText('My button');
  5. // 行动
  6. for (let i = 0; i < 3; ++i) {
  7. userEvent.tab();
  8. }
  9. // 断言
  10. expect(button).toHaveFocus();
  11. });

错误信息:

  1. 预期具有焦点的元素:
  2. <my-button>My button</my-button> // 注意:`<my-button>`是我公司设计系统中特定的按钮组件。
  3. 接收到具有焦点的元素:
  4. <body><div><div class="nextPage">... 更多标签在这里...<div class="bodyButtons"><my-button>My button</my-button></div></div></div></div></div></body>

无论我多少次使用userEvent.tab(),接收到具有焦点的元素始终是<body>。我认为这是因为尝试定位公司设计系统中的非标准按钮(<my-button>)引起的。

<br>

问题

如何在我的单元测试中使用键盘访问此页面上的"Account"按钮? 在部署的应用程序上键盘操作正常工作。

<br>

我尝试过的事情

  • 根据第一个单元测试使用within
  • userEvent.tab()的次数从0更改为1、2、5、10
  • 使用fireEvent而不是userEvent
  • 渲染组件时不使用container
  • 最初在按Tab键之前使用button.focus(),但我也无法将焦点放在按钮上
  • <my-button>更改为使用标准HTML标签<button>
  • 使用querySelector('my-button')获取按钮
  1. <details>
  2. <summary>英文:</summary>
  3. ## Background
  4. I have a React/TypeScript project that I test with Jest and React Testing Library (RTL). I have one passing test that tests keyboard accessibility with RTL&#39;s `userEvent.tab()` by tabbing to a button.

test('that the button is keyboard accessible', () => {
// Arrange
const page = render(<BrowserRouter><Page/></BrowserRouter>).container;
const button: HTMLElement = within(page).getByText('Form button');

  1. // Act
  2. for (let i = 0; i &lt; 5; ++i) {
  3. userEvent.tab();
  4. }
  5. // Assert
  6. expect(button).toHaveFocus();

});

  1. &lt;br&gt;
  2. ## Problem
  3. I have another similar keyboard accessibility test that fails:

test('that My button is keyboard accessible', () => {
// Arrange
const nextPage: HTMLElement = render(<NextPage />).container;
const button = screen.getByText('My button');

  1. // Act
  2. for (let i = 0; i &lt; 3; ++i) {
  3. userEvent.tab();
  4. }
  5. // Assert
  6. expect(button).toHaveFocus();

});

  1. The error:

Expected element with focus:
<my-button>My button</my-button> // Note: &lt;my-button&gt; is a button component specific to the design system my company uses.
Received element with focus:
<body><div><div class="nextPage">... more tags here...<div class="bodyButtons"><my-button>My button</my-button></div></div></div></div></body>

  1. No matter how many times I `userEvent.tab()`, the received element with focus is always `&lt;body&gt;`. I assume this is due to attempting to target a non-standard button (`&lt;my-button&gt;`) that is part of my company&#39;s design system.
  2. &lt;br&gt;
  3. ## Question
  4. **How do I keyboard to the Account button on this page with my unit test?** Keyboarding on the deployed app works just fine.
  5. &lt;br&gt;
  6. ## Things I&#39;ve tried
  7. - `within` per the first unit test
  8. - Changing the number of `userEvent.tab()`s from 0 to 1 to 2 to 5 to 10
  9. - `fireEvent` instead of `userEvent`
  10. - Rendering the component without the container
  11. - Initially using `button.focus()` before tabbing, but I can&#39;t focus the button either
  12. - Changing `&lt;my-button&gt;` to `&lt;button&gt;` to use a standard HTML tag
  13. - Getting the button with `querySelector(&#39;my-button&#39;)`
  14. </details>
  15. # 答案1
  16. **得分**: 0
  17. > 如何在我的单元测试中使用键盘访问此页面上的“Account”按钮?
  18. ## 解决方案
  19. 我给`<my-button>`添加了`tabindex=0`属性。这使按钮可以在程序中获得焦点。
  20. 产品代码:
  21. ```jsx
  22. <my-button onClick={handleClick} tabIndex='0'>我的按钮</my-button>

测试代码:

  1. test('确保我的按钮可以通过键盘访问', () => {
  2. // 安排
  3. render(<NextPage/>);
  4. const button: HTMLElement = screen.getByText('我的按钮');
  5. // 操作
  6. for (let i = 0; i < 3; ++i) {
  7. userEvent.tab();
  8. }
  9. // 断言
  10. expect(button).toHaveFocus();
  11. });

更多信息请参阅MDN上的tabIndex文档

英文:

> How do I keyboard to the Account button on this page with my unit test?

Solution

I gave &lt;my-button&gt; the tabindex=0 attribute. This made the button programmatically focusable.

The product code:

  1. &lt;my-button onClick={handleClick} tabIndex=&#39;0&#39;&gt;My button&lt;/my-button&gt;

The test:

  1. test(&#39;that My button is keyboard accessible&#39;, () =&gt; {
  2. // Arrange
  3. render(&lt;NextPage/&gt;);
  4. const button: HTMLElement = screen.getByText(&#39;My button&#39;);
  5. // Act
  6. for (let i = 0; i &lt; 3; ++i) {
  7. userEvent.tab();
  8. }
  9. // Assert
  10. expect(button).toHaveFocus();
  11. });

More info from MDN on tabIndex

huangapple
  • 本文由 发表于 2023年5月30日 05:20:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定