@@ -72,7 +72,10 @@ type IsAsyncUtilFn = (
72
72
validNames ?: readonly ( typeof ASYNC_UTILS ) [ number ] [ ]
73
73
) => boolean ;
74
74
type IsFireEventMethodFn = ( node : TSESTree . Identifier ) => boolean ;
75
- type IsUserEventMethodFn = ( node : TSESTree . Identifier ) => boolean ;
75
+ type IsUserEventMethodFn = (
76
+ node : TSESTree . Identifier ,
77
+ userEventSession ?: string
78
+ ) => boolean ;
76
79
type IsRenderUtilFn = ( node : TSESTree . Identifier ) => boolean ;
77
80
type IsCreateEventUtil = (
78
81
node : TSESTree . CallExpression | TSESTree . Identifier
@@ -97,6 +100,9 @@ type FindImportedTestingLibraryUtilSpecifierFn = (
97
100
type IsNodeComingFromTestingLibraryFn = (
98
101
node : TSESTree . Identifier | TSESTree . MemberExpression
99
102
) => boolean ;
103
+ type getUserEventImportIdentifierFn = (
104
+ node : ImportModuleNode | null
105
+ ) => TSESTree . Identifier | null ;
100
106
101
107
export interface DetectionHelpers {
102
108
getTestingLibraryImportNode : GetTestingLibraryImportNodeFn ;
@@ -130,6 +136,7 @@ export interface DetectionHelpers {
130
136
canReportErrors : CanReportErrorsFn ;
131
137
findImportedTestingLibraryUtilSpecifier : FindImportedTestingLibraryUtilSpecifierFn ;
132
138
isNodeComingFromTestingLibrary : IsNodeComingFromTestingLibraryFn ;
139
+ getUserEventImportIdentifier : getUserEventImportIdentifierFn ;
133
140
}
134
141
135
142
const USER_EVENT_PACKAGE = '@testing-library/user-event' ;
@@ -326,6 +333,35 @@ export function detectTestingLibraryUtils<
326
333
return getImportModuleName ( importedCustomModuleNode ) ;
327
334
} ;
328
335
336
+ const getUserEventImportIdentifier = ( node : ImportModuleNode | null ) => {
337
+ if ( ! node ) {
338
+ return null ;
339
+ }
340
+
341
+ if ( isImportDeclaration ( node ) ) {
342
+ const userEventIdentifier = node . specifiers . find ( ( specifier ) =>
343
+ isImportDefaultSpecifier ( specifier )
344
+ ) ;
345
+
346
+ if ( userEventIdentifier ) {
347
+ return userEventIdentifier . local ;
348
+ }
349
+ } else {
350
+ if ( ! ASTUtils . isVariableDeclarator ( node . parent ) ) {
351
+ return null ;
352
+ }
353
+
354
+ const requireNode = node . parent ;
355
+ if ( ! ASTUtils . isIdentifier ( requireNode . id ) ) {
356
+ return null ;
357
+ }
358
+
359
+ return requireNode . id ;
360
+ }
361
+
362
+ return null ;
363
+ } ;
364
+
329
365
/**
330
366
* Determines whether Testing Library utils are imported or not for
331
367
* current file being analyzed.
@@ -557,7 +593,10 @@ export function detectTestingLibraryUtils<
557
593
return regularCall || wildcardCall || wildcardCallWithCallExpression ;
558
594
} ;
559
595
560
- const isUserEventMethod : IsUserEventMethodFn = ( node ) => {
596
+ const isUserEventMethod : IsUserEventMethodFn = (
597
+ node ,
598
+ userEventInstance
599
+ ) => {
561
600
const userEvent = findImportedUserEventSpecifier ( ) ;
562
601
let userEventName : string | undefined ;
563
602
@@ -567,7 +606,7 @@ export function detectTestingLibraryUtils<
567
606
userEventName = USER_EVENT_NAME ;
568
607
}
569
608
570
- if ( ! userEventName ) {
609
+ if ( ! userEventName && ! userEventInstance ) {
571
610
return false ;
572
611
}
573
612
@@ -591,8 +630,11 @@ export function detectTestingLibraryUtils<
591
630
592
631
// check userEvent.click() usage
593
632
return (
594
- ASTUtils . isIdentifier ( parentMemberExpression . object ) &&
595
- parentMemberExpression . object . name === userEventName
633
+ ( ASTUtils . isIdentifier ( parentMemberExpression . object ) &&
634
+ parentMemberExpression . object . name === userEventName ) ||
635
+ // check userEventInstance.click() usage
636
+ ( ASTUtils . isIdentifier ( parentMemberExpression . object ) &&
637
+ parentMemberExpression . object . name === userEventInstance )
596
638
) ;
597
639
} ;
598
640
@@ -853,35 +895,7 @@ export function detectTestingLibraryUtils<
853
895
854
896
const findImportedUserEventSpecifier : ( ) => TSESTree . Identifier | null =
855
897
( ) => {
856
- if ( ! importedUserEventLibraryNode ) {
857
- return null ;
858
- }
859
-
860
- if ( isImportDeclaration ( importedUserEventLibraryNode ) ) {
861
- const userEventIdentifier =
862
- importedUserEventLibraryNode . specifiers . find ( ( specifier ) =>
863
- isImportDefaultSpecifier ( specifier )
864
- ) ;
865
-
866
- if ( userEventIdentifier ) {
867
- return userEventIdentifier . local ;
868
- }
869
- } else {
870
- if (
871
- ! ASTUtils . isVariableDeclarator ( importedUserEventLibraryNode . parent )
872
- ) {
873
- return null ;
874
- }
875
-
876
- const requireNode = importedUserEventLibraryNode . parent ;
877
- if ( ! ASTUtils . isIdentifier ( requireNode . id ) ) {
878
- return null ;
879
- }
880
-
881
- return requireNode . id ;
882
- }
883
-
884
- return null ;
898
+ return getUserEventImportIdentifier ( importedUserEventLibraryNode ) ;
885
899
} ;
886
900
887
901
const getTestingLibraryImportedUtilSpecifier = (
@@ -997,6 +1011,7 @@ export function detectTestingLibraryUtils<
997
1011
canReportErrors,
998
1012
findImportedTestingLibraryUtilSpecifier,
999
1013
isNodeComingFromTestingLibrary,
1014
+ getUserEventImportIdentifier,
1000
1015
} ;
1001
1016
1002
1017
// Instructions for Testing Library detection.
0 commit comments