@@ -53,9 +53,9 @@ describe('SearchWithin', function () {
53
53
} ) ;
54
54
55
55
it ( 'renders correctly' , function ( ) {
56
- let { getAllByText, getByRole, getByLabelText } = renderSearchWithin ( ) ;
56
+ let { getAllByText, getByRole} = renderSearchWithin ( ) ;
57
57
58
- let searchfield = getByLabelText ( 'Test' , { selector : 'input' } ) ;
58
+ let searchfield = getByRole ( 'searchbox' ) ;
59
59
expect ( searchfield ) . toBeVisible ( ) ;
60
60
expect ( searchfield ) . toHaveAttribute ( 'type' , 'search' ) ;
61
61
@@ -69,8 +69,8 @@ describe('SearchWithin', function () {
69
69
70
70
it ( 'can type in search and get onChange' , function ( ) {
71
71
let onChange = jest . fn ( ) ;
72
- let { getByLabelText } = renderSearchWithin ( { } , { onChange} ) ;
73
- let searchfield = getByLabelText ( 'Test' , { selector : 'input' } ) ;
72
+ let { getByRole } = renderSearchWithin ( { } , { onChange} ) ;
73
+ let searchfield = getByRole ( 'searchbox' ) ;
74
74
expect ( searchfield ) . toHaveAttribute ( 'value' , '' ) ;
75
75
76
76
act ( ( ) => { searchfield . focus ( ) ; } ) ;
@@ -94,34 +94,34 @@ describe('SearchWithin', function () {
94
94
} ) ;
95
95
96
96
it ( 'searchfield and picker are labelled correctly' , function ( ) {
97
- let { getByRole, getAllByText, getByLabelText } = renderSearchWithin ( ) ;
97
+ let { getByRole, getAllByText} = renderSearchWithin ( ) ;
98
98
99
- let searchfield = getByLabelText ( 'Test' , { selector : 'input' } ) ;
99
+ let searchfield = getByRole ( 'searchbox' ) ;
100
100
let picker = getByRole ( 'button' ) ;
101
101
let group = getByRole ( 'group' ) ;
102
102
triggerPress ( picker ) ;
103
103
104
104
let listbox = getByRole ( 'listbox' ) ;
105
105
let label = getAllByText ( 'Test' ) [ 0 ] ;
106
- expect ( listbox ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
107
- expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
106
+ expect ( listbox ) . toHaveAttribute ( 'aria-labelledby' , ` ${ label . id } ${ picker . id } ` ) ;
107
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , ` ${ label . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
108
108
expect ( group ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
109
109
} ) ;
110
110
111
111
it ( 'isDisabled=true disables both the searchfield and picker' , function ( ) {
112
- let { getByRole, getByLabelText } = renderSearchWithin ( { isDisabled : true } ) ;
112
+ let { getByRole} = renderSearchWithin ( { isDisabled : true } ) ;
113
113
114
- let searchfield = getByLabelText ( 'Test' , { selector : 'input' } ) ;
114
+ let searchfield = getByRole ( 'searchbox' ) ;
115
115
let picker = getByRole ( 'button' ) ;
116
116
117
117
expect ( searchfield ) . toHaveAttribute ( 'disabled' ) ;
118
118
expect ( picker ) . toHaveAttribute ( 'disabled' ) ;
119
119
} ) ;
120
120
121
121
it ( 'autoFocus=true on searchfield will automatically focus the input' , function ( ) {
122
- let { getByLabelText } = renderSearchWithin ( { } , { autoFocus : true } ) ;
122
+ let { getByRole } = renderSearchWithin ( { } , { autoFocus : true } ) ;
123
123
124
- let searchfield = getByLabelText ( 'Test' , { selector : 'input' } ) ;
124
+ let searchfield = getByRole ( 'searchbox' ) ;
125
125
126
126
expect ( searchfield ) . toHaveFocus ( ) ;
127
127
} ) ;
@@ -135,13 +135,13 @@ describe('SearchWithin', function () {
135
135
} ) ;
136
136
137
137
it ( 'slot props override props provided to children' , function ( ) {
138
- let { getByRole, getAllByText, getByLabelText } = renderSearchWithin (
138
+ let { getByRole, getAllByText} = renderSearchWithin (
139
139
{ isDisabled : true , isRequired : false , label : 'Test1' } ,
140
140
{ isDisabled : false , isRequired : true , label : 'Test2' , isQuiet : true } ,
141
141
{ isDisabled : false , isRequired : true , label : 'Test3' , isQuiet : true }
142
142
) ;
143
143
144
- let searchfield = getByLabelText ( 'Test1' , { selector : 'input' } ) ;
144
+ let searchfield = getByRole ( 'searchbox' ) ;
145
145
let picker = getByRole ( 'button' ) ;
146
146
let group = getByRole ( 'group' ) ;
147
147
triggerPress ( picker ) ;
@@ -152,20 +152,87 @@ describe('SearchWithin', function () {
152
152
153
153
expect ( searchfield ) . not . toHaveAttribute ( 'aria-required' ) ;
154
154
155
- expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
155
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , ` ${ label . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
156
156
expect ( group ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
157
157
158
158
expect ( searchfield . classList . contains ( 'is-quiet' ) ) . toBeFalsy ( ) ;
159
159
expect ( picker . classList . contains ( 'spectrum-Dropdown--quiet' ) ) . toBeFalsy ( ) ;
160
160
} ) ;
161
+ } ) ;
162
+
163
+ describe ( 'SearchWithin labeling' , function ( ) {
164
+ it ( 'no label - default' , function ( ) {
165
+ let { getByRole} = renderSearchWithin ( { label : undefined } ) ;
166
+
167
+ let group = getByRole ( 'group' ) ;
168
+ let searchfield = getByRole ( 'searchbox' ) ;
169
+ let picker = getByRole ( 'button' ) ;
170
+
171
+ expect ( group ) . toHaveAttribute ( 'aria-label' , 'Search' ) ;
172
+
173
+ expect ( group ) . not . toHaveAttribute ( 'aria-labelledby' ) ;
174
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , `${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
175
+ expect ( picker ) . toHaveAttribute ( 'aria-labelledby' , `${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
176
+ } ) ;
161
177
162
- it ( 'searchfield and group are labelled correctly if aria- label used ' , function ( ) {
163
- let { getByRole, getByLabelText } = renderSearchWithin ( { label : undefined , 'aria-label' : 'Aria Label '} ) ;
178
+ it ( 'label = foo ' , function ( ) {
179
+ let { getByRole, getByText } = renderSearchWithin ( { label : 'foo '} ) ;
164
180
165
- let searchfield = getByLabelText ( 'Aria Label' , { selector : 'input' } ) ;
166
181
let group = getByRole ( 'group' ) ;
182
+ let searchfield = getByRole ( 'searchbox' ) ;
183
+ let picker = getByRole ( 'button' ) ;
184
+ let label = getByText ( 'foo' ) ;
185
+
186
+ expect ( group ) . not . toHaveAttribute ( 'aria-label' ) ;
187
+
188
+ expect ( group ) . toHaveAttribute ( 'aria-labelledby' , label . id ) ;
189
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , `${ label . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
190
+ expect ( picker ) . toHaveAttribute ( 'aria-labelledby' , `${ label . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
191
+
192
+ expect ( label ) . toHaveAttribute ( 'for' , searchfield . id ) ;
193
+ } ) ;
194
+
195
+ it ( 'aria-label = bar' , function ( ) {
196
+ let { getByRole} = renderSearchWithin ( { 'aria-label' : 'bar' , label : undefined } ) ;
197
+
198
+ let group = getByRole ( 'group' ) ;
199
+ let searchfield = getByRole ( 'searchbox' ) ;
200
+ let picker = getByRole ( 'button' ) ;
201
+
202
+ expect ( group ) . toHaveAttribute ( 'aria-label' , 'bar' ) ;
203
+
204
+ expect ( group ) . not . toHaveAttribute ( 'aria-labelledby' ) ;
205
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , `${ group . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
206
+ expect ( picker ) . toHaveAttribute ( 'aria-labelledby' , `${ group . id } ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
207
+ } ) ;
208
+
209
+ it ( 'aria-labelledby = {id}' , function ( ) {
210
+ let { getByRole} = render (
211
+ < Provider theme = { theme } >
212
+ < label id = "id-foo-label" htmlFor = "id-searchfield" >
213
+ Foo
214
+ </ label >
215
+ < SearchWithin aria-labelledby = "id-foo-label" >
216
+ < SearchField id = "id-searchfield" placeholder = "Search" />
217
+ < Picker defaultSelectedKey = "all" >
218
+ < Item key = "all" > All</ Item >
219
+ < Item key = "campaigns" > Campaigns</ Item >
220
+ < Item key = "audiences" > Audiences</ Item >
221
+ < Item key = "tags" > Tags</ Item >
222
+ </ Picker >
223
+ </ SearchWithin >
224
+ </ Provider >
225
+ ) ;
226
+
227
+ let group = getByRole ( 'group' ) ;
228
+ let searchfield = getByRole ( 'searchbox' ) ;
229
+ let picker = getByRole ( 'button' ) ;
230
+
231
+ expect ( group ) . not . toHaveAttribute ( 'aria-label' ) ;
167
232
168
- expect ( searchfield ) . toHaveAttribute ( 'aria-label' , 'Aria Label' ) ;
169
- expect ( group ) . toHaveAttribute ( 'aria-labelledby' , 'Aria Label' ) ;
233
+ expect ( group ) . toHaveAttribute ( 'aria-labelledby' , 'id-foo-label' ) ;
234
+ expect ( searchfield ) . toHaveAttribute ( 'aria-labelledby' , `id-foo-label ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
235
+ expect ( searchfield ) . toHaveAttribute ( 'id' , 'id-searchfield' ) ;
236
+ expect ( picker ) . toHaveAttribute ( 'aria-labelledby' , `id-foo-label ${ picker . id } ${ picker . childNodes [ 0 ] . id } ` ) ;
170
237
} ) ;
171
238
} ) ;
0 commit comments