Skip to content

Commit 80074af

Browse files
authored
docs: clarify scoped queries in prefer-screen-queries (#1342)
2 parents 95caba8 + edaefd8 commit 80074af

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

docs/rules/prefer-screen-queries.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ render(<Component />).getByText('foo');
3535
// calling a query from a custom `render` method that returns an array
3636
const [getByText] = myCustomRender(<Component />);
3737
getByText('foo');
38+
39+
// calling a top-level query with a specific container
40+
const popup = screen.getByTestId('modal-dialog');
41+
getByText(popup, 'label');
3842
```
3943

44+
When you need to query inside a specific subtree, prefer `within(subtree)` over importing or destructuring a top-level query and passing the subtree as the first argument. That keeps the query scoped while preserving the readability this rule encourages.
45+
4046
Examples of **correct** code for this rule:
4147

4248
```js
@@ -49,6 +55,10 @@ screen.getByText('foo');
4955
// using after within clause
5056
within(screen.getByTestId('section')).getByText('foo');
5157

58+
// querying inside a specific subtree
59+
const popup = screen.getByTestId('modal-dialog');
60+
within(popup).getByText('label');
61+
5262
// calling a query method returned from a within call
5363
const { getByText } = within(screen.getByText('foo'));
5464
getByText('foo');

0 commit comments

Comments
 (0)