8
8
AutocompleteListbox ,
9
9
AutocompleteOption ,
10
10
} from './autocomplete' ;
11
- import './autocompleteTest.css' ;
12
11
13
12
const fruits = [
14
13
'Apple' ,
@@ -133,15 +132,19 @@ describe('Autocomplete', () => {
133
132
cy . checkA11yForComponent ( ) ;
134
133
} ) ;
135
134
136
- 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'.` , ( ) => {
137
138
cy . mount ( < RegularAutocomplete /> ) ;
138
139
139
140
cy . get ( 'button' ) . click ( ) . should ( 'have.attr' , 'aria-expanded' , 'true' ) ;
140
141
141
142
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
142
143
} ) ;
143
144
144
- 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'.` , ( ) => {
145
148
cy . mount ( < RegularAutocomplete /> ) ;
146
149
147
150
cy . get ( 'button' )
@@ -152,15 +155,69 @@ describe('Autocomplete', () => {
152
155
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
153
156
} ) ;
154
157
155
- 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.` , ( ) => {
156
211
cy . mount ( < RegularAutocomplete /> ) ;
157
212
158
213
cy . get ( 'input' ) . type ( `Ap` ) ;
159
214
160
215
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
161
216
} ) ;
162
217
163
- 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.` , ( ) => {
164
221
cy . mount ( < RegularAutocomplete /> ) ;
165
222
166
223
cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -172,7 +229,9 @@ describe('Autocomplete', () => {
172
229
cy . get ( 'input' ) . should ( 'have.value' , 'Apple' ) ;
173
230
} ) ;
174
231
175
- 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.` , ( ) => {
176
235
cy . mount ( < RegularAutocomplete /> ) ;
177
236
178
237
cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -184,7 +243,9 @@ describe('Autocomplete', () => {
184
243
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
185
244
} ) ;
186
245
187
- 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.` , ( ) => {
188
249
cy . mount ( < RegularAutocomplete /> ) ;
189
250
190
251
cy . get ( 'input' ) . type ( `Ba` ) ;
@@ -196,23 +257,26 @@ describe('Autocomplete', () => {
196
257
cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
197
258
} ) ;
198
259
199
- 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.` , ( ) => {
200
263
cy . mount ( < RegularAutocomplete /> ) ;
201
264
202
- cy . get ( 'input ' ) . type ( `Ba` ) ;
265
+ cy . get ( 'button ' ) . click ( ) ;
203
266
204
267
cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
205
268
206
- cy . get ( 'input' ) . type ( `{downarrow }` ) ;
269
+ cy . findByRole ( 'option' , { name : 'Guava' } ) . focus ( ) . type ( `{enter }` ) ;
207
270
208
- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{enter}` ) ;
209
-
210
- cy . get ( 'input' ) . should ( 'have.value' , 'Banana' ) ;
271
+ cy . get ( 'input' ) . should ( 'have.value' , 'Guava' ) ;
211
272
212
273
cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
213
274
} ) ;
214
275
215
- 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
+ ` , ( ) => {
216
280
cy . mount ( < RegularAutocomplete /> ) ;
217
281
218
282
cy . get ( 'input' ) . type ( `A` ) ;
@@ -221,12 +285,22 @@ describe('Autocomplete', () => {
221
285
222
286
cy . get ( 'input' ) . type ( `{downarrow}` ) ;
223
287
224
- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{downarrow}` ) ;
288
+ cy . findByRole ( 'option' , { name : 'Apple' } ) . focus ( ) . type ( `{downarrow}` ) ;
225
289
226
290
// grabs the 2nd element because the index is 1
227
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' ) ;
228
302
229
- cy . get ( 'li' ) . filter ( ':visible' ) . eq ( 1 ) . type ( `{uparrow}` ) ;
303
+ cy . findByRole ( 'option' , { name : 'Apricot' } ) . focus ( ) . type ( `{uparrow}` ) ;
230
304
231
305
cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
232
306
} ) ;
0 commit comments