11import { ASTUtils } from '@typescript-eslint/utils' ;
2+ import { isIdentifier } from '@typescript-eslint/utils/ast-utils' ;
23
34import { createTestingLibraryRule } from '../create-testing-library-rule' ;
45import {
@@ -409,7 +410,8 @@ export default createTestingLibraryRule<Options, MessageIds>({
409410 return ;
410411 }
411412
412- if ( ! isCallExpression ( argument . body ) ) {
413+ const argumentBody = argument . body ;
414+ if ( ! isCallExpression ( argumentBody ) ) {
413415 return ;
414416 }
415417
@@ -438,29 +440,42 @@ export default createTestingLibraryRule<Options, MessageIds>({
438440 }
439441
440442 const queryVariant = getFindByQueryVariant ( fullQueryMethod ) ;
441- const callArguments = getQueryArguments ( argument . body ) ;
443+ const callArguments = getQueryArguments ( argumentBody ) ;
442444 const queryMethod = fullQueryMethod . split ( 'By' ) [ 1 ] ;
443445
444446 if ( ! queryMethod ) {
445447 return ;
446448 }
447-
448449 reportInvalidUsage ( node , {
449450 queryMethod,
450451 queryVariant,
451452 prevQuery : fullQueryMethod ,
452453 fix ( fixer ) {
453- const property = (
454- ( argument . body as TSESTree . CallExpression )
455- . callee as TSESTree . MemberExpression
456- ) . property ;
457- if ( helpers . isCustomQuery ( property as TSESTree . Identifier ) ) {
458- return null ;
459- }
460- const newCode = `${ caller } .${ queryVariant } ${ queryMethod } (${ callArguments
454+ const findByCallText = `${ caller } .${ queryVariant } ${ queryMethod } (${ callArguments
461455 . map ( ( callArgNode ) => sourceCode . getText ( callArgNode ) )
462456 . join ( ', ' ) } ${ waitOptionsSourceCode } )`;
463- return fixer . replaceText ( node , newCode ) ;
457+
458+ if ( ! isMemberExpression ( argumentBody . callee ) ) return null ;
459+
460+ const { property, object } = argumentBody . callee ;
461+ if ( ASTUtils . isVariableDeclarator ( node . parent . parent ) ) {
462+ if ( isIdentifier ( property ) && helpers . isCustomQuery ( property ) ) {
463+ return null ;
464+ }
465+ return fixer . replaceText ( node , findByCallText ) ;
466+ }
467+
468+ if ( ! isCallExpression ( object ) ) return null ;
469+
470+ const originalExpect = sourceCode . getText ( argumentBody ) ;
471+ const awaited = `await ${ findByCallText } ` ;
472+ const newExpect = originalExpect . replace (
473+ sourceCode . getText ( object . arguments [ 0 ] ) ,
474+ awaited
475+ ) ;
476+ const output = originalExpect . replace ( originalExpect , newExpect ) ;
477+
478+ return fixer . replaceText ( node . parent , output ) ;
464479 } ,
465480 } ) ;
466481 return ;
@@ -481,7 +496,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
481496
482497 const queryMethod = fullQueryMethod . split ( 'By' ) [ 1 ] ;
483498 const queryVariant = getFindByQueryVariant ( fullQueryMethod ) ;
484- const callArguments = getQueryArguments ( argument . body ) ;
499+ const callArguments = getQueryArguments ( argumentBody ) ;
485500
486501 reportInvalidUsage ( node , {
487502 queryMethod,
@@ -490,10 +505,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
490505 fix ( fixer ) {
491506 // we know from above callee is an Identifier
492507 if (
493- helpers . isCustomQuery (
494- ( argument . body as TSESTree . CallExpression )
495- . callee as TSESTree . Identifier
496- )
508+ helpers . isCustomQuery ( argumentBody . callee as TSESTree . Identifier )
497509 ) {
498510 return null ;
499511 }
0 commit comments