@@ -59,7 +59,26 @@ export default createTestingLibraryRule<Options, MessageIds>({
59
59
} ) ;
60
60
}
61
61
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 [ ] = [ ] ;
63
82
// use an array as within might be used more than once in a test
64
83
const withinDeclaredVariables : string [ ] = [ ] ;
65
84
@@ -71,30 +90,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
71
90
) {
72
91
return ;
73
92
}
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
+
74
102
const isWithinFunction = node . init . callee . name === 'within' ;
75
103
const usesRenderOptions =
76
- helpers . isRenderUtil ( node . init . callee ) &&
77
- usesContainerOrBaseElement ( node . init ) ;
104
+ isComingFromValidRender && usesContainerOrBaseElement ( node . init ) ;
78
105
79
106
if ( ! isWithinFunction && ! usesRenderOptions ) {
80
107
return ;
81
108
}
82
109
83
110
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 ) ;
98
114
return ;
99
115
}
100
116
@@ -108,9 +124,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
108
124
}
109
125
110
126
if (
111
- ! queriesDestructuredInWithinDeclaration . some (
112
- ( queryName ) => queryName === node . name
113
- )
127
+ ! safeDestructuredQueries . some ( ( queryName ) => queryName === node . name )
114
128
) {
115
129
reportInvalidUsage ( node ) ;
116
130
}
0 commit comments