@@ -164,6 +164,13 @@ describe('keyboard', () => {
164
164
} ) ;
165
165
166
166
describe ( 'selecting items' , ( ) => {
167
+ const verifyEnterKeyPropagation = ( allowPropagation ) => {
168
+ const enterEvent = keyboardEventFor ( 'keydown' , 13 , [ ] , 'Enter' ) ;
169
+ const stopPropagationSpy = sinon . spy ( enterEvent , 'stopPropagation' ) ;
170
+ input . dispatchEvent ( enterEvent ) ;
171
+ expect ( stopPropagationSpy . called ) . to . equal ( ! allowPropagation ) ;
172
+ } ;
173
+
167
174
describe ( 'auto-open' , ( ) => {
168
175
beforeEach ( async ( ) => {
169
176
comboBox . value = 'bar' ;
@@ -199,18 +206,41 @@ describe('keyboard', () => {
199
206
} ) ;
200
207
201
208
it ( 'should stop propagation of the keyboard enter event when dropdown is opened' , ( ) => {
202
- const keydownSpy = sinon . spy ( ) ;
203
- document . addEventListener ( 'keydown' , keydownSpy ) ;
204
- enterKeyDown ( input ) ;
205
- expect ( keydownSpy . called ) . to . be . false ;
209
+ verifyEnterKeyPropagation ( false ) ;
206
210
} ) ;
207
211
208
212
it ( 'should stop propagation of the keyboard enter event when input value is invalid' , ( ) => {
209
213
setInputValue ( comboBox , 'foobar' ) ;
210
- const keydownSpy = sinon . spy ( ) ;
211
- document . addEventListener ( 'keydown' , keydownSpy ) ;
214
+
215
+ verifyEnterKeyPropagation ( false ) ;
216
+ } ) ;
217
+
218
+ it ( 'should propagate keyboard enter event after entering an unknown option when custom values are allowed' , ( ) => {
219
+ comboBox . allowCustomValue = true ;
220
+ setInputValue ( comboBox , 'foobar' ) ;
221
+ enterKeyDown ( input ) ;
222
+
223
+ verifyEnterKeyPropagation ( true ) ;
224
+ } ) ;
225
+
226
+ it ( 'should propagate keyboard enter event if filtered items are cleared after selecting a predefined option' , ( ) => {
227
+ setInputValue ( comboBox , 'foo' ) ;
228
+ enterKeyDown ( input ) ;
229
+ // Simulate user or data provider mixin resetting filtered items after closing overlay
230
+ comboBox . filteredItems = [ ] ;
231
+ expect ( comboBox . _focusedIndex ) . to . equal ( - 1 ) ;
232
+
233
+ verifyEnterKeyPropagation ( true ) ;
234
+ } ) ;
235
+
236
+ it ( 'should propagate keyboard enter event after clearing the value' , ( ) => {
237
+ setInputValue ( comboBox , 'foo' ) ;
238
+ enterKeyDown ( input ) ;
239
+
240
+ setInputValue ( comboBox , '' ) ;
212
241
enterKeyDown ( input ) ;
213
- expect ( keydownSpy . called ) . to . be . false ;
242
+
243
+ verifyEnterKeyPropagation ( true ) ;
214
244
} ) ;
215
245
216
246
it ( 'should not close the overlay with enter when custom values are not allowed' , ( ) => {
@@ -399,37 +429,39 @@ describe('keyboard', () => {
399
429
400
430
it ( 'should stop propagation of the keyboard enter event when input value is invalid' , ( ) => {
401
431
setInputValue ( comboBox , 'foobar' ) ;
402
- const keydownSpy = sinon . spy ( ) ;
403
- document . addEventListener ( 'keydown' , keydownSpy ) ;
404
- enterKeyDown ( input ) ;
405
- expect ( keydownSpy . called ) . to . be . false ;
432
+
433
+ verifyEnterKeyPropagation ( false ) ;
406
434
} ) ;
407
435
408
- it ( 'should not stop propagation of the keyboard enter event when input has a predefined option' , ( ) => {
436
+ it ( 'should propagate the keyboard enter event when input has a predefined option' , ( ) => {
409
437
setInputValue ( comboBox , 'foo' ) ;
410
438
expect ( comboBox . opened ) . to . be . false ;
411
- const keydownSpy = sinon . spy ( ) ;
412
- document . addEventListener ( 'keydown' , keydownSpy ) ;
439
+
440
+ verifyEnterKeyPropagation ( true ) ;
441
+ } ) ;
442
+
443
+ it ( 'should propagate keyboard enter event if filtered items are cleared after selecting a predefined option' , ( ) => {
444
+ setInputValue ( comboBox , 'foo' ) ;
413
445
enterKeyDown ( input ) ;
414
- expect ( keydownSpy . called ) . to . be . true ;
446
+ // Simulate user or data provider mixin resetting filtered items after closing overlay
447
+ comboBox . filteredItems = [ ] ;
448
+ expect ( comboBox . _focusedIndex ) . to . equal ( - 1 ) ;
449
+
450
+ verifyEnterKeyPropagation ( true ) ;
415
451
} ) ;
416
452
417
- it ( 'should not stop propagation of the keyboard enter event when input has a custom value' , ( ) => {
453
+ it ( 'should propagate the keyboard enter event when input has a custom value' , ( ) => {
418
454
comboBox . allowCustomValue = true ;
419
455
setInputValue ( comboBox , 'foobar' ) ;
420
- const keydownSpy = sinon . spy ( ) ;
421
- document . addEventListener ( 'keydown' , keydownSpy ) ;
422
- enterKeyDown ( input ) ;
423
- expect ( keydownSpy . called ) . to . be . true ;
456
+
457
+ verifyEnterKeyPropagation ( true ) ;
424
458
} ) ;
425
459
426
- it ( 'should not stop propagation of the keyboard enter event when input is empty' , ( ) => {
460
+ it ( 'should propagate the keyboard enter event when input is empty' , ( ) => {
427
461
comboBox . allowCustomValue = true ;
428
462
setInputValue ( comboBox , '' ) ;
429
- const keydownSpy = sinon . spy ( ) ;
430
- document . addEventListener ( 'keydown' , keydownSpy ) ;
431
- enterKeyDown ( input ) ;
432
- expect ( keydownSpy . called ) . to . be . true ;
463
+
464
+ verifyEnterKeyPropagation ( true ) ;
433
465
} ) ;
434
466
} ) ;
435
467
} ) ;
0 commit comments