Skip to content

Commit e9156c6

Browse files
authored
Merge pull request #5957 from IgniteUI/nrobakova/fix-issue-5949-81
Check if data is undefined in the paging pipe
2 parents bad2b8f + d1b78f1 commit e9156c6

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { async, TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
22
import { By } from '@angular/platform-browser';
33
import { IgxGridModule } from './index';
4-
import { ReorderedColumnsComponent, PagingAndEditingComponent, GridIDNameJobTitleComponent } from '../../test-utils/grid-samples.spec';
4+
import {
5+
ReorderedColumnsComponent,
6+
PagingAndEditingComponent,
7+
GridIDNameJobTitleComponent,
8+
GridWithUndefinedDataComponent
9+
} from '../../test-utils/grid-samples.spec';
510
import { PagingComponent } from '../../test-utils/grid-base-components.spec';
611
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
712
import { IgxGridComponent } from './grid.component';
@@ -21,7 +26,8 @@ describe('IgxGrid - Grid Paging', () => {
2126
ReorderedColumnsComponent,
2227
PagingComponent,
2328
PagingAndEditingComponent,
24-
GridIDNameJobTitleComponent
29+
GridIDNameJobTitleComponent,
30+
GridWithUndefinedDataComponent
2531
],
2632
imports: [IgxGridModule, NoopAnimationsModule]
2733
}).compileComponents();
@@ -63,7 +69,7 @@ describe('IgxGrid - Grid Paging', () => {
6369
verifyGridPager(fix, 3, '1', '1 of 4', [true, true, false, false]);
6470
}));
6571

66-
it('should paginate data API', fakeAsync (() => {
72+
it('should paginate data API', fakeAsync(() => {
6773
const fix = TestBed.createComponent(PagingComponent);
6874
fix.detectChanges();
6975

@@ -281,7 +287,7 @@ describe('IgxGrid - Grid Paging', () => {
281287
verifyGridPager(fix, 2, '9', '3 of 3', []);
282288
});
283289

284-
it('activate/deactivate paging', fakeAsync(() => {
290+
it('activate/deactivate paging', fakeAsync(() => {
285291
const fix = TestBed.createComponent(ReorderedColumnsComponent);
286292
const grid = fix.componentInstance.grid;
287293
fix.detectChanges();
@@ -461,6 +467,27 @@ describe('IgxGrid - Grid Paging', () => {
461467
expect(gridElement.querySelector(PAGER_CLASS)).not.toBeNull();
462468
}));
463469

470+
it('should not throw error when data is undefined', fakeAsync(() => {
471+
let errorMessage = '';
472+
const fix = TestBed.createComponent(GridWithUndefinedDataComponent);
473+
try {
474+
fix.detectChanges();
475+
} catch (ex) {
476+
errorMessage = ex.message;
477+
}
478+
expect(errorMessage).toBe('');
479+
const grid = fix.componentInstance.grid;
480+
let paginator = grid.nativeElement.querySelector(PAGER_CLASS);
481+
expect(paginator).toBeNull();
482+
expect(grid.rowList.length).toBe(0);
483+
tick(305);
484+
fix.detectChanges();
485+
486+
paginator = grid.nativeElement.querySelector(PAGER_CLASS);
487+
expect(paginator).toBeDefined();
488+
expect(grid.rowList.length).toBe(5);
489+
}));
490+
464491
function verifyGridPager(fix, rowsCount, firstCellValue, pagerText, buttonsVisibility) {
465492
const disabled = 'igx-button--disabled';
466493
const grid = fix.componentInstance.grid;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export class IgxGridPagingPipe implements PipeTransform {
104104
index: page,
105105
recordsPerPage: perPage
106106
};
107-
DataUtil.correctPagingState(state, collection.data.length);
107+
DataUtil.correctPagingState(state, collection.data ? collection.data.length : 0);
108108

109109
const result = {
110110
data: DataUtil.page(cloneArray(collection.data), state),

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, TemplateRef, ViewChild, Input, AfterViewInit, ChangeDetectorRef } from '@angular/core';
1+
import { Component, TemplateRef, ViewChild, Input, AfterViewInit, ChangeDetectorRef, OnInit } from '@angular/core';
22
import { IgxGridCellComponent } from '../grids/cell.component';
33
import { IgxDateSummaryOperand, IgxNumberSummaryOperand, IgxSummaryResult } from '../grids/summaries/grid-summary';
44
import { IGridCellEventArgs, IGridEditEventArgs, IgxGridTransaction } from '../grids/grid-base.component';
@@ -11,6 +11,7 @@ import { IgxColumnComponent } from '../grids/column.component';
1111
import { IgxTransactionService } from '../services';
1212
import { IgxFilteringOperand } from '../data-operations/filtering-condition';
1313
import { ExpressionUI } from '../grids/filtering/grid-filtering.service';
14+
import { IgxGridComponent } from '../grids/grid';
1415

1516
@Component({
1617
template: `<div style="width: 800px; height: 600px;">
@@ -1119,3 +1120,23 @@ export class DynamicColumnsComponent extends GridWithSizeComponent {
11191120
width = '800px';
11201121
height = '800px';
11211122
}
1123+
1124+
@Component({
1125+
template: GridTemplateStrings.declareGrid(
1126+
` [width]="width" [height]="height" [paging]="'true'" [perPage]="perPage" [primaryKey]="'ProductID'"`,
1127+
'', ColumnDefinitions.productBasic)
1128+
})
1129+
export class GridWithUndefinedDataComponent implements OnInit {
1130+
@ViewChild(IgxGridComponent, { static: true })
1131+
public grid: IgxGridComponent;
1132+
public data ;
1133+
public perPage = 5;
1134+
public width = '800px';
1135+
public height = '600px';
1136+
1137+
public ngOnInit(): void {
1138+
setTimeout(() => {
1139+
this.data = SampleTestData.foodProductDataExtended();
1140+
}, 300);
1141+
}
1142+
}

0 commit comments

Comments
 (0)