Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: update rule docs
  • Loading branch information
lourenci committed Jun 6, 2020
commit 6d680419c8325b100e35c5f829a8af0d51da8086
16 changes: 12 additions & 4 deletions docs/rules/prefer-screen-queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,30 @@

## Rule Details

DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
DOM Testing Library (and other Testing Library frameworks built on top of it) exports a `screen` object which has every query (the `container` and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result.

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

```js
// calling a query from the `render` method
const { getByText } = render(<Component />);
const { getByText, container } = render(<Component />);
getByText('foo');
container.querySelector('foo');

// calling a query from a variable returned from a `render` method
const utils = render(<Component />);
utils.getByText('foo');
utils.container.querySelector('foo');

// using after render
render(<Component />).getByText('foo');
render(<Component />).container.querySelector('foo');

// calling a query from a custom `render` method that returns an array
const [getByText] = myCustomRender(<Component />);
const [getByText, container] = myCustomRender(<Component />);
getByText('foo');
container.querySelector('foo');
```

Examples of **correct** code for this rule:
Expand All @@ -32,17 +36,21 @@ import { screen } from '@testing-library/any-framework';
// calling a query from the `screen` object
render(<Component />);
screen.getByText('foo');
screen.container.querySelector('foo');

// using after within clause
within(screen.getByTestId('section')).getByText('foo');
within(screen.container.querySelector('section')).getByText('foo');

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

// calling a method directly from a variable created by within
const myWithinVariable = within(screen.getByText('foo'));
myWithinVariable.getByText('foo');
myWithinVariable.container.querySelector('foo');
```

## Further Reading
Expand Down