Skip to content

Commit e6d3de6

Browse files
committed
fix(prefer-screen-queries): detect queries coming from proper render
1 parent c344c33 commit e6d3de6

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed

lib/rules/prefer-screen-queries.ts

+34-20
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,26 @@ export default createTestingLibraryRule<Options, MessageIds>({
5959
});
6060
}
6161

62-
const queriesDestructuredInWithinDeclaration: string[] = [];
62+
function saveSafeDestructuredQueries(node: TSESTree.VariableDeclarator) {
63+
if (isObjectPattern(node.id)) {
64+
const identifiers = node.id.properties
65+
.filter(
66+
(property) =>
67+
isProperty(property) &&
68+
ASTUtils.isIdentifier(property.key) &&
69+
helpers.isQuery(property.key)
70+
)
71+
.map(
72+
(property: TSESTree.Property) =>
73+
(property.key as TSESTree.Identifier).name
74+
);
75+
safeDestructuredQueries.push(...identifiers);
76+
}
77+
}
78+
79+
// keep here those queries which are safe and shouldn't be reported
80+
// (from within, from render + container/base element, not related to TL, etc)
81+
const safeDestructuredQueries: string[] = [];
6382
// use an array as within might be used more than once in a test
6483
const withinDeclaredVariables: string[] = [];
6584

@@ -71,30 +90,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
7190
) {
7291
return;
7392
}
93+
94+
const isComingFromValidRender = helpers.isRenderUtil(node.init.callee);
95+
96+
if (!isComingFromValidRender) {
97+
// save the destructured query methods as safe since they are coming
98+
// from render not related to TL
99+
saveSafeDestructuredQueries(node);
100+
}
101+
74102
const isWithinFunction = node.init.callee.name === 'within';
75103
const usesRenderOptions =
76-
helpers.isRenderUtil(node.init.callee) &&
77-
usesContainerOrBaseElement(node.init);
104+
isComingFromValidRender && usesContainerOrBaseElement(node.init);
78105

79106
if (!isWithinFunction && !usesRenderOptions) {
80107
return;
81108
}
82109

83110
if (isObjectPattern(node.id)) {
84-
// save the destructured query methods
85-
const identifiers = node.id.properties
86-
.filter(
87-
(property) =>
88-
isProperty(property) &&
89-
ASTUtils.isIdentifier(property.key) &&
90-
helpers.isQuery(property.key)
91-
)
92-
.map(
93-
(property: TSESTree.Property) =>
94-
(property.key as TSESTree.Identifier).name
95-
);
96-
97-
queriesDestructuredInWithinDeclaration.push(...identifiers);
111+
// save the destructured query methods as safe since they are coming
112+
// from within or render + base/container options
113+
saveSafeDestructuredQueries(node);
98114
return;
99115
}
100116

@@ -108,9 +124,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
108124
}
109125

110126
if (
111-
!queriesDestructuredInWithinDeclaration.some(
112-
(queryName) => queryName === node.name
113-
)
127+
!safeDestructuredQueries.some((queryName) => queryName === node.name)
114128
) {
115129
reportInvalidUsage(node);
116130
}

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

+17-9
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,23 @@ ruleTester.run(RULE_NAME, rule, {
151151
`,
152152
})
153153
),
154-
// ...ALL_BUILTIN_AND_CUSTOM_QUERIES_COMBINATIONS.map((queryMethod) => ({
155-
// settings: {
156-
// 'testing-library/custom-renders': ['customRender'],
157-
// },
158-
// code: `
159-
// import { anotherRender } from 'whatever'
160-
// const { ${queryMethod} } = anotherRender(foo)
161-
// ${queryMethod}()`,
162-
// })),
154+
...ALL_BUILTIN_AND_CUSTOM_QUERIES_COMBINATIONS.map((queryMethod) => ({
155+
settings: { 'testing-library/utils-module': 'test-utils' },
156+
code: `
157+
import { render as testUtilRender } from 'test-utils'
158+
import { render } from 'somewhere-else'
159+
const { ${queryMethod} } = render(foo)
160+
${queryMethod}()`,
161+
})),
162+
...ALL_BUILTIN_AND_CUSTOM_QUERIES_COMBINATIONS.map((queryMethod) => ({
163+
settings: {
164+
'testing-library/custom-renders': ['customRender'],
165+
},
166+
code: `
167+
import { anotherRender } from 'whatever'
168+
const { ${queryMethod} } = anotherRender(foo)
169+
${queryMethod}()`,
170+
})),
163171
],
164172

165173
invalid: [

0 commit comments

Comments
 (0)