@@ -242,17 +242,19 @@ describe('Combobox', () => {
242242 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
243243 } ) ;
244244
245- it ( 'should clear the completion string and not close on escape when a completion is present ' , ( ) => {
245+ it ( 'should close then clear the completion string ' , ( ) => {
246246 fixture . componentInstance . filterMode . set ( 'highlight' ) ;
247247 focus ( ) ;
248- input ( 'A ' ) ;
248+ input ( 'Ala ' ) ;
249249 expect ( inputElement . value ) . toBe ( 'Alabama' ) ;
250250 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
251251 escape ( ) ;
252- expect ( inputElement . value ) . toBe ( 'A' ) ;
253- expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
252+ expect ( inputElement . value ) . toBe ( 'Alabama' ) ;
253+ expect ( inputElement . selectionEnd ) . toBe ( 7 ) ;
254+ expect ( inputElement . selectionStart ) . toBe ( 3 ) ;
255+ expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ; // close
254256 escape ( ) ;
255- expect ( inputElement . value ) . toBe ( 'A ' ) ;
257+ expect ( inputElement . value ) . toBe ( '' ) ; // clear input
256258 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
257259 } ) ;
258260
@@ -424,6 +426,28 @@ describe('Combobox', () => {
424426 expect ( inputElement . selectionEnd ) . toBe ( 7 ) ;
425427 } ) ) ;
426428
429+ it ( 'should not insert a completion string on backspace' , fakeAsync ( ( ) => {
430+ focus ( ) ;
431+ input ( 'New' ) ;
432+ tick ( ) ;
433+
434+ expect ( inputElement . value ) . toBe ( 'New Hampshire' ) ;
435+ expect ( inputElement . selectionStart ) . toBe ( 3 ) ;
436+ expect ( inputElement . selectionEnd ) . toBe ( 13 ) ;
437+ } ) ) ;
438+
439+ it ( 'should insert a completion string even if the items are not changed' , fakeAsync ( ( ) => {
440+ focus ( ) ;
441+ input ( 'New' ) ;
442+ tick ( ) ;
443+
444+ input ( 'New ' ) ;
445+ tick ( ) ;
446+ expect ( inputElement . value ) . toBe ( 'New Hampshire' ) ;
447+ expect ( inputElement . selectionStart ) . toBe ( 4 ) ;
448+ expect ( inputElement . selectionEnd ) . toBe ( 13 ) ;
449+ } ) ) ;
450+
427451 it ( 'should commit the selected option on focusout' , ( ) => {
428452 focus ( ) ;
429453 input ( 'Cali' ) ;
@@ -438,15 +462,15 @@ describe('Combobox', () => {
438462 // TODO(wagnermaciel): Add unit tests for disabled options.
439463
440464 describe ( 'Filtering' , ( ) => {
441- beforeEach ( ( ) => setupCombobox ( ) ) ;
442-
443465 it ( 'should lazily render options' , ( ) => {
466+ setupCombobox ( ) ;
444467 expect ( getOptions ( ) . length ) . toBe ( 0 ) ;
445468 focus ( ) ;
446469 expect ( getOptions ( ) . length ) . toBe ( 50 ) ;
447470 } ) ;
448471
449472 it ( 'should filter the options based on the input value' , ( ) => {
473+ setupCombobox ( ) ;
450474 focus ( ) ;
451475 input ( 'New' ) ;
452476
@@ -459,20 +483,47 @@ describe('Combobox', () => {
459483 } ) ;
460484
461485 it ( 'should show no options if nothing matches' , ( ) => {
486+ setupCombobox ( ) ;
462487 focus ( ) ;
463488 input ( 'xyz' ) ;
464489 const options = getOptions ( ) ;
465490 expect ( options . length ) . toBe ( 0 ) ;
466491 } ) ;
467492
468493 it ( 'should show all options when the input is cleared' , ( ) => {
494+ setupCombobox ( ) ;
469495 focus ( ) ;
470496 input ( 'Alabama' ) ;
471497 expect ( getOptions ( ) . length ) . toBe ( 1 ) ;
472498
473499 input ( '' ) ;
474500 expect ( getOptions ( ) . length ) . toBe ( 50 ) ;
475501 } ) ;
502+
503+ it ( 'should determine the highlighted state on open' , ( ) => {
504+ setupCombobox ( { filterMode : 'highlight' } ) ;
505+ focus ( ) ;
506+ input ( 'N' ) ;
507+ expect ( inputElement . value ) . toBe ( 'Nebraska' ) ;
508+ expect ( inputElement . selectionEnd ) . toBe ( 8 ) ;
509+ expect ( inputElement . selectionStart ) . toBe ( 1 ) ;
510+ expect ( getOptions ( ) . length ) . toBe ( 8 ) ;
511+
512+ escape ( ) ; // close
513+ inputElement . selectionStart = 2 ; // Change highlighting
514+ down ( ) ; // open
515+
516+ expect ( inputElement . value ) . toBe ( 'Nebraska' ) ;
517+ expect ( inputElement . selectionEnd ) . toBe ( 8 ) ;
518+ expect ( inputElement . selectionStart ) . toBe ( 2 ) ;
519+ expect ( getOptions ( ) . length ) . toBe ( 6 ) ;
520+
521+ escape ( ) ; // close
522+ inputElement . selectionStart = 3 ; // Change highlighting
523+ down ( ) ; // open
524+
525+ expect ( getOptions ( ) . length ) . toBe ( 1 ) ;
526+ } ) ;
476527 } ) ;
477528
478529 // describe('with programmatic value changes', () => {
@@ -907,17 +958,17 @@ describe('Combobox', () => {
907958 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
908959 } ) ;
909960
910- it ( 'should clear the completion string and not close on escape when a completion is present ' , ( ) => {
961+ it ( 'should close then clear the completion string ' , ( ) => {
911962 fixture . componentInstance . filterMode . set ( 'highlight' ) ;
912963 focus ( ) ;
913964 input ( 'Mar' ) ;
914965 expect ( inputElement . value ) . toBe ( 'March' ) ;
915966 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true' ) ;
916967 escape ( ) ;
917- expect ( inputElement . value ) . toBe ( 'Mar ' ) ;
918- expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'true ' ) ;
968+ expect ( inputElement . value ) . toBe ( 'March ' ) ;
969+ expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false ' ) ; // close
919970 escape ( ) ;
920- expect ( inputElement . value ) . toBe ( 'Mar ' ) ;
971+ expect ( inputElement . value ) . toBe ( '' ) ; // clear input
921972 expect ( inputElement . getAttribute ( 'aria-expanded' ) ) . toBe ( 'false' ) ;
922973 } ) ;
923974
0 commit comments