@@ -132,15 +132,19 @@ describe('Autocomplete', () => {
132
132
cy . checkA11yForComponent ( ) ;
133
133
} ) ;
134
134
135
- it ( 'Should open the listbox and aria-expanded is true on the button' , ( ) => {
135
+ it ( `GIVEN a button is present that controls the visibility of a listbox
136
+ WHEN the button is clicked,
137
+ THEN the listbox should open and the attribute 'aria-expanded' on the button should be set to 'true'.` , ( ) => {
136
138
cy . mount ( < RegularAutocomplete /> ) ;
137
139
138
140
cy . get ( 'button' ) . click ( ) . should ( 'have.attr' , 'aria-expanded' , 'true' ) ;
139
141
140
142
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
141
143
} ) ;
142
144
143
- it ( 'Should close the listbox and aria-expanded is false on the button' , ( ) => {
145
+ it ( `GIVEN a button is present that controls the visibility of a listbox, and the listbox is currently open,
146
+ WHEN the button is clicked,
147
+ THEN the listbox should close and the attribute 'aria-expanded' on the button should be set to 'false'.` , ( ) => {
144
148
cy . mount ( < RegularAutocomplete /> ) ;
145
149
146
150
cy . get ( 'button' )
@@ -151,15 +155,69 @@ describe('Autocomplete', () => {
151
155
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
152
156
} ) ;
153
157
154
- it ( 'Should input a value and the listbox should open' , ( ) => {
158
+ it ( `GIVEN an open listbox
159
+ WHEN the user presses the 'Escape' key,
160
+ THEN the listbox should close and the attribute 'aria-expanded' on the button should be set to 'false'.` , ( ) => {
161
+ cy . mount ( < RegularAutocomplete /> ) ;
162
+
163
+ cy . get ( 'button' ) . click ( ) ;
164
+
165
+ cy . findByRole ( 'listbox' ) ;
166
+
167
+ cy . get ( 'body' ) . type ( `{esc}` ) ;
168
+
169
+ cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
170
+ } ) ;
171
+
172
+ it ( `GIVEN a focused option in a listbox,
173
+ WHEN the user presses the 'Home' key,
174
+ THEN the first option in the listbox should be focused.` , ( ) => {
175
+ cy . mount ( < RegularAutocomplete /> ) ;
176
+
177
+ cy . get ( 'button' ) . click ( ) ;
178
+
179
+ cy . findByRole ( 'option' , { name : 'Yuzu' } ) . focus ( ) . type ( `{home}` ) ;
180
+
181
+ cy . get ( 'li' ) . first ( ) . should ( 'have.focus' ) ;
182
+ } ) ;
183
+
184
+ it ( `GIVEN a focused option in a listbox,
185
+ WHEN the user presses the 'End' key,
186
+ THEN the last option in the listbox should be focused.` , ( ) => {
187
+ cy . mount ( < RegularAutocomplete /> ) ;
188
+
189
+ cy . get ( 'button' ) . click ( ) ;
190
+
191
+ cy . findByRole ( 'option' , { name : 'Apricot' } ) . focus ( ) . type ( `{end}` ) ;
192
+
193
+ cy . get ( 'li' ) . last ( ) . should ( 'have.focus' ) ;
194
+ } ) ;
195
+
196
+ it ( `GIVEN selected text in an input field,
197
+ WHEN the user presses the 'Delete' key,
198
+ THEN the text within the input field should be removed.` , ( ) => {
199
+ cy . mount ( < RegularAutocomplete /> ) ;
200
+
201
+ cy . get ( 'input' )
202
+ . type ( `Here's some text!` )
203
+ . type ( `{selectall}` )
204
+ . type ( `{del}` )
205
+ . should ( 'have.value' , '' ) ;
206
+ } ) ;
207
+
208
+ it ( `GIVEN an input field is present that is connected to a listbox,
209
+ WHEN a value is entered into the input field,
210
+ THEN the listbox should open.` , ( ) => {
155
211
cy . mount ( < RegularAutocomplete /> ) ;
156
212
157
213
cy . get ( 'input' ) . type ( `Ap` ) ;
158
214
159
215
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
160
216
} ) ;
161
217
162
- it ( 'Should input a value and select a value, with the input value as the option' , ( ) => {
218
+ it ( `GIVEN an input field is present that is connected to a listbox,
219
+ WHEN a value is entered into the input field, and an option matching the input value is selected from the listbox,
220
+ THEN the selected value should populate the input field.` , ( ) => {
163
221
cy . mount ( < RegularAutocomplete /> ) ;
164
222
165
223
cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -171,7 +229,9 @@ describe('Autocomplete', () => {
171
229
cy . get ( 'input' ) . should ( 'have.value' , 'Apple' ) ;
172
230
} ) ;
173
231
174
- it ( 'Should close the listbox when a value is selected' , ( ) => {
232
+ it ( `GIVEN a listbox is open and populated with selectable options,
233
+ WHEN a value is selected from the listbox,
234
+ THEN the listbox should close.` , ( ) => {
175
235
cy . mount ( < RegularAutocomplete /> ) ;
176
236
177
237
cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -183,7 +243,9 @@ describe('Autocomplete', () => {
183
243
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
184
244
} ) ;
185
245
186
- it ( 'Should focus the first filtered option when the down arrow is prssed' , ( ) => {
246
+ it ( `GIVEN a listbox is open and populated with filtered options,
247
+ WHEN the down arrow key is pressed,
248
+ THEN the first filtered option in the listbox should receive focus.` , ( ) => {
187
249
cy . mount ( < RegularAutocomplete /> ) ;
188
250
189
251
cy . get ( 'input' ) . type ( `Ba` ) ;
@@ -195,23 +257,26 @@ describe('Autocomplete', () => {
195
257
cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
196
258
} ) ;
197
259
198
- it ( 'Should select an option using the enter key, closing the listbox' , ( ) => {
260
+ it ( `GIVEN a listbox is open and an option is in focus,
261
+ WHEN the enter key is pressed,
262
+ THEN the focused option should be selected and the listbox should close.` , ( ) => {
199
263
cy . mount ( < RegularAutocomplete /> ) ;
200
264
201
- cy . get ( 'input ' ) . type ( `Ba` ) ;
265
+ cy . get ( 'button ' ) . click ( ) ;
202
266
203
267
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
204
268
205
- cy . get ( 'input' ) . type ( `{downarrow }` ) ;
269
+ cy . findByRole ( 'option' , { name : 'Guava' } ) . focus ( ) . type ( `{enter }` ) ;
206
270
207
- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{enter}` ) ;
208
-
209
- cy . get ( 'input' ) . should ( 'have.value' , 'Banana' ) ;
271
+ cy . get ( 'input' ) . should ( 'have.value' , 'Guava' ) ;
210
272
211
273
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
212
274
} ) ;
213
275
214
- it ( 'Should go down an option and back up, using the up & down arrow keys' , ( ) => {
276
+ it ( `GIVEN a listbox is open and populated with selectable options,
277
+ WHEN the down arrow key is pressed,
278
+ THEN focus should move to the next option in the list,
279
+ ` , ( ) => {
215
280
cy . mount ( < RegularAutocomplete /> ) ;
216
281
217
282
cy . get ( 'input' ) . type ( `A` ) ;
@@ -220,12 +285,22 @@ describe('Autocomplete', () => {
220
285
221
286
cy . get ( 'input' ) . type ( `{downarrow}` ) ;
222
287
223
- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{downarrow}` ) ;
288
+ cy . findByRole ( 'option' , { name : 'Apple' } ) . focus ( ) . type ( `{downarrow}` ) ;
224
289
225
290
// grabs the 2nd element because the index is 1
226
291
cy . get ( 'li' ) . filter ( ':visible' ) . eq ( 1 ) . should ( 'have.focus' ) ;
292
+ } ) ;
293
+
294
+ it ( `GIVEN a listbox is open and populated with selectable options,
295
+ WHEN the up arrow key is pressed,
296
+ THEN focus should move back to the previous option in the list.` , ( ) => {
297
+ cy . mount ( < RegularAutocomplete /> ) ;
298
+
299
+ cy . get ( 'input' ) . type ( `A` ) ;
300
+
301
+ cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
227
302
228
- cy . get ( 'li' ) . filter ( ':visible' ) . eq ( 1 ) . type ( `{uparrow}` ) ;
303
+ cy . findByRole ( 'option' , { name : 'Apricot' } ) . focus ( ) . type ( `{uparrow}` ) ;
229
304
230
305
cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
231
306
} ) ;
0 commit comments