@@ -35,7 +35,11 @@ function getRegExpMatcher(string) {
35
35
}
36
36
37
37
function makeSuggestion ( queryName , content , { variant = 'get' , name} ) {
38
- const queryArgs = [ queryName === 'Role' ? content : getRegExpMatcher ( content ) ]
38
+ const queryArgs = [
39
+ queryName === 'Role' || queryName === 'TestId'
40
+ ? content
41
+ : getRegExpMatcher ( content ) ,
42
+ ]
39
43
40
44
if ( name ) {
41
45
queryArgs . push ( { name : new RegExp ( escapeRegExp ( name . toLowerCase ( ) ) , 'i' ) } )
@@ -64,45 +68,58 @@ function makeSuggestion(queryName, content, {variant = 'get', name}) {
64
68
}
65
69
}
66
70
67
- export function getSuggestedQuery ( element , variant ) {
71
+ function canSuggest ( currentMethod , requestedMethod , data ) {
72
+ return data && ( ! requestedMethod || requestedMethod === currentMethod )
73
+ }
74
+
75
+ export function getSuggestedQuery ( element , variant , method ) {
76
+ // don't create suggestions for script and style elements
77
+ if ( element . matches ( DEFAULT_IGNORE_TAGS ) ) {
78
+ return undefined
79
+ }
80
+
68
81
const role =
69
82
element . getAttribute ( 'role' ) ?? getImplicitAriaRoles ( element ) ?. [ 0 ]
70
- if ( role ) {
83
+ if ( canSuggest ( 'Role' , method , role ) ) {
71
84
return makeSuggestion ( 'Role' , role , {
72
85
variant,
73
86
name : computeAccessibleName ( element ) ,
74
87
} )
75
88
}
76
89
77
90
const labelText = getLabelTextFor ( element )
78
- if ( labelText ) {
91
+ if ( canSuggest ( 'LabelText' , method , labelText ) ) {
79
92
return makeSuggestion ( 'LabelText' , labelText , { variant} )
80
93
}
81
94
82
95
const placeholderText = element . getAttribute ( 'placeholder' )
83
- if ( placeholderText ) {
96
+ if ( canSuggest ( 'PlaceholderText' , method , placeholderText ) ) {
84
97
return makeSuggestion ( 'PlaceholderText' , placeholderText , { variant} )
85
98
}
86
99
87
100
const textContent = normalize ( getNodeText ( element ) )
88
- if ( textContent && ! element . matches ( DEFAULT_IGNORE_TAGS ) ) {
101
+ if ( canSuggest ( 'Text' , method , textContent ) ) {
89
102
return makeSuggestion ( 'Text' , textContent , { variant} )
90
103
}
91
104
92
- if ( element . value ) {
105
+ if ( canSuggest ( 'DisplayValue' , method , element . value ) ) {
93
106
return makeSuggestion ( 'DisplayValue' , normalize ( element . value ) , { variant} )
94
107
}
95
108
96
109
const alt = element . getAttribute ( 'alt' )
97
- if ( alt ) {
110
+ if ( canSuggest ( 'AltText' , method , alt ) ) {
98
111
return makeSuggestion ( 'AltText' , alt , { variant} )
99
112
}
100
113
101
114
const title = element . getAttribute ( 'title' )
102
-
103
- if ( title ) {
115
+ if ( canSuggest ( 'Title' , method , title ) ) {
104
116
return makeSuggestion ( 'Title' , title , { variant} )
105
117
}
106
118
119
+ const testId = element . getAttribute ( 'data-testid' )
120
+ if ( canSuggest ( 'TestId' , method , testId ) ) {
121
+ return makeSuggestion ( 'TestId' , testId , { variant} )
122
+ }
123
+
107
124
return undefined
108
125
}
0 commit comments