@@ -17,12 +17,15 @@ function queryByAltText(container, alt) {
17
17
)
18
18
}
19
19
20
- function queryByPlaceholderText ( container , text ) {
20
+ function queryByAttribute ( attribute , container , text ) {
21
21
return query (
22
- container , '[placeholder]' , elem => elem . getAttribute ( 'placeholder' ) , text
22
+ container , `[ ${ attribute } ]` , elem => elem . getAttribute ( attribute ) , text
23
23
)
24
24
}
25
25
26
+ const queryByPlaceholderText = queryByAttribute . bind ( null , 'placeholder' )
27
+ const queryByTestId = queryByAttribute . bind ( null , 'data-testid' )
28
+
26
29
function queryLabelByText ( container , text ) {
27
30
return query (
28
31
container , 'label' , elem => elem . textContent , text
@@ -39,7 +42,7 @@ function queryByLabelText(container, text, { selector = '*' } = {}) {
39
42
const label = queryLabelByText ( container , text )
40
43
41
44
if ( ! label ) {
42
- return null
45
+ return queryByAttribute ( 'aria-label' , container , text )
43
46
}
44
47
45
48
/* istanbul ignore if */
@@ -65,15 +68,6 @@ function queryByLabelText(container, text, { selector = '*' } = {}) {
65
68
}
66
69
}
67
70
68
- function queryByTestId ( container , id ) {
69
- const found = container . find ( getDataTestIdSelector ( id ) )
70
- return found && found . element ? found . element : null
71
- }
72
-
73
- function getDataTestIdSelector ( id ) {
74
- return `[data-testid="${ id } "]`
75
- }
76
-
77
71
function getText ( node ) {
78
72
if ( ! node ) {
79
73
return null
@@ -96,7 +90,7 @@ function getByTestId(container, id, ...rest) {
96
90
const el = queryByTestId ( container , id , ...rest )
97
91
if ( ! el ) {
98
92
throw new Error (
99
- `Unable to find an element by: ${ getDataTestIdSelector ( id ) } ` ,
93
+ `Unable to find an element by: [data-testid=" ${ id } "] ` ,
100
94
)
101
95
}
102
96
return el
0 commit comments