@@ -92,7 +92,7 @@ describe('PoComboComponent:', () => {
9292 component . isProcessingValueByTab = false ;
9393 spyOn ( component , 'applyFilter' ) ;
9494 component . controlApplyFilter ( 'valor' ) ;
95- expect ( component . applyFilter ) . toHaveBeenCalledWith ( 'valor' , true ) ;
95+ expect ( component . applyFilter ) . toHaveBeenCalledWith ( 'valor' , true , undefined ) ;
9696 } ) ;
9797
9898 it ( 'shouldn`t call apply filter when is processing "getObjectByValue"' , ( ) => {
@@ -208,9 +208,9 @@ describe('PoComboComponent:', () => {
208208 spyOn ( component , 'controlComboVisibility' ) ;
209209 component . comboOpen = true ;
210210 component . disabled = false ;
211-
212- component . toggleComboVisibility ( ) ;
213- expect ( component . controlComboVisibility ) . toHaveBeenCalledWith ( false ) ;
211+ fixture . detectChanges ( ) ;
212+ component . toggleComboVisibility ( false ) ;
213+ expect ( component . controlComboVisibility ) . toHaveBeenCalledWith ( false , false , false ) ;
214214 } ) ;
215215
216216 it ( 'should call applyFilterInFirstClick' , ( ) => {
@@ -571,12 +571,8 @@ describe('PoComboComponent:', () => {
571571 focus : ( ) => { }
572572 }
573573 } ;
574- component . poListbox = {
575- setFocus : ( ) => { }
576- } as any ;
577574
578575 spyOn ( component . contentElement . nativeElement , 'focus' ) ;
579- spyOn ( component . poListbox , 'setFocus' ) ;
580576 component . visibleOptions = [ { value : 'item 1' , label : 'item 1' } ] ;
581577 component . comboOpen = true ;
582578 component . changeOnEnter = true ;
@@ -586,15 +582,13 @@ describe('PoComboComponent:', () => {
586582
587583 expect ( component . controlComboVisibility ) . toHaveBeenCalledWith ( true ) ;
588584 expect ( component . isFiltering ) . toBe ( false ) ;
589- expect ( component . poListbox . setFocus ) . toHaveBeenCalled ( ) ;
590585 } ) ;
591586
592587 it ( 'shouldn`t call `selectNextOption` and call `controlComboVisibility` if `comboOpen` with false' , ( ) => {
593588 const event = { ...fakeEvent , keyCode : 40 } ;
594589
595590 component . comboOpen = false ;
596591 component . visibleOptions = [ { value : '1' , label : '1' } ] ;
597-
598592 spyOn ( component , 'controlComboVisibility' ) ;
599593
600594 component . onKeyDown ( event ) ;
@@ -683,6 +677,36 @@ describe('PoComboComponent:', () => {
683677 expect ( component . updateComboList ) . toHaveBeenCalled ( ) ;
684678 } ) ;
685679
680+ it ( 'should call `clear` if esc is double clicked' , ( ) => {
681+ const event = { ...fakeEvent , keyCode : 27 } ;
682+
683+ component . service = undefined ;
684+ component . selectedValue = 'Test1' ;
685+ component . comboOpen = true ;
686+ component [ 'lastKey' ] = 27 ;
687+
688+ spyOn ( component , 'clearAndFocus' ) ;
689+
690+ component . onKeyDown ( event ) ;
691+
692+ expect ( component . clearAndFocus ) . toHaveBeenCalled ( ) ;
693+ } ) ;
694+
695+ it ( 'should call `onCloseCombo` if esc is clicked' , ( ) => {
696+ const event = { ...fakeEvent , keyCode : 27 } ;
697+
698+ component . service = undefined ;
699+ component . selectedValue = 'Test1' ;
700+ component . comboOpen = true ;
701+ component [ 'lastKey' ] = 20 ;
702+
703+ spyOn ( component , 'onCloseCombo' ) ;
704+
705+ component . onKeyDown ( event ) ;
706+
707+ expect ( component . onCloseCombo ) . toHaveBeenCalled ( ) ;
708+ } ) ;
709+
686710 it ( 'shouldn`t call `updateComboList` if itens service is not undefined' , ( ) => {
687711 const event = { ...fakeEvent , keyCode : 13 } ;
688712
@@ -724,6 +748,16 @@ describe('PoComboComponent:', () => {
724748 expect ( component . inputEl . nativeElement . focus ) . toHaveBeenCalled ( ) ;
725749 } ) ;
726750
751+ it ( 'should call inputEl.nativeElement and clear' , ( ) => {
752+ spyOn ( component . inputEl . nativeElement , 'focus' ) ;
753+ spyOn ( component , 'clear' ) ;
754+
755+ component . clearAndFocus ( ) ;
756+
757+ expect ( component . inputEl . nativeElement . focus ) . toHaveBeenCalled ( ) ;
758+ expect ( component . clear ) . toHaveBeenCalled ( ) ;
759+ } ) ;
760+
727761 it ( 'onOptionClick: should call `stopPropagation` if has an event parameter' , ( ) => {
728762 const option : PoComboOption = { value : 'value' , label : 'label' } ;
729763
@@ -949,6 +983,22 @@ describe('PoComboComponent:', () => {
949983 expect ( component . comboOpen ) . toBe ( true ) ;
950984 } ) ;
951985
986+ it ( 'should open the input element' , ( ) => {
987+ const focusSpy = spyOn ( component . inputEl . nativeElement , 'focus' ) ;
988+
989+ component [ 'open' ] ( true ) ;
990+
991+ expect ( focusSpy ) . toHaveBeenCalled ( ) ;
992+ } ) ;
993+
994+ it ( 'should add class to input element when isButton is true' , ( ) => {
995+ const addClassSpy = spyOn ( component . renderer , 'addClass' ) ;
996+
997+ component [ 'open' ] ( false , true ) ;
998+
999+ expect ( addClassSpy ) . toHaveBeenCalledWith ( component . inputEl . nativeElement , 'po-combo-input-focus' ) ;
1000+ } ) ;
1001+
9521002 it ( 'open: should set page and options when has inifity scroll' , ( ) => {
9531003 component . infiniteScroll = true ;
9541004 spyOn ( component , 'getInputValue' ) . and . returnValue ( undefined ) ;
@@ -1156,18 +1206,19 @@ describe('PoComboComponent:', () => {
11561206 it ( 'should show po-clean if `clean` is true and `disabled` is false' , ( ) => {
11571207 component . clean = true ;
11581208 component . disabled = false ;
1209+ component . inputEl . nativeElement . value = 'Test' ;
11591210
11601211 fixture . detectChanges ( ) ;
11611212
1162- expect ( nativeElement . querySelector ( 'po -clean' ) ) . toBeTruthy ( ) ;
1213+ expect ( nativeElement . querySelector ( '.po-combo -clean' ) ) . toBeTruthy ( ) ;
11631214 } ) ;
11641215
11651216 it ( 'shouldn`t show po-clean if `clean` is true and `disabled` is true' , ( ) => {
11661217 component . clean = true ;
11671218 component . disabled = true ;
11681219
11691220 fixture . detectChanges ( ) ;
1170- expect ( nativeElement . querySelector ( 'po -clean' ) ) . toBe ( null ) ;
1221+ expect ( nativeElement . querySelector ( '.po-combo -clean' ) ) . toBe ( null ) ;
11711222 } ) ;
11721223
11731224 it ( 'shouldn`t show po-clean if `clean` is false' , ( ) => {
@@ -1316,11 +1367,14 @@ describe('PoComboComponent - with service:', () => {
13161367 it ( 'applyFilter: should call PoComboFilterService.getFilteredData() with param and filterParams' , ( ) => {
13171368 const filterParams = 'filter' ;
13181369 const applyFilterValue = 'value' ;
1370+ const isArrowDown = true ;
13191371 const param = { property : 'label' , value : applyFilterValue } ;
13201372 const fakeThis : any = {
13211373 controlComboVisibility : ( ) => { } ,
13221374 setOptionsByApplyFilter : ( ) => { } ,
1375+ focusItem : ( ) => { } ,
13231376 fieldLabel : 'label' ,
1377+ isArrowDown : true ,
13241378 filterParams : filterParams ,
13251379 service : {
13261380 getFilteredData : ( ) => { }
@@ -1332,11 +1386,23 @@ describe('PoComboComponent - with service:', () => {
13321386
13331387 spyOn ( fakeThis . service , 'getFilteredData' ) . and . returnValue ( { subscribe : callback => callback ( ) } ) ;
13341388
1335- component . applyFilter . apply ( fakeThis , [ applyFilterValue ] ) ;
1389+ component . applyFilter . apply ( fakeThis , [ applyFilterValue , isArrowDown ] ) ;
13361390
13371391 expect ( fakeThis . service . getFilteredData ) . toHaveBeenCalledWith ( param , filterParams ) ;
13381392 } ) ;
13391393
1394+ it ( 'applyFilter: should call focusItem if param `isArrowDown` is true' , ( ) => {
1395+ spyOn ( component . service , 'getFilteredData' ) . and . returnValue ( of ( [ { value : 'test' } ] ) ) ;
1396+ spyOn ( component , 'setOptionsByApplyFilter' ) ;
1397+ spyOn ( component , < any > 'focusItem' ) ;
1398+
1399+ component . defaultService . hasNext = true ;
1400+ component . applyFilter ( 'test' , false , true ) ;
1401+
1402+ expect ( component . setOptionsByApplyFilter ) . toHaveBeenCalled ( ) ;
1403+ expect ( component [ 'focusItem' ] ) . toHaveBeenCalled ( ) ;
1404+ } ) ;
1405+
13401406 it ( 'applyFilter: shouldn´t call PoComboFilterService.getFilteredData() if hasNext is false' , ( ) => {
13411407 const filterParams = 'filter' ;
13421408 const applyFilterValue = 'value' ;
@@ -1539,49 +1605,60 @@ describe('PoComboComponent - with service:', () => {
15391605 expect ( component [ 'initInputObservable' ] ) . not . toHaveBeenCalled ( ) ;
15401606 } ) ;
15411607
1542- it ( `searchOnEnter : should call 'controlApplyFilter' if has a service,
1608+ it ( `searchOnEnterOrArrow : should call 'controlApplyFilter' if has a service,
15431609 not has selectedView and value.length is greater than 'filterMinlength'` , ( ) => {
15441610 const value = 'newValue' ;
1611+ const event = {
1612+ key : 'Enter'
1613+ } ;
15451614 component . selectedView = undefined ;
15461615 component . filterMinlength = 2 ;
15471616
15481617 spyOn ( component , 'controlApplyFilter' ) ;
15491618
1550- component . searchOnEnter ( value ) ;
1619+ component . searchOnEnterOrArrow ( event , value ) ;
15511620
1552- expect ( component . controlApplyFilter ) . toHaveBeenCalledWith ( value ) ;
1621+ expect ( component . controlApplyFilter ) . toHaveBeenCalledWith ( value , false ) ;
15531622 } ) ;
15541623
1555- it ( `searchOnEnter : shouldn't call 'controlApplyFilter' if has a service and has selectedView` , ( ) => {
1624+ it ( `searchOnEnterOrArrow : shouldn't call 'controlApplyFilter' if has a service and has selectedView` , ( ) => {
15561625 const value = 'value' ;
15571626 component . selectedView = { label : 'Option 1' , value : '1' } ;
1558-
1627+ const event = {
1628+ key : 'Enter'
1629+ } ;
15591630 spyOn ( component , 'controlApplyFilter' ) ;
15601631
1561- component . searchOnEnter ( value ) ;
1632+ component . searchOnEnterOrArrow ( event , value ) ;
15621633
15631634 expect ( component . controlApplyFilter ) . not . toHaveBeenCalled ( ) ;
15641635 } ) ;
15651636
1566- it ( `searchOnEnter : shouldn't call 'controlApplyFilter' if doesn't have a service` , ( ) => {
1637+ it ( `searchOnEnterOrArrow : shouldn't call 'controlApplyFilter' if doesn't have a service` , ( ) => {
15671638 const value = 'value' ;
15681639 component . service = undefined ;
1640+ const event = {
1641+ key : 'Enter'
1642+ } ;
15691643
15701644 spyOn ( component , 'controlApplyFilter' ) ;
15711645
1572- component . searchOnEnter ( value ) ;
1646+ component . searchOnEnterOrArrow ( event , value ) ;
15731647
15741648 expect ( component . controlApplyFilter ) . not . toHaveBeenCalled ( ) ;
15751649 } ) ;
15761650
1577- it ( `searchOnEnter : shouldn't call 'controlApplyFilter' if value.length is less than 'filterMinlength'` , ( ) => {
1651+ it ( `searchOnEnterOrArrow : shouldn't call 'controlApplyFilter' if value.length is less than 'filterMinlength'` , ( ) => {
15781652 const value = 'value' ;
15791653 component . selectedView = { label : 'Option 1' , value : '1' } ;
15801654 component . filterMinlength = 8 ;
1655+ const event = {
1656+ key : 'Enter'
1657+ } ;
15811658
15821659 spyOn ( component , 'controlApplyFilter' ) ;
15831660
1584- component . searchOnEnter ( value ) ;
1661+ component . searchOnEnterOrArrow ( event , value ) ;
15851662
15861663 expect ( component . controlApplyFilter ) . not . toHaveBeenCalled ( ) ;
15871664 } ) ;
@@ -1719,6 +1796,19 @@ describe('PoComboComponent - with service:', () => {
17191796
17201797 expect ( component . visibleOptions ) . toEqual ( mockOptions ) ;
17211798 } ) ;
1799+
1800+ it ( 'should focus on the listbox item when selectedValue is true' , fakeAsync ( ( ) => {
1801+ const listboxItem = document . createElement ( 'div' ) ;
1802+ listboxItem . setAttribute ( 'aria-selected' , 'true' ) ;
1803+ spyOn ( document , 'querySelector' ) . and . returnValue ( listboxItem ) ;
1804+
1805+ const focusSpy = spyOn ( listboxItem , 'focus' ) ;
1806+
1807+ component . selectedValue = true ;
1808+ component [ 'focusItem' ] ( ) ;
1809+ tick ( 100 ) ;
1810+ expect ( focusSpy ) . toHaveBeenCalled ( ) ;
1811+ } ) ) ;
17221812 } ) ;
17231813} ) ;
17241814
0 commit comments