Skip to content

Commit 59e2a8f

Browse files
ViktorSlavovLipata
authored andcommitted
refactor(combo): change combo remote selection clear, add tests #5729
1 parent e36abe9 commit 59e2a8f

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

projects/igniteui-angular/src/lib/combo/combo.component.spec.ts

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -960,8 +960,8 @@ describe('igxCombo', () => {
960960
// Combo selection is now based on valueKey:
961961
// if valueKey is specified, only the key will be written in the selection (not the whole data entry)
962962
const itemID = combo.valueKey !== null && combo.value !== undefined ?
963-
combo.data[itemIndex][combo.valueKey] :
964-
combo.data[itemIndex];
963+
combo.data[itemIndex][combo.valueKey] :
964+
combo.data[itemIndex];
965965
expect(combo.isItemSelected(itemID)).toBeTruthy();
966966
expect(combo.selectedItems()[selectedItemIndex]).toEqual(itemID);
967967
}
@@ -971,8 +971,8 @@ describe('igxCombo', () => {
971971
itemIndex: number) {
972972
const checkbox = getCheckbox(dropdownElement, itemIndex);
973973
const itemID = combo.valueKey !== null && combo.value !== undefined ?
974-
combo.data[itemIndex][combo.valueKey] :
975-
combo.data[itemIndex];
974+
combo.data[itemIndex][combo.valueKey] :
975+
combo.data[itemIndex];
976976
expect(checkbox.classList.contains(CSS_CLASS_CHECKED)).toBeFalsy();
977977
expect(combo.isItemSelected(itemID)).toBeFalsy();
978978
}
@@ -2286,6 +2286,37 @@ describe('igxCombo', () => {
22862286
}, 20);
22872287
}, 10);
22882288
});
2289+
2290+
it('Should bind combo data to remote data and clear selection properly', async (done) => {
2291+
const fixture = TestBed.createComponent(IgxComboRemoteDataComponent);
2292+
fixture.detectChanges();
2293+
const combo = fixture.componentInstance.instance;
2294+
let selectedItems = [combo.data[0], combo.data[1]];
2295+
const spyObj = jasmine.createSpyObj('event', ['stopPropagation']);
2296+
combo.toggle();
2297+
combo.selectItems([selectedItems[0][combo.valueKey], selectedItems[1][combo.valueKey]]);
2298+
expect(combo.value).toEqual(`${selectedItems[0][combo.displayKey]}, ${selectedItems[1][combo.displayKey]}`);
2299+
expect(combo.selectedItems()).toEqual([selectedItems[0][combo.valueKey], selectedItems[1][combo.valueKey]]);
2300+
// Clear items while they are in view
2301+
combo.handleClearItems(spyObj);
2302+
expect(combo.selectedItems()).toEqual([]);
2303+
expect(combo.value).toBe('');
2304+
selectedItems = [combo.data[2], combo.data[3]];
2305+
combo.selectItems([selectedItems[0][combo.valueKey], selectedItems[1][combo.valueKey]]);
2306+
expect(combo.value).toEqual(`${selectedItems[0][combo.displayKey]}, ${selectedItems[1][combo.displayKey]}`);
2307+
2308+
// Scroll selected items out of view
2309+
combo.virtualScrollContainer.scrollTo(40);
2310+
await wait(60);
2311+
fixture.detectChanges();
2312+
combo.handleClearItems(spyObj);
2313+
expect(combo.selectedItems()).toEqual([]);
2314+
expect(combo.value).toBe('');
2315+
combo.selectItems([combo.data[7][combo.valueKey]]);
2316+
expect(combo.value).toBe(combo.data[7][combo.displayKey]);
2317+
done();
2318+
});
2319+
22892320
it('Should render empty template when combo data source is not set', fakeAsync(() => {
22902321
const fixture = TestBed.createComponent(IgxComboEmptyTestComponent);
22912322
fixture.detectChanges();
@@ -3214,7 +3245,7 @@ describe('igxCombo', () => {
32143245
expect(fixture.componentInstance.comboSelectedItems).toEqual([...data].splice(1, 3));
32153246
}));
32163247

3217-
it('Should properly initialize when used in a Template form control', fakeAsync (() => {
3248+
it('Should properly initialize when used in a Template form control', fakeAsync(() => {
32183249
const fix = TestBed.createComponent(IgxComboInTemplatedFormComponent);
32193250
fix.detectChanges();
32203251
tick();

projects/igniteui-angular/src/lib/combo/combo.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,14 +1108,14 @@ export class IgxComboComponent extends DisplayDensityBase implements IgxComboBas
11081108

11091109
/** Contains key-value pairs of the selected valueKeys and their resp. displayKeys */
11101110
private registerRemoteEntries(ids: any[], add = true) {
1111-
const selection = this.getValueDisplayPairs(ids);
11121111
if (add) {
1112+
const selection = this.getValueDisplayPairs(ids);
11131113
for (const entry of selection) {
11141114
this._remoteSelection[entry[this.valueKey]] = entry[this.displayKey];
11151115
}
11161116
} else {
1117-
for (const entry of selection) {
1118-
delete this._remoteSelection[entry[this.valueKey]];
1117+
for (const entry of ids) {
1118+
delete this._remoteSelection[entry];
11191119
}
11201120
}
11211121
}

0 commit comments

Comments
 (0)