Skip to content

Commit 5239aaf

Browse files
committed
fix(combo): add test for combo in IgxGrid cell template
1 parent 3f203d1 commit 5239aaf

File tree

1 file changed

+95
-23
lines changed

1 file changed

+95
-23
lines changed

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

+95-23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { AbsoluteScrollStrategy, AutoPositionStrategy, ConnectedPositioningStrat
1616
import { configureTestSuite } from '../test-utils/configure-suite';
1717
import { UIInteractions, wait } from '../test-utils/ui-interactions.spec';
1818
import { IgxSimpleComboComponent, ISimpleComboSelectionChangingEventArgs } from './public_api';
19+
import { IgxGridComponent } from '../grids/grid/grid.component';
20+
import { IGX_GRID_DIRECTIVES } from '../grids/grid/public_api';
1921

2022

2123
const CSS_CLASS_COMBO = 'igx-combo';
@@ -2239,40 +2241,110 @@ describe('IgxSimpleCombo', () => {
22392241

22402242
expect(combo.comboInput.value).toEqual('');
22412243
});
2244+
});
22422245

2243-
it('should display correct value after the value has been changed from the form and then by the user', fakeAsync(() => {
2244-
fixture = TestBed.createComponent(IgxComboRemoteDataInReactiveFormComponent);
2245-
fixture.detectChanges();
2246-
combo = fixture.componentInstance.reactiveCombo;
2247-
reactiveForm = fixture.componentInstance.reactiveForm;
2248-
reactiveControl = reactiveForm.form.controls['comboValue'];
2249-
input = fixture.debugElement.query(By.css(`.${CSS_CLASS_COMBO_INPUTGROUP}`));
2250-
tick()
2251-
fixture.detectChanges();
2252-
expect(combo).toBeTruthy();
2246+
describe('Integration', () => {
2247+
let grid: IgxGridComponent;
22532248

2254-
combo.select(0);
2249+
beforeAll(waitForAsync(() => {
2250+
TestBed.configureTestingModule({
2251+
imports: [
2252+
NoopAnimationsModule,
2253+
IgxSimpleComboInGridComponent
2254+
]
2255+
}).compileComponents();
2256+
}));
2257+
beforeEach(() => {
2258+
fixture = TestBed.createComponent(IgxSimpleComboInGridComponent);
22552259
fixture.detectChanges();
2256-
expect(combo.value).toEqual(0);
2257-
expect(input.nativeElement.value).toEqual('Product 0');
2260+
grid = fixture.componentInstance.grid;
2261+
});
2262+
it('Combo in IgxGrid cell display template correctly handles selection - issue #14305', async () => {
2263+
const firstRecRegionCell = grid.gridAPI.get_cell_by_index(0, 'Region') as any;
2264+
let comboNativeEl = firstRecRegionCell.nativeElement.querySelector(SIMPLE_COMBO_ELEMENT);
2265+
const comboToggleButton = comboNativeEl.querySelector(`.${CSS_CLASS_TOGGLEBUTTON}`);
22582266

2259-
reactiveControl.setValue(3);
2267+
UIInteractions.simulateClickEvent(comboToggleButton);
22602268
fixture.detectChanges();
2261-
expect(combo.value).toEqual(3);
2262-
expect(input.nativeElement.value).toEqual('Product 3');
22632269

2264-
combo.open();
2265-
fixture.detectChanges();
2266-
const item1 = fixture.debugElement.queryAll(By.css(`.${CSS_CLASS_DROPDOWNLISTITEM}`))[5];
2267-
item1.triggerEventHandler('click', UIInteractions.getMouseEvent('click'));
2270+
const comboDropDownList = fixture.debugElement.query(By.css(`.${CSS_CLASS_DROPDOWNLIST}`));
2271+
const firstItem = comboDropDownList.nativeElement.querySelector(`.${CSS_CLASS_DROPDOWNLISTITEM}`);
2272+
2273+
UIInteractions.simulateClickEvent(firstItem);
22682274
fixture.detectChanges();
22692275

2270-
expect(combo.value).toEqual(5);
2271-
expect(input.nativeElement.value).toEqual('Product 5');
2272-
}));
2276+
const firstRegionCellObject = grid.getCellByColumn(0, 'Region');
2277+
expect(firstRegionCellObject.value).toEqual(fixture.componentInstance.regions[0]);
2278+
2279+
try {
2280+
// combo should not throw from the selection getter at this point
2281+
grid.navigateTo(fixture.componentInstance.data.length - 1, 0);
2282+
await wait(30);
2283+
fixture.detectChanges();
2284+
} catch (error) {
2285+
fail(`Test failed with error: ${error}`)
2286+
}
2287+
2288+
const virtState = grid.verticalScrollContainer.state;
2289+
expect(virtState.startIndex).toBe(grid.dataView.length - virtState.chunkSize);
2290+
2291+
// These will fail in case the editor (combo) in the cell display template is not bound to the cell value
2292+
// as the first record's selected value will be applied on the reused combos bc of the virtualization
2293+
for (let i = virtState.startIndex; i < virtState.startIndex + virtState.chunkSize && i < grid.dataView.length; i++) {
2294+
const targetCell = grid.gridAPI.get_cell_by_index(i, 'Region') as any;
2295+
comboNativeEl = targetCell.nativeElement.querySelector(SIMPLE_COMBO_ELEMENT);
2296+
const comboInput = comboNativeEl.querySelector('input');
2297+
expect(comboInput.value).toBe('', `Failed on index: ${i.toString()}`);
2298+
}
2299+
2300+
for (let i = virtState.startIndex; i < virtState.startIndex + virtState.chunkSize && i < grid.dataView.length; i++) {
2301+
const cell = grid.getCellByColumn(i, 'Region');
2302+
expect(cell.value).toBe(undefined);
2303+
}
2304+
});
22732305
});
22742306
});
22752307

2308+
@Component({
2309+
template: `
2310+
<igx-grid #grid [data]="data" [autoGenerate]="false" width="100%" height="500px" [primaryKey]="'ID'">
2311+
<igx-column field="ID" header="ID" [dataType]="'number'" width="100px">
2312+
</igx-column>
2313+
<igx-column field="Region" header="Region" dataType="string" width="220px">
2314+
<ng-template igxCell let-cell="cell">
2315+
<div>
2316+
<igx-simple-combo [id]="'region-' + cell.row.data.ID"
2317+
[data]="regions" placeholder="Choose Region..."
2318+
[(ngModel)]="cell.value"
2319+
>
2320+
</igx-simple-combo>
2321+
</div>
2322+
</ng-template>
2323+
</igx-column>
2324+
</igx-grid>
2325+
`,
2326+
standalone: true,
2327+
imports: [IgxSimpleComboComponent, IGX_GRID_DIRECTIVES, FormsModule]
2328+
})
2329+
class IgxSimpleComboInGridComponent {
2330+
@ViewChild('grid', { read: IgxGridComponent, static: true })
2331+
public grid: IgxGridComponent;
2332+
2333+
public data = [];
2334+
public regions = [];
2335+
constructor() {
2336+
for (let i = 1; i <= 15; i++) {
2337+
this.data.push({
2338+
ID: i,
2339+
region: undefined
2340+
});
2341+
}
2342+
for (let i = 1; i <= 5; i++) {
2343+
this.regions.push(`Region ${i}`);
2344+
}
2345+
}
2346+
}
2347+
22762348
@Component({
22772349
template: `
22782350
<igx-simple-combo #combo [placeholder]="'Location'" [data]='items' [style.--ig-size]="'var(--ig-size-' + size + ')'"

0 commit comments

Comments
 (0)