@@ -56,18 +56,9 @@ getByLabelText(
56
56
This will search for the label that matches the given [ ` TextMatch ` ] ( #textmatch ) ,
57
57
then find the element associated with that label.
58
58
59
- <!-- DOCUSAURUS_CODE_TABS-->
60
-
61
- <!-- Native-->
62
-
63
- ``` javascript
64
- import {getByLabelText } from ' dom-testing-library'
65
-
66
- const container = document .body
67
- const inputNode = getByLabelText (container, ' Username' )
68
-
69
- // This will find the input node for the following DOM structures:
59
+ The example below will find the input node for the following DOM structures:
70
60
61
+ ``` js
71
62
// for/htmlFor relationship between label and form element id
72
63
< label for = " username-input" > Username< / label>
73
64
< input id= " username-input" / >
@@ -89,16 +80,17 @@ const inputNode = getByLabelText(container, 'Username')
89
80
// Take care because this is not a label that users can see on the page,
90
81
// so the purpose of your input must be obvious to visual users.
91
82
< input aria- label= " username" / >
83
+ ```
92
84
93
- // It will NOT find the input node for label text broken up by elements,
94
- // like this:
95
- < label >
96
- < span > Username < / span > < input / >
97
- < / label >
98
- // For this case, you can provide a `selector` in the options:
99
- const inputNode = getByLabelText (container, ' username ' , {
100
- selector : ' input ' ,
101
- } )
85
+ <!-- DOCUSAURUS_CODE_TABS -->
86
+
87
+ <!-- Native -- >
88
+
89
+ ``` javascript
90
+ import { getByLabelText } from ' dom-testing-library '
91
+
92
+ const container = document . body
93
+ const inputNode = getByLabelText (container, ' Username ' )
102
94
```
103
95
104
96
<!-- React-->
@@ -119,11 +111,19 @@ cy.getByLabelText('username').should('exist')
119
111
120
112
<!-- END_DOCUSAURUS_CODE_TABS-->
121
113
122
- > ** Note**
123
- >
124
- > This method will throw an error if it cannot find the node. If you don't want
125
- > this behavior (for example you wish to assert that it doesn't exist), then use
126
- > ` queryByLabelText ` instead.
114
+ It will NOT find the input node for label text broken up by elements. For this
115
+ case, you can provide a ` selector ` in the options:
116
+
117
+ ``` html
118
+ <label > <span >Username</span > <input /> </label >
119
+ ```
120
+
121
+ ``` js
122
+ const container = document .body
123
+ const inputNode = getByLabelText (container, ' username' , {
124
+ selector: ' input' ,
125
+ })
126
+ ```
127
127
128
128
### ` ByPlaceholderText `
129
129
0 commit comments