@@ -2,15 +2,18 @@ import { AfterViewInit, ChangeDetectorRef, Component, Injectable, OnInit, ViewCh
2
2
import { TestBed , tick , fakeAsync , ComponentFixture , waitForAsync } from '@angular/core/testing' ;
3
3
import { By } from '@angular/platform-browser' ;
4
4
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
5
- import { FormGroup , FormControl , Validators , FormBuilder , ReactiveFormsModule ,
6
- FormsModule , NgControl , NgModel , NgForm } from '@angular/forms' ;
5
+ import {
6
+ FormGroup , FormControl , Validators , FormBuilder , ReactiveFormsModule ,
7
+ FormsModule , NgControl , NgModel , NgForm
8
+ } from '@angular/forms' ;
7
9
import {
8
10
IgxComboComponent ,
9
11
IgxComboModule ,
10
12
IComboSelectionChangeEventArgs ,
11
13
IgxComboState ,
12
14
IComboSearchInputEventArgs ,
13
- IComboItemAdditionEvent
15
+ IComboItemAdditionEvent ,
16
+ ComboSelectionMode
14
17
} from './combo.component' ;
15
18
import { IgxComboItemComponent } from './combo-item.component' ;
16
19
import { IgxComboDropDownComponent } from './combo-dropdown.component' ;
@@ -305,7 +308,7 @@ describe('igxCombo', () => {
305
308
} ;
306
309
combo . comboInput = {
307
310
nativeElement : {
308
- focus : ( ) => { }
311
+ focus : ( ) => { }
309
312
}
310
313
} as any ;
311
314
combo . handleOpening ( inputEvent ) ;
@@ -2090,6 +2093,50 @@ describe('igxCombo', () => {
2090
2093
expect ( combo . selectedItems ( ) . length ) . toEqual ( 0 ) ;
2091
2094
expect ( combo . onSelectionChange . emit ) . toHaveBeenCalledTimes ( 0 ) ;
2092
2095
} ) ;
2096
+ it ( 'should allow changing of selection mode runtime, leaving only the last element selected' , ( ) => {
2097
+ spyOn ( combo . onSelectionChange , 'emit' ) . and . callThrough ( ) ;
2098
+
2099
+ combo . selectItems ( [ 'Michigan' , 'Ohio' , 'Wisconsin' ] ) ;
2100
+ expect ( combo . onSelectionChange . emit ) . toHaveBeenCalledTimes ( 1 ) ;
2101
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 3 ) ;
2102
+ combo . selectionMode = ComboSelectionMode . single ;
2103
+ fixture . detectChanges ( ) ;
2104
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 1 ) ;
2105
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Wisconsin' ] ) ;
2106
+ expect ( combo . onSelectionChange . emit ) . toHaveBeenCalledTimes ( 2 ) ;
2107
+
2108
+ // does not mutate the input array
2109
+ const selectCall = [ 'Ohio' , 'Wisconsin' , 'Michigan' ] ;
2110
+ combo . selectItems ( selectCall ) ;
2111
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 1 ) ;
2112
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Michigan' ] ) ;
2113
+ expect ( selectCall ) . toEqual ( [ 'Ohio' , 'Wisconsin' , 'Michigan' ] ) ;
2114
+ } ) ;
2115
+ it ( 'should support single selection mode, allowing only one item to be selected' , ( ) => {
2116
+ spyOn ( combo . onSelectionChange , 'emit' ) . and . callThrough ( ) ;
2117
+ combo . selectionMode = ComboSelectionMode . single ;
2118
+
2119
+ combo . selectItems ( [ 'Michigan' ] ) ;
2120
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 1 ) ;
2121
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Michigan' ] ) ;
2122
+ expect ( combo . onSelectionChange . emit ) . toHaveBeenCalledTimes ( 1 ) ;
2123
+
2124
+ combo . selectItems ( [ 'Wisconsin' ] ) ;
2125
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 1 ) ;
2126
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Wisconsin' ] ) ;
2127
+ expect ( combo . onSelectionChange . emit ) . toHaveBeenCalledTimes ( 2 ) ;
2128
+ } ) ;
2129
+ it ( 'should select only the last item when calling selectAllItems and selection mode === single' , ( ) => {
2130
+ combo . selectAllItems ( ) ;
2131
+
2132
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 51 ) ;
2133
+ const lastItem = combo . selectedItems ( ) [ combo . selectedItems ( ) . length - 1 ] ;
2134
+ expect ( lastItem ) . toBe ( 'Washington' ) ;
2135
+
2136
+ combo . selectionMode = ComboSelectionMode . single ;
2137
+ expect ( combo . selectedItems ( ) . length ) . toBe ( 1 ) ;
2138
+ expect ( combo . selectedItems ( ) [ 0 ] ) . toBe ( 'Washington' ) ;
2139
+ } ) ;
2093
2140
} ) ;
2094
2141
describe ( 'Grouping tests: ' , ( ) => {
2095
2142
configureTestSuite ( ) ;
@@ -2406,8 +2453,8 @@ describe('igxCombo', () => {
2406
2453
const searchInput = fixture . debugElement . query ( By . css ( CSS_CLASS_SEARCHINPUT ) ) ;
2407
2454
2408
2455
const verifyFilteredItems = ( inputValue : string ,
2409
- expectedDropdownItemsNumber : number ,
2410
- expectedFilteredItemsNumber : number ) => {
2456
+ expectedDropdownItemsNumber : number ,
2457
+ expectedFilteredItemsNumber : number ) => {
2411
2458
UIInteractions . triggerInputEvent ( searchInput , inputValue ) ;
2412
2459
fixture . detectChanges ( ) ;
2413
2460
dropdownList = fixture . debugElement . query ( By . css ( `.${ CSS_CLASS_CONTAINER } ` ) ) . nativeElement ;
@@ -2912,6 +2959,29 @@ describe('igxCombo', () => {
2912
2959
expect ( combo . valid ) . toEqual ( IgxComboState . INITIAL ) ;
2913
2960
expect ( combo . comboInput . valid ) . toEqual ( IgxInputState . INITIAL ) ;
2914
2961
} ) ) ;
2962
+
2963
+ it ( 'should allow binding to an array of multiple items, leaving only 1 item selected, when SINGLE mode' , fakeAsync ( ( ) => {
2964
+ expect ( combo . selectionMode ) . toBe ( ComboSelectionMode . multiple ) ;
2965
+ combo . selectItems ( [ 'Connecticut' , 'Washington' ] ) ;
2966
+ tick ( ) ;
2967
+ fixture . detectChanges ( ) ;
2968
+ expect ( fixture . componentInstance . values ) . toEqual ( [ 'Connecticut' , 'Washington' ] ) ;
2969
+
2970
+ combo . selectionMode = ComboSelectionMode . single ;
2971
+ tick ( ) ;
2972
+ fixture . detectChanges ( ) ;
2973
+ expect ( fixture . componentInstance . values ) . toEqual ( [ 'Washington' ] ) ;
2974
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'Washington' ] ) ;
2975
+
2976
+ fixture . componentInstance . values = [ 'Connecticut' , 'New Jersey' ] ;
2977
+ tick ( ) ;
2978
+ fixture . detectChanges ( ) ;
2979
+ tick ( ) ;
2980
+ fixture . detectChanges ( ) ;
2981
+
2982
+ expect ( combo . selectedItems ( ) ) . toEqual ( [ 'New Jersey' ] ) ;
2983
+ expect ( fixture . componentInstance . values ) . toEqual ( [ 'New Jersey' ] ) ;
2984
+ } ) ) ;
2915
2985
} ) ;
2916
2986
} ) ;
2917
2987
describe ( 'Display density' , ( ) => {
0 commit comments