11import { AfterViewInit , ChangeDetectorRef , Component , Injectable , OnInit , ViewChild , OnDestroy , DebugElement } from '@angular/core' ;
2- import { async , TestBed , tick , fakeAsync } from '@angular/core/testing' ;
2+ import { async , TestBed , tick , fakeAsync , ComponentFixture } from '@angular/core/testing' ;
33import { By } from '@angular/platform-browser' ;
44import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5- import { FormGroup , FormControl , Validators , FormBuilder , ReactiveFormsModule , FormsModule , NgControl } from '@angular/forms' ;
5+ import { FormGroup , FormControl , Validators , FormBuilder , ReactiveFormsModule , FormsModule , NgControl , NgModel } from '@angular/forms' ;
66import { IgxComboComponent , IgxComboModule , IComboSelectionChangeEventArgs , IgxComboState ,
77 IComboSearchInputEventArgs } from './combo.component' ;
88import { IgxComboItemComponent } from './combo-item.component' ;
@@ -60,7 +60,7 @@ const defaultDropdownItemHeight = 40;
6060const defaultDropdownItemMaxHeight = 400 ;
6161
6262describe ( 'igxCombo' , ( ) => {
63- let fixture ;
63+ let fixture : ComponentFixture < any > ;
6464 let combo : IgxComboComponent ;
6565 let input : DebugElement ;
6666
@@ -94,12 +94,10 @@ describe('igxCombo', () => {
9494
9595 // writeValue
9696 expect ( combo . value ) . toBe ( '' ) ;
97- mockSelection . add_items . and . returnValue ( new Set ( [ 'test' ] ) ) ;
97+ mockSelection . get . and . returnValue ( new Set ( [ 'test' ] ) ) ;
9898 spyOnProperty ( combo , 'isRemote' ) . and . returnValue ( false ) ;
9999 combo . writeValue ( [ 'test' ] ) ;
100- // TODO: Uncomment after fix for write value going through entire selection process
101- // expect(mockNgControl.registerOnChangeCb).not.toHaveBeenCalled();
102- expect ( mockSelection . add_items ) . toHaveBeenCalledWith ( combo . id , [ 'test' ] , true ) ;
100+ expect ( mockNgControl . registerOnChangeCb ) . not . toHaveBeenCalled ( ) ;
103101 expect ( mockSelection . select_items ) . toHaveBeenCalledWith ( combo . id , [ 'test' ] , true ) ;
104102 expect ( combo . value ) . toBe ( 'test' ) ;
105103
@@ -120,11 +118,8 @@ describe('igxCombo', () => {
120118 spyOnProperty ( combo , 'collapsed' ) . and . returnValue ( true ) ;
121119 spyOnProperty ( combo , 'valid' , 'set' ) ;
122120
123- combo . onFocus ( ) ;
124- expect ( mockNgControl . registerOnTouchedCb ) . toHaveBeenCalledTimes ( 1 ) ;
125-
126121 combo . onBlur ( ) ;
127- expect ( mockNgControl . registerOnTouchedCb ) . toHaveBeenCalledTimes ( 2 ) ;
122+ expect ( mockNgControl . registerOnTouchedCb ) . toHaveBeenCalledTimes ( 1 ) ;
128123 } ) ;
129124 it ( 'should correctly handle ngControl validity' , ( ) => {
130125 pending ( 'Convert existing form test here' ) ;
@@ -199,17 +194,18 @@ describe('igxCombo', () => {
199194 combo = new IgxComboComponent ( { nativeElement : null } , mockCdr , mockSelection as any , mockComboService , null , mockInjector ) ;
200195 combo . ngOnInit ( ) ;
201196 combo . data = data ;
202- spyOn ( combo , 'selectItems' ) ;
197+ mockSelection . select_items . calls . reset ( ) ;
198+ spyOnProperty ( combo , 'isRemote' ) . and . returnValue ( false ) ;
203199 combo . writeValue ( [ 'EXAMPLE' ] ) ;
204- expect ( combo . selectItems ) . toHaveBeenCalledTimes ( 1 ) ;
200+ expect ( mockSelection . select_items ) . toHaveBeenCalledTimes ( 1 ) ;
205201
206- // Calling "SelectItems " through the writeValue accessor should clear the previous values;
202+ // Calling "select_items " through the writeValue accessor should clear the previous values;
207203 // Select items is called with the invalid value and it is written in selection, though no item is selected
208204 // Controlling the selection is up to the user
209- expect ( combo . selectItems ) . toHaveBeenCalledWith ( [ 'EXAMPLE' ] , true ) ;
205+ expect ( mockSelection . select_items ) . toHaveBeenCalledWith ( combo . id , [ 'EXAMPLE' ] , true ) ;
210206 combo . writeValue ( combo . data [ 0 ] ) ;
211207 // When value key is specified, the item's value key is stored in the selection
212- expect ( combo . selectItems ) . toHaveBeenCalledWith ( combo . data [ 0 ] , true ) ;
208+ expect ( mockSelection . select_items ) . toHaveBeenCalledWith ( combo . id , [ ] , true ) ;
213209 } ) ;
214210 it ( 'should select items through setSelctedItem method' , ( ) => {
215211 const selectionService = new IgxSelectionAPIService ( ) ;
@@ -2578,6 +2574,7 @@ describe('igxCombo', () => {
25782574 expect ( combo . valid ) . toEqual ( IgxComboState . INVALID ) ;
25792575 expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INVALID ) ;
25802576
2577+ input . triggerEventHandler ( 'focus' , { } ) ;
25812578 combo . selectAllItems ( ) ;
25822579 fixture . detectChanges ( ) ;
25832580 expect ( combo . valid ) . toEqual ( IgxComboState . VALID ) ;
@@ -2589,17 +2586,72 @@ describe('igxCombo', () => {
25892586 expect ( combo . valid ) . toEqual ( IgxComboState . INVALID ) ;
25902587 expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INVALID ) ;
25912588 } ) ;
2592- it ( 'should have correctly bound focus and blur handlers' , ( ) => {
2593- spyOn ( combo , 'onFocus' ) ;
2594- spyOn ( combo , 'onBlur' ) ;
2589+ it ( 'should properly init with empty array and handle consecutive model changes' , fakeAsync ( ( ) => {
2590+ const model = fixture . debugElement . query ( By . directive ( NgModel ) ) . injector . get ( NgModel ) ;
2591+ fixture . componentInstance . values = [ ] ;
2592+ fixture . detectChanges ( ) ;
2593+ tick ( ) ;
2594+ expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2595+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2596+ expect ( model . valid ) . toBeFalse ( ) ;
2597+ expect ( model . dirty ) . toBeFalse ( ) ;
2598+ expect ( model . touched ) . toBeFalse ( ) ;
25952599
2596- input . triggerEventHandler ( 'focus' , { } ) ;
2597- expect ( combo . onFocus ) . toHaveBeenCalled ( ) ;
2598- expect ( combo . onFocus ) . toHaveBeenCalledWith ( ) ;
2600+ fixture . componentInstance . values = [ 'Missouri' ] ;
2601+ fixture . detectChanges ( ) ;
2602+ tick ( ) ;
2603+ expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2604+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2605+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Missouri' ] ) ;
2606+ expect ( combo . value ) . toEqual ( 'Missouri' ) ;
2607+ expect ( model . valid ) . toBeTrue ( ) ;
2608+ expect ( model . touched ) . toBeFalse ( ) ;
2609+
2610+ fixture . componentInstance . values = [ 'Missouri' , 'Missouri' ] ;
2611+ fixture . detectChanges ( ) ;
2612+ expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2613+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2614+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Missouri' ] ) ;
2615+ expect ( combo . value ) . toEqual ( 'Missouri' ) ;
2616+ expect ( model . valid ) . toBeTrue ( ) ;
2617+ expect ( model . touched ) . toBeFalse ( ) ;
2618+
2619+ fixture . componentInstance . values = null ;
2620+ fixture . detectChanges ( ) ;
2621+ tick ( ) ;
2622+ expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2623+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2624+ expect ( combo . selectedItems ( ) ) . toEqual ( [ ] ) ;
2625+ expect ( combo . value ) . toEqual ( '' ) ;
2626+ expect ( model . valid ) . toBeFalse ( ) ;
2627+ expect ( model . touched ) . toBeFalse ( ) ;
2628+ expect ( model . dirty ) . toBeFalse ( ) ;
2629+
2630+ combo . onBlur ( ) ;
2631+ fixture . detectChanges ( ) ;
2632+ expect ( combo . valid ) . toEqual ( IgxComboState . INVALID ) ;
2633+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INVALID ) ;
2634+ expect ( model . valid ) . toBeFalse ( ) ;
2635+ expect ( model . touched ) . toBeTrue ( ) ;
2636+ expect ( model . dirty ) . toBeFalse ( ) ;
2637+
2638+ fixture . componentInstance . values = [ 'New Jersey' ] ;
2639+ fixture . detectChanges ( ) ;
2640+ tick ( ) ;
2641+ expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2642+ expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2643+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'New Jersey' ] ) ;
2644+ expect ( combo . value ) . toEqual ( 'New Jersey' ) ;
2645+ expect ( model . valid ) . toBeTrue ( ) ;
2646+ expect ( model . touched ) . toBeTrue ( ) ;
2647+ expect ( model . dirty ) . toBeFalse ( ) ;
2648+ } ) ) ;
2649+ it ( 'should have correctly bound blur handler' , ( ) => {
2650+ spyOn ( combo , 'onBlur' ) ;
25992651
26002652 input . triggerEventHandler ( 'blur' , { } ) ;
26012653 expect ( combo . onBlur ) . toHaveBeenCalled ( ) ;
2602- expect ( combo . onFocus ) . toHaveBeenCalledWith ( ) ;
2654+ expect ( combo . onBlur ) . toHaveBeenCalledWith ( ) ;
26032655 } ) ;
26042656 } ) ;
26052657 } ) ;
0 commit comments