Skip to content

Commit ed77cec

Browse files
authored
Revert "fix(prefer-screen-queries): take container into account (testing-library#150)"
This reverts commit a783036.
1 parent f1a4045 commit ed77cec

File tree

3 files changed

+18
-28
lines changed

3 files changed

+18
-28
lines changed

docs/rules/prefer-screen-queries.md

+4-12
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,26 @@
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 (plus the `container` and a `debug` method). This works better with autocomplete and makes each test a little simpler to write and maintain.
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.
66
This rule aims to force writing tests using queries directly from `screen` object rather than destructuring them from `render` result.
77

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

1010
```js
1111
// calling a query from the `render` method
12-
const { getByText, container } = render(<Component />);
12+
const { getByText } = render(<Component />);
1313
getByText('foo');
14-
container.querySelector('foo');
1514

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

2119
// using after render
2220
render(<Component />).getByText('foo');
23-
render(<Component />).container.querySelector('foo');
2421

2522
// calling a query from a custom `render` method that returns an array
26-
const [getByText, container] = myCustomRender(<Component />);
23+
const [getByText] = myCustomRender(<Component />);
2724
getByText('foo');
28-
container.querySelector('foo');
2925
```
3026

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

4136
// using after within clause
4237
within(screen.getByTestId('section')).getByText('foo');
43-
within(screen.getByTestId('section')).container.querySelector('foo');
4438

4539
// calling a query method returned from a within call
46-
const { getByText, container } = within(screen.getByText('foo'));
40+
const { getByText } = within(screen.getByText('foo'));
4741
getByText('foo');
48-
container.querySelector('foo');
4942

5043
// calling a method directly from a variable created by within
5144
const myWithinVariable = within(screen.getByText('foo'));
5245
myWithinVariable.getByText('foo');
53-
myWithinVariable.container.querySelector('foo');
5446
```
5547

5648
## Further Reading

lib/rules/prefer-screen-queries.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const RULE_NAME = 'prefer-screen-queries';
1212
export type MessageIds = 'preferScreenQueries';
1313
type Options = [];
1414

15-
const ALL_QUERIES_COMBINATIONS_REGEXP = [...ALL_QUERIES_COMBINATIONS, 'container'].join('|');
15+
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');
1616

1717
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
1818
name: RULE_NAME,
@@ -93,7 +93,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
9393
isIdentifier(node) &&
9494
isMemberExpression(node.parent) &&
9595
isCallExpression(node.parent.object) &&
96-
isIdentifier(node.parent.object.callee) &&
96+
isIdentifier(node.parent.object.callee) &&
9797
node.parent.object.callee.name !== 'within'
9898
) {
9999
reportInvalidUsage(node);

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

+12-14
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { ALL_QUERIES_COMBINATIONS } from '../../../lib/utils';
44

55
const ruleTester = createRuleTester();
66

7-
const ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER = [...ALL_QUERIES_COMBINATIONS, 'container']
8-
97
ruleTester.run(RULE_NAME, rule, {
108
valid: [
11-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
9+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
1210
code: `screen.${queryMethod}()`,
1311
})),
1412
{
@@ -17,19 +15,19 @@ ruleTester.run(RULE_NAME, rule, {
1715
{
1816
code: `component.otherFunctionShouldNotThrow()`,
1917
},
20-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
18+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
2119
code: `within(component).${queryMethod}()`,
2220
})),
23-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
21+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
2422
code: `within(screen.${queryMethod}()).${queryMethod}()`,
2523
})),
26-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
24+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
2725
code: `
2826
const { ${queryMethod} } = within(screen.getByText('foo'))
2927
${queryMethod}(baz)
3028
`,
3129
})),
32-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
30+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
3331
code: `
3432
const myWithinVariable = within(foo)
3533
myWithinVariable.${queryMethod}('baz')
@@ -38,7 +36,7 @@ ruleTester.run(RULE_NAME, rule, {
3836
],
3937

4038
invalid: [
41-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
39+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
4240
code: `${queryMethod}()`,
4341
errors: [
4442
{
@@ -50,7 +48,7 @@ ruleTester.run(RULE_NAME, rule, {
5048
],
5149
})),
5250

53-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
51+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
5452
code: `render().${queryMethod}()`,
5553
errors: [
5654
{
@@ -62,7 +60,7 @@ ruleTester.run(RULE_NAME, rule, {
6260
],
6361
})),
6462

65-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
63+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
6664
code: `component.${queryMethod}()`,
6765
errors: [
6866
{
@@ -73,7 +71,7 @@ ruleTester.run(RULE_NAME, rule, {
7371
},
7472
],
7573
})),
76-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
74+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
7775
code: `
7876
const { ${queryMethod} } = render()
7977
${queryMethod}(baz)
@@ -87,7 +85,7 @@ ruleTester.run(RULE_NAME, rule, {
8785
},
8886
],
8987
})),
90-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
88+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
9189
code: `
9290
const myRenderVariable = render()
9391
myRenderVariable.${queryMethod}(baz)
@@ -101,7 +99,7 @@ ruleTester.run(RULE_NAME, rule, {
10199
},
102100
],
103101
})),
104-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
102+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
105103
code: `
106104
const [myVariable] = render()
107105
myVariable.${queryMethod}(baz)
@@ -115,7 +113,7 @@ ruleTester.run(RULE_NAME, rule, {
115113
},
116114
],
117115
})),
118-
...ALL_QUERIES_COMBINATIONS_PLUS_CONTAINER.map(queryMethod => ({
116+
...ALL_QUERIES_COMBINATIONS.map(queryMethod => ({
119117
code: `
120118
const [myVariable] = within()
121119
myVariable.${queryMethod}(baz)

0 commit comments

Comments
 (0)