Skip to content

Commit 6ed3785

Browse files
authored
fix(no-node-access): stop reporting focus() usage (#1028)
Closes #1027
1 parent 67f08f9 commit 6ed3785

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

docs/rules/no-node-access.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ Disallow direct access or manipulation of DOM nodes in favor of Testing Library'
88

99
## Rule Details
1010

11-
This rule aims to disallow direct access and manipulation of DOM nodes using native HTML properties and methods — including traversal (e.g. `closest`, `lastChild`) as well as direct actions (e.g. `click()`, `focus()`). Use Testing Library’s queries and userEvent APIs instead.
11+
This rule aims to disallow direct access and manipulation of DOM nodes using native HTML properties and methods — including traversal (e.g. `closest`, `lastChild`) as well as direct actions (e.g. `click()`, `select()`). Use Testing Library’s queries and userEvent APIs instead.
12+
13+
> [!NOTE]
14+
> This rule does not report usage of `focus()`, because imperative focus (e.g. `getByText('focus me').focus()`) is recommended over `fireEvent.focus()`.
15+
> If an element is not focusable, related assertions will fail, leading to more robust tests. See [Testing Library Events Guide](https://testing-library.com/docs/guide-events/) for more details.
1216
1317
Examples of **incorrect** code for this rule:
1418

@@ -104,3 +108,7 @@ expect(container.firstChild).toMatchSnapshot();
104108
- [`Document`](https://developer.mozilla.org/en-US/docs/Web/API/Document)
105109
- [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element)
106110
- [`Node`](https://developer.mozilla.org/en-US/docs/Web/API/Node)
111+
112+
### Testing Library Guides
113+
114+
- [Testing Library Events Guide](https://testing-library.com/docs/guide-events/)

lib/utils/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,7 @@ const METHODS_RETURNING_NODES = [
114114
'querySelectorAll',
115115
] as const;
116116

117-
const EVENT_HANDLER_METHODS = [
118-
'click',
119-
'focus',
120-
'blur',
121-
'select',
122-
'submit',
123-
] as const;
117+
const EVENT_HANDLER_METHODS = ['click', 'blur', 'select', 'submit'] as const;
124118

125119
const ALL_RETURNING_NODES = [
126120
...PROPERTIES_RETURNING_NODES,

0 commit comments

Comments
 (0)