Skip to content

Commit 96dd651

Browse files
committed
docs: update prefer-screen-queries with allowed methods and properties
1 parent ed77cec commit 96dd651

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

docs/rules/prefer-screen-queries.md

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Rule Details
44

5-
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.
6-
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result.
5+
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.
6+
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result. Given the screen component does not expose utility methods such as `rerender()` or the `container` property, it is correct to use the `render` response in those scenarios-
77

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

@@ -43,6 +43,17 @@ getByText('foo');
4343
// calling a method directly from a variable created by within
4444
const myWithinVariable = within(screen.getByText('foo'));
4545
myWithinVariable.getByText('foo');
46+
47+
// accessing the container and the base element
48+
const utils = render(baz);
49+
utils.container.querySelector('foo');
50+
utils.baseElement.querySelector('foo');
51+
52+
// calling the utilities function
53+
const utils = render(<Foo />);
54+
utils.rerender(<Foo />);
55+
utils.unmount();
56+
utils.asFragment();
4657
```
4758

4859
## Further Reading

tests/lib/rules/prefer-screen-queries.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ ruleTester.run(RULE_NAME, rule, {
3333
myWithinVariable.${queryMethod}('baz')
3434
`,
3535
})),
36+
{
37+
code: `
38+
const screen = render(baz);
39+
screen.container.querySelector('foo');
40+
`
41+
},
42+
{
43+
code: `
44+
const screen = render(baz);
45+
screen.baseElement.querySelector('foo');
46+
`
47+
},
48+
{
49+
code: `
50+
const utils = render(baz);
51+
screen.rerender();
52+
`
53+
},
54+
{
55+
code: `
56+
const utils = render(baz);
57+
utils.unmount();
58+
`
59+
},
60+
{
61+
code: `
62+
const utils = render(baz);
63+
utils.asFragment();
64+
`
65+
}
3666
],
3767

3868
invalid: [

0 commit comments

Comments
 (0)