@@ -132,15 +132,19 @@ describe('Autocomplete', () => {
132132 cy . checkA11yForComponent ( ) ;
133133 } ) ;
134134
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'.` , ( ) => {
136138 cy . mount ( < RegularAutocomplete /> ) ;
137139
138140 cy . get ( 'button' ) . click ( ) . should ( 'have.attr' , 'aria-expanded' , 'true' ) ;
139141
140142 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
141143 } ) ;
142144
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'.` , ( ) => {
144148 cy . mount ( < RegularAutocomplete /> ) ;
145149
146150 cy . get ( 'button' )
@@ -151,15 +155,69 @@ describe('Autocomplete', () => {
151155 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
152156 } ) ;
153157
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.` , ( ) => {
155211 cy . mount ( < RegularAutocomplete /> ) ;
156212
157213 cy . get ( 'input' ) . type ( `Ap` ) ;
158214
159215 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
160216 } ) ;
161217
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.` , ( ) => {
163221 cy . mount ( < RegularAutocomplete /> ) ;
164222
165223 cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -171,7 +229,9 @@ describe('Autocomplete', () => {
171229 cy . get ( 'input' ) . should ( 'have.value' , 'Apple' ) ;
172230 } ) ;
173231
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.` , ( ) => {
175235 cy . mount ( < RegularAutocomplete /> ) ;
176236
177237 cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -183,7 +243,9 @@ describe('Autocomplete', () => {
183243 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
184244 } ) ;
185245
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.` , ( ) => {
187249 cy . mount ( < RegularAutocomplete /> ) ;
188250
189251 cy . get ( 'input' ) . type ( `Ba` ) ;
@@ -195,23 +257,26 @@ describe('Autocomplete', () => {
195257 cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
196258 } ) ;
197259
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.` , ( ) => {
199263 cy . mount ( < RegularAutocomplete /> ) ;
200264
201- cy . get ( 'input ' ) . type ( `Ba` ) ;
265+ cy . get ( 'button ' ) . click ( ) ;
202266
203267 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
204268
205- cy . get ( 'input' ) . type ( `{downarrow }` ) ;
269+ cy . findByRole ( 'option' , { name : 'Guava' } ) . focus ( ) . type ( `{enter }` ) ;
206270
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' ) ;
210272
211273 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
212274 } ) ;
213275
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+ ` , ( ) => {
215280 cy . mount ( < RegularAutocomplete /> ) ;
216281
217282 cy . get ( 'input' ) . type ( `A` ) ;
@@ -220,12 +285,22 @@ describe('Autocomplete', () => {
220285
221286 cy . get ( 'input' ) . type ( `{downarrow}` ) ;
222287
223- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{downarrow}` ) ;
288+ cy . findByRole ( 'option' , { name : 'Apple' } ) . focus ( ) . type ( `{downarrow}` ) ;
224289
225290 // grabs the 2nd element because the index is 1
226291 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' ) ;
227302
228- cy . get ( 'li' ) . filter ( ':visible' ) . eq ( 1 ) . type ( `{uparrow}` ) ;
303+ cy . findByRole ( 'option' , { name : 'Apricot' } ) . focus ( ) . type ( `{uparrow}` ) ;
229304
230305 cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
231306 } ) ;
0 commit comments