Skip to content

Commit c14c5e1

Browse files
authored
Merge pull request #12278 from IgniteUI/dpetev/tree-grid-no-data
fix(grid,tree-grid): avoid errors on basic init without data set
2 parents c98b7a9 + 729e2d2 commit c14c5e1

File tree

6 files changed

+85
-22
lines changed

6 files changed

+85
-22
lines changed

projects/igniteui-angular/src/lib/grids/grid/grid.component.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,40 @@ describe('IgxGrid Component Tests #grid', () => {
26612661
fix.detectChanges();
26622662
});
26632663
});
2664+
2665+
describe('Setting null data', () => {
2666+
beforeAll(waitForAsync(() => {
2667+
TestBed.configureTestingModule({
2668+
declarations: [
2669+
IgxGridNoDataComponent,
2670+
IgxGridTestComponent
2671+
],
2672+
imports: [
2673+
NoopAnimationsModule, IgxGridModule
2674+
]
2675+
})
2676+
.compileComponents();
2677+
}));
2678+
2679+
it('should not throw error when data is null', () => {
2680+
const fix = TestBed.createComponent(IgxGridNoDataComponent);
2681+
fix.componentInstance.grid.batchEditing = true;
2682+
expect(() => fix.detectChanges()).not.toThrow();
2683+
});
2684+
2685+
it('should not throw error when data is set to null', () => {
2686+
const fix = TestBed.createComponent(IgxGridTestComponent);
2687+
fix.componentInstance.data = null;
2688+
expect(() => fix.detectChanges()).not.toThrow();
2689+
});
2690+
2691+
it('should not throw error when data is set to null and transactions are enabled', () => {
2692+
const fix = TestBed.createComponent(IgxGridTestComponent);
2693+
fix.componentInstance.grid.batchEditing = true;
2694+
fix.componentInstance.data = null;
2695+
expect(() => fix.detectChanges()).not.toThrow();
2696+
});
2697+
});
26642698
});
26652699

26662700
@Component({
@@ -3258,3 +3292,16 @@ export class IgxGridPerformanceComponent implements AfterViewInit, OnInit {
32583292
this.delta = new Date().getTime() - this.startTime;
32593293
}
32603294
}
3295+
3296+
@Component({
3297+
template: `
3298+
<igx-grid>
3299+
<igx-column field="ID"></igx-column>
3300+
<igx-column field="Name" [hasSummary]="true"></igx-column>
3301+
<igx-paginator></igx-paginator>
3302+
</igx-grid>
3303+
`
3304+
})
3305+
export class IgxGridNoDataComponent {
3306+
@ViewChild(IgxGridComponent, { static: true }) public grid: IgxGridComponent;
3307+
}

projects/igniteui-angular/src/lib/grids/grid/grid.pipes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export class IgxGridPagingPipe implements PipeTransform {
8787
index: page,
8888
recordsPerPage: perPage
8989
};
90-
const total = this.grid._totalRecords >= 0 ? this.grid._totalRecords : collection.data.length;
90+
const total = this.grid._totalRecords >= 0 ? this.grid._totalRecords : collection.data?.length;
9191
DataUtil.correctPagingState(state, total);
9292

9393
const result = {

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-api.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class IgxTreeGridAPIService extends GridBaseAPIService<GridType> {
2323

2424
public get_summary_data() {
2525
const grid = this.grid;
26-
const data = grid.processedRootRecords.filter(row => row.isFilteredOutParent === undefined || row.isFilteredOutParent === false)
26+
const data = grid.processedRootRecords?.filter(row => row.isFilteredOutParent === undefined || row.isFilteredOutParent === false)
2727
.map(rec => rec.data);
28-
if (grid.transactions.enabled) {
28+
if (data && grid.transactions.enabled) {
2929
const deletedRows = grid.transactions.getTransactionLog().filter(t => t.type === TransactionType.DELETE).map(t => t.id);
3030
deletedRows.forEach(rowID => {
3131
const tempData = grid.primaryKey ? data.map(rec => rec[grid.primaryKey]) : data;

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.component.spec.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
IgxTreeGridWrappedInContComponent,
1010
IgxTreeGridDefaultLoadingComponent,
1111
IgxTreeGridCellSelectionComponent,
12-
IgxTreeGridSummariesTransactionsComponent
12+
IgxTreeGridSummariesTransactionsComponent,
13+
IgxTreeGridNoDataComponent
1314
} from '../../test-utils/tree-grid-components.spec';
1415
import { wait } from '../../test-utils/ui-interactions.spec';
1516
import { GridSelectionMode } from '../common/enums';
@@ -29,7 +30,8 @@ describe('IgxTreeGrid Component Tests #tGrid', () => {
2930
IgxTreeGridWrappedInContComponent,
3031
IgxTreeGridDefaultLoadingComponent,
3132
IgxTreeGridCellSelectionComponent,
32-
IgxTreeGridSummariesTransactionsComponent
33+
IgxTreeGridSummariesTransactionsComponent,
34+
IgxTreeGridNoDataComponent
3335
],
3436
imports: [
3537
NoopAnimationsModule,
@@ -264,27 +266,21 @@ describe('IgxTreeGrid Component Tests #tGrid', () => {
264266

265267
describe('Setting null data', () => {
266268
it('should not throw error when data is null', () => {
267-
let errorMessage = '';
269+
fix = TestBed.createComponent(IgxTreeGridNoDataComponent);
270+
fix.componentInstance.treeGrid.batchEditing = true;
271+
expect(() => fix.detectChanges()).not.toThrow();
272+
});
273+
274+
it('should not throw error when data is set to null', () => {
268275
fix = TestBed.createComponent(IgxTreeGridCellSelectionComponent);
269276
fix.componentInstance.data = null;
270-
try {
271-
fix.detectChanges();
272-
} catch (ex) {
273-
errorMessage = ex.message;
274-
}
275-
expect(errorMessage).toBe('');
277+
expect(() => fix.detectChanges()).not.toThrow();
276278
});
277279

278-
it('should not throw error when data is null and transactions are enabled', () => {
279-
let errorMessage = '';
280+
it('should not throw error when data is set to null and transactions are enabled', () => {
280281
fix = TestBed.createComponent(IgxTreeGridSummariesTransactionsComponent);
281282
fix.componentInstance.data = null;
282-
try {
283-
fix.detectChanges();
284-
} catch (ex) {
285-
errorMessage = ex.message;
286-
}
287-
expect(errorMessage).toBe('');
283+
expect(() => fix.detectChanges()).not.toThrow();
288284
});
289285
});
290286

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.pipes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export class IgxTreeGridHierarchizingPipe implements PipeTransform {
2323
const treeGridRecordsMap = new Map<any, ITreeGridRecord>();
2424
const flatData: any[] = [];
2525

26+
if (!collection || !collection.length) {
27+
return collection;
28+
}
29+
2630
if (primaryKey && foreignKey) {
2731
hierarchicalRecords = this.hierarchizeFlatData(collection, primaryKey, foreignKey, treeGridRecordsMap, flatData);
2832
} else if (childDataKey) {

projects/igniteui-angular/src/lib/test-utils/tree-grid-components.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,12 @@ export class IgxTreeGridExpandingComponent {
155155
@Component({
156156
template: `
157157
<igx-tree-grid #treeGrid [data]="data" primaryKey="ID" childDataKey="Employees" expansionDepth="2"
158-
width="900px" height="500px" [perPage]="10">
158+
width="900px" height="500px">
159159
<igx-column [field]="'ID'" dataType="number"></igx-column>
160160
<igx-column [field]="'Name'" dataType="string"></igx-column>
161161
<igx-column [field]="'HireDate'" dataType="date"></igx-column>
162162
<igx-column [field]="'Age'" dataType="number"></igx-column>
163-
<igx-paginator></igx-paginator>
163+
<igx-paginator [perPage]="10"></igx-paginator>
164164
</igx-tree-grid>
165165
`
166166
})
@@ -169,6 +169,22 @@ export class IgxTreeGridCellSelectionComponent {
169169
public data = SampleTestData.employeeTreeData();
170170
}
171171

172+
@Component({
173+
template: `
174+
<igx-tree-grid #treeGrid primaryKey="ID" childDataKey="Employees" expansionDepth="2"
175+
width="900px" height="500px">
176+
<igx-column [field]="'ID'" dataType="number"></igx-column>
177+
<igx-column [field]="'Name'" dataType="string"></igx-column>
178+
<igx-column [field]="'HireDate'" dataType="date" [hasSummary]="true"></igx-column>
179+
<igx-column [field]="'Age'" dataType="number" [hasSummary]="true"></igx-column>
180+
<igx-paginator [perPage]="10"></igx-paginator>
181+
</igx-tree-grid>
182+
`
183+
})
184+
export class IgxTreeGridNoDataComponent {
185+
@ViewChild(IgxTreeGridComponent, { static: true }) public treeGrid: IgxTreeGridComponent;
186+
}
187+
172188
// Test Component with 'string' dataType tree-column
173189
@Component({
174190
template: `

0 commit comments

Comments
 (0)