Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/rules/prefer-screen-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ render(<Component />).getByText('foo');
// calling a query from a custom `render` method that returns an array
const [getByText] = myCustomRender(<Component />);
getByText('foo');

// calling a top-level query with a specific container
const popup = screen.getByTestId('modal-dialog');
getByText(popup, 'label');
```

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.

Examples of **correct** code for this rule:

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

// querying inside a specific subtree
const popup = screen.getByTestId('modal-dialog');
within(popup).getByText('label');

// calling a query method returned from a within call
const { getByText } = within(screen.getByText('foo'));
getByText('foo');
Expand Down