88 AutocompleteListbox ,
99 AutocompleteOption ,
1010} from './autocomplete' ;
11- import './autocompleteTest.css' ;
1211
1312const fruits = [
1413 'Apple' ,
@@ -133,15 +132,19 @@ describe('Autocomplete', () => {
133132 cy . checkA11yForComponent ( ) ;
134133 } ) ;
135134
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'.` , ( ) => {
137138 cy . mount ( < RegularAutocomplete /> ) ;
138139
139140 cy . get ( 'button' ) . click ( ) . should ( 'have.attr' , 'aria-expanded' , 'true' ) ;
140141
141142 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
142143 } ) ;
143144
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'.` , ( ) => {
145148 cy . mount ( < RegularAutocomplete /> ) ;
146149
147150 cy . get ( 'button' )
@@ -152,15 +155,69 @@ describe('Autocomplete', () => {
152155 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
153156 } ) ;
154157
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.` , ( ) => {
156211 cy . mount ( < RegularAutocomplete /> ) ;
157212
158213 cy . get ( 'input' ) . type ( `Ap` ) ;
159214
160215 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
161216 } ) ;
162217
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.` , ( ) => {
164221 cy . mount ( < RegularAutocomplete /> ) ;
165222
166223 cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -172,7 +229,9 @@ describe('Autocomplete', () => {
172229 cy . get ( 'input' ) . should ( 'have.value' , 'Apple' ) ;
173230 } ) ;
174231
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.` , ( ) => {
176235 cy . mount ( < RegularAutocomplete /> ) ;
177236
178237 cy . get ( 'input' ) . type ( `Ap` ) ;
@@ -184,7 +243,9 @@ describe('Autocomplete', () => {
184243 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
185244 } ) ;
186245
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.` , ( ) => {
188249 cy . mount ( < RegularAutocomplete /> ) ;
189250
190251 cy . get ( 'input' ) . type ( `Ba` ) ;
@@ -196,23 +257,26 @@ describe('Autocomplete', () => {
196257 cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
197258 } ) ;
198259
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.` , ( ) => {
200263 cy . mount ( < RegularAutocomplete /> ) ;
201264
202- cy . get ( 'input ' ) . type ( `Ba` ) ;
265+ cy . get ( 'button ' ) . click ( ) ;
203266
204267 cy . findByRole ( 'listbox' ) . should ( 'be.visible' ) ;
205268
206- cy . get ( 'input' ) . type ( `{downarrow }` ) ;
269+ cy . findByRole ( 'option' , { name : 'Guava' } ) . focus ( ) . type ( `{enter }` ) ;
207270
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' ) ;
211272
212273 cy . findByRole ( 'listbox' ) . should ( 'not.exist' ) ;
213274 } ) ;
214275
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+ ` , ( ) => {
216280 cy . mount ( < RegularAutocomplete /> ) ;
217281
218282 cy . get ( 'input' ) . type ( `A` ) ;
@@ -221,12 +285,22 @@ describe('Autocomplete', () => {
221285
222286 cy . get ( 'input' ) . type ( `{downarrow}` ) ;
223287
224- cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . type ( `{downarrow}` ) ;
288+ cy . findByRole ( 'option' , { name : 'Apple' } ) . focus ( ) . type ( `{downarrow}` ) ;
225289
226290 // grabs the 2nd element because the index is 1
227291 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' ) ;
228302
229- cy . get ( 'li' ) . filter ( ':visible' ) . eq ( 1 ) . type ( `{uparrow}` ) ;
303+ cy . findByRole ( 'option' , { name : 'Apricot' } ) . focus ( ) . type ( `{uparrow}` ) ;
230304
231305 cy . get ( 'li' ) . filter ( ':visible' ) . first ( ) . should ( 'have.focus' ) ;
232306 } ) ;
0 commit comments