@@ -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 }
0 commit comments