@@ -15,7 +15,7 @@ function queryLabelByText(container, text) {
15
15
function queryByLabelText ( container , text , { selector = '*' } = { } ) {
16
16
const label = queryLabelByText ( container , text )
17
17
if ( ! label ) {
18
- return null
18
+ return queryByAttribute ( 'aria-label' , container , text )
19
19
}
20
20
/* istanbul ignore if */
21
21
if ( label . control ) {
@@ -46,22 +46,21 @@ function queryByText(container, text, {selector = '*'} = {}) {
46
46
)
47
47
}
48
48
49
- function queryByPlaceholderText ( container , text ) {
49
+ // this is just a utility and not an exposed query.
50
+ // There are no plans to expose this.
51
+ function queryByAttribute ( attribute , container , text ) {
50
52
return (
51
- Array . from ( container . querySelectorAll ( '[placeholder]' ) ) . find ( node =>
52
- matches ( node . getAttribute ( 'placeholder' ) , node , text ) ,
53
+ Array . from ( container . querySelectorAll ( `[ ${ attribute } ]` ) ) . find ( node =>
54
+ matches ( node . getAttribute ( attribute ) , node , text ) ,
53
55
) || null
54
56
)
55
57
}
56
58
57
- function queryByTestId ( container , id ) {
58
- return container . querySelector ( getDataTestIdSelector ( id ) )
59
- }
60
-
61
- function getDataTestIdSelector ( id ) {
62
- return `[data-testid="${ id } "]`
63
- }
59
+ const queryByPlaceholderText = queryByAttribute . bind ( null , 'placeholder' )
60
+ const queryByTestId = queryByAttribute . bind ( null , 'data-testid' )
64
61
62
+ // this is just a utility and not an exposed query.
63
+ // There are no plans to expose this.
65
64
function getText ( node ) {
66
65
return Array . from ( node . childNodes )
67
66
. filter (
@@ -79,9 +78,7 @@ function getText(node) {
79
78
function getByTestId ( container , id , ...rest ) {
80
79
const el = queryByTestId ( container , id , ...rest )
81
80
if ( ! el ) {
82
- throw new Error (
83
- `Unable to find an element by: ${ getDataTestIdSelector ( id ) } ` ,
84
- )
81
+ throw new Error ( `Unable to find an element by: [data-testid="${ id } "]` )
85
82
}
86
83
return el
87
84
}
0 commit comments