Skip to content

Commit ab82bd0

Browse files
HristoP96HristoP96
authored andcommitted
chore(*): return pager functions
1 parent 921f13d commit ab82bd0

File tree

2 files changed

+125
-95
lines changed

2 files changed

+125
-95
lines changed

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

Lines changed: 124 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,31 @@ import { NoopAnimationsModule } from '@angular/platform-browser/animations';
1010
import { configureTestSuite } from '../../test-utils/configure-suite';
1111
import { wait } from '../../test-utils/ui-interactions.spec';
1212
import { IgxNumberFilteringOperand } from '../../data-operations/filtering-condition';
13-
import { GridFunctions } from '../../test-utils/grid-functions.spec';
14-
import { ControlsFunction } from '../../test-utils/controls-functions.spec';
13+
import { GridFunctions, PAGER_CLASS } from '../../test-utils/grid-functions.spec';
14+
import { ControlsFunction, BUTTON_DISABLED_CLASS } from '../../test-utils/controls-functions.spec';
1515
import { DebugElement } from '@angular/core';
1616

17+
function verifyGridPager (fix, rowsCount, firstCellValue, pagerText, buttonsVisibility) {
18+
const grid = fix.componentInstance.grid;
19+
20+
expect(grid.getCellByColumn(0, 'ID').value).toMatch(firstCellValue);
21+
expect(grid.rowList.length).toEqual(rowsCount, 'Invalid number of rows initialized');
22+
23+
if (pagerText != null) {
24+
expect(grid.nativeElement.querySelector(PAGER_CLASS)).toBeDefined();
25+
expect(grid.nativeElement.querySelectorAll('igx-select').length).toEqual(1);
26+
expect(grid.nativeElement.querySelector('.igx-paginator__pager > div').textContent).toMatch(pagerText);
27+
}
28+
if (buttonsVisibility != null && buttonsVisibility.length === 4) {
29+
const pagingButtons = GridFunctions.getPagingButtons(grid.nativeElement);
30+
expect(pagingButtons.length).toEqual(4);
31+
expect(pagingButtons[0].className.includes(BUTTON_DISABLED_CLASS)).toBe(buttonsVisibility[0]);
32+
expect(pagingButtons[1].className.includes(BUTTON_DISABLED_CLASS)).toBe(buttonsVisibility[1]);
33+
expect(pagingButtons[2].className.includes(BUTTON_DISABLED_CLASS)).toBe(buttonsVisibility[2]);
34+
expect(pagingButtons[3].className.includes(BUTTON_DISABLED_CLASS)).toBe(buttonsVisibility[3]);
35+
}
36+
}
37+
1738
describe('IgxGrid - Grid Paging #grid', () => {
1839
configureTestSuite();
1940

@@ -47,27 +68,27 @@ describe('IgxGrid - Grid Paging #grid', () => {
4768
it('should paginate data UI', () => {
4869

4970
expect(grid.paging).toBeTruthy();
50-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
71+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
5172

5273
// Go to next page
5374
GridFunctions.navigateToNextPage(grid.nativeElement);
5475
fix.detectChanges();
55-
GridFunctions.verifyGridPager(fix, 3, '4', '2\xA0of\xA04', [false, false, false, false]);
76+
verifyGridPager(fix, 3, '4', '2\xA0of\xA04', [false, false, false, false]);
5677

5778
// Go to last page
5879
GridFunctions.navigateToLastPage(grid.nativeElement);
5980
fix.detectChanges();
60-
GridFunctions.verifyGridPager(fix, 1, '10', '4\xA0of\xA04', [false, false, true, true]);
81+
verifyGridPager(fix, 1, '10', '4\xA0of\xA04', [false, false, true, true]);
6182

6283
// Go to previous page
6384
GridFunctions.navigateToPrevPage(grid.nativeElement);
6485
fix.detectChanges();
65-
GridFunctions.verifyGridPager(fix, 3, '7', '3\xA0of\xA04', [false, false, false, false]);
86+
verifyGridPager(fix, 3, '7', '3\xA0of\xA04', [false, false, false, false]);
6687

6788
// Go to first page
6889
GridFunctions.navigateToFirstPage(grid.nativeElement);
6990
fix.detectChanges();
70-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
91+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
7192
});
7293

7394
it('should paginate data API', () => {
@@ -79,30 +100,30 @@ describe('IgxGrid - Grid Paging #grid', () => {
79100
fix.detectChanges();
80101

81102
expect(grid.onPagingDone.emit).toHaveBeenCalled();
82-
GridFunctions.verifyGridPager(fix, 3, '7', '3\xA0of\xA04', []);
103+
verifyGridPager(fix, 3, '7', '3\xA0of\xA04', []);
83104

84105
// Go to next page
85106
grid.nextPage();
86107
fix.detectChanges();
87108

88109
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(2);
89110
expect(grid.isLastPage).toBe(true);
90-
GridFunctions.verifyGridPager(fix, 1, '10', '4\xA0of\xA04', []);
111+
verifyGridPager(fix, 1, '10', '4\xA0of\xA04', []);
91112

92113
// Go to next page when last page is selected
93114
grid.nextPage();
94115
fix.detectChanges();
95116

96117
expect(grid.isLastPage).toBe(true);
97118
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(2);
98-
GridFunctions.verifyGridPager(fix, 1, '10', '4\xA0of\xA04', []);
119+
verifyGridPager(fix, 1, '10', '4\xA0of\xA04', []);
99120

100121
// Go to previous page
101122
grid.previousPage();
102123
fix.detectChanges();
103124

104125
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(3);
105-
GridFunctions.verifyGridPager(fix, 3, '7', '3\xA0of\xA04', []);
126+
verifyGridPager(fix, 3, '7', '3\xA0of\xA04', []);
106127
expect(grid.isLastPage).toBe(false);
107128
expect(grid.isFirstPage).toBe(false);
108129

@@ -111,31 +132,31 @@ describe('IgxGrid - Grid Paging #grid', () => {
111132
fix.detectChanges();
112133

113134
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(4);
114-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
135+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
115136
expect(grid.isFirstPage).toBe(true);
116137

117138
// Go to previous page when first page is selected
118139
grid.previousPage();
119140
fix.detectChanges();
120141

121142
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(4);
122-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
143+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
123144
expect(grid.isFirstPage).toBe(true);
124145

125146
// Go to negative page number
126147
grid.paginate(-3);
127148
fix.detectChanges();
128149

129150
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(4);
130-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
151+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
131152
});
132153

133154
it('change paging settings UI', () => {
134155

135156
expect(grid.paging).toBeTruthy();
136157
expect(grid.perPage).toEqual(3, 'Invalid page size');
137158

138-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
159+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', []);
139160

140161
// Change page size
141162
GridFunctions.clickOnPageSelectElement(fix);
@@ -144,7 +165,7 @@ describe('IgxGrid - Grid Paging #grid', () => {
144165

145166
expect(grid.paging).toBeTruthy();
146167
expect(grid.perPage).toEqual(10, 'Invalid page size');
147-
GridFunctions.verifyGridPager(fix, 10, '1', '1\xA0of\xA01', []);
168+
verifyGridPager(fix, 10, '1', '1\xA0of\xA01', []);
148169
});
149170

150171
it('change paging settings API', () => {
@@ -155,15 +176,15 @@ describe('IgxGrid - Grid Paging #grid', () => {
155176

156177
expect(grid.paging).toBeTruthy();
157178
expect(grid.perPage).toEqual(2, 'Invalid page size');
158-
GridFunctions.verifyGridPager(fix, 2, '1', '1\xA0of\xA05', []);
179+
verifyGridPager(fix, 2, '1', '1\xA0of\xA05', []);
159180

160181
// Turn off paging
161182
grid.paging = false;
162183
fix.detectChanges();
163184

164185
expect(grid.paging).toBeFalsy();
165186
expect(grid.perPage).toEqual(2, 'Invalid page size after paging was turned off');
166-
GridFunctions.verifyGridPager(fix, 10, '1', null, []);
187+
verifyGridPager(fix, 10, '1', null, []);
167188
expect(GridFunctions.getGridPaginator(grid)).toBeNull();
168189
expect(grid.nativeElement.querySelectorAll('.igx-paginator > select').length).toEqual(0);
169190
});
@@ -181,7 +202,7 @@ describe('IgxGrid - Grid Paging #grid', () => {
181202

182203
expect(grid.paging).toBeTruthy();
183204
expect(grid.perPage).toEqual(2, 'Invalid page size');
184-
GridFunctions.verifyGridPager(fix, 2, '3', '2\xA0of\xA05', []);
205+
verifyGridPager(fix, 2, '3', '2\xA0of\xA05', []);
185206

186207
// Change page size to be 5
187208
spyOn(grid.onPagingDone, 'emit');
@@ -190,7 +211,7 @@ describe('IgxGrid - Grid Paging #grid', () => {
190211
fix.detectChanges();
191212
let vScrollBar = grid.verticalScrollContainer.getScroll();
192213
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(1);
193-
GridFunctions.verifyGridPager(fix, 5, '1', '1\xA0of\xA02', [true, true, false, false]);
214+
verifyGridPager(fix, 5, '1', '1\xA0of\xA02', [true, true, false, false]);
194215
expect(vScrollBar.scrollHeight).toBeGreaterThanOrEqual(250);
195216
expect(vScrollBar.scrollHeight).toBeLessThanOrEqual(255);
196217

@@ -201,7 +222,7 @@ describe('IgxGrid - Grid Paging #grid', () => {
201222
vScrollBar = grid.verticalScrollContainer.getScroll();
202223
// onPagingDone should be emitted only if we have a change in the page number
203224
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(1);
204-
GridFunctions.verifyGridPager(fix, 5, '1', '1\xA0of\xA01', [true, true, true, true]);
225+
verifyGridPager(fix, 5, '1', '1\xA0of\xA01', [true, true, true, true]);
205226
expect(vScrollBar.scrollHeight).toBeGreaterThanOrEqual(500);
206227
expect(vScrollBar.scrollHeight).toBeLessThanOrEqual(510);
207228

@@ -210,7 +231,7 @@ describe('IgxGrid - Grid Paging #grid', () => {
210231
await wait();
211232
fix.detectChanges();
212233
expect(grid.onPagingDone.emit).toHaveBeenCalledTimes(1);
213-
GridFunctions.verifyGridPager(fix, 5, '1', '1\xA0of\xA01', [true, true, true, true]);
234+
verifyGridPager(fix, 5, '1', '1\xA0of\xA01', [true, true, true, true]);
214235
expect(vScrollBar.scrollHeight).toBeGreaterThanOrEqual(500);
215236
expect(vScrollBar.scrollHeight).toBeLessThanOrEqual(510);
216237
}));
@@ -263,17 +284,17 @@ describe('IgxGrid - Grid Paging #grid', () => {
263284
// Filter column
264285
grid.filter('ID', 1, IgxNumberFilteringOperand.instance().condition('greaterThan'));
265286
fix.detectChanges();
266-
GridFunctions.verifyGridPager(fix, 3, '2', '1\xA0of\xA03', [true, true, false, false]);
287+
verifyGridPager(fix, 3, '2', '1\xA0of\xA03', [true, true, false, false]);
267288

268289
// Filter column
269290
grid.filter('ID', 1, IgxNumberFilteringOperand.instance().condition('equals'));
270291
fix.detectChanges();
271-
GridFunctions.verifyGridPager(fix, 1, '1', '1\xA0of\xA01', [true, true, true, true]);
292+
verifyGridPager(fix, 1, '1', '1\xA0of\xA01', [true, true, true, true]);
272293

273294
// Reset filters
274295
grid.clearFilter('ID');
275296
fix.detectChanges();
276-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
297+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
277298
});
278299

279300
it('should work correct with crud operations', () => {
@@ -284,49 +305,49 @@ describe('IgxGrid - Grid Paging #grid', () => {
284305
// Delete first row
285306
grid.deleteRow(1);
286307
fix.detectChanges();
287-
GridFunctions.verifyGridPager(fix, 3, '2', '1\xA0of\xA03', [true, true, false, false]);
308+
verifyGridPager(fix, 3, '2', '1\xA0of\xA03', [true, true, false, false]);
288309
expect(grid.totalPages).toBe(3);
289310

290311
// Delete all rows on first page
291312
grid.deleteRow(2);
292313
grid.deleteRow(3);
293314
grid.deleteRow(4);
294315
fix.detectChanges();
295-
GridFunctions.verifyGridPager(fix, 3, '5', '1\xA0of\xA02', []);
316+
verifyGridPager(fix, 3, '5', '1\xA0of\xA02', []);
296317
expect(grid.totalPages).toBe(2);
297318

298319
// Delete all rows on first page
299320
grid.deleteRow(5);
300321
grid.deleteRow(6);
301322
grid.deleteRow(7);
302323
fix.detectChanges();
303-
GridFunctions.verifyGridPager(fix, 3, '8', '1\xA0of\xA01', [true, true, true, true]);
324+
verifyGridPager(fix, 3, '8', '1\xA0of\xA01', [true, true, true, true]);
304325
expect(grid.totalPages).toBe(1);
305326

306327
// Add new row
307328
grid.addRow({ ID: 1, Name: 'Test Name', JobTitle: 'Test Job Title' });
308329
fix.detectChanges();
309-
GridFunctions.verifyGridPager(fix, 3, '8', '1\xA0of\xA02', [true, true, false, false]);
330+
verifyGridPager(fix, 3, '8', '1\xA0of\xA02', [true, true, false, false]);
310331
expect(grid.totalPages).toBe(2);
311332

312333
grid.nextPage();
313334
fix.detectChanges();
314-
GridFunctions.verifyGridPager(fix, 1, '1', '2\xA0of\xA02', []);
335+
verifyGridPager(fix, 1, '1', '2\xA0of\xA02', []);
315336

316337
// Add new rows on second page
317338
grid.addRow({ ID: 2, Name: 'Test Name', JobTitle: 'Test Job Title' });
318339
grid.addRow({ ID: 3, Name: 'Test Name', JobTitle: 'Test Job Title' });
319340
grid.addRow({ ID: 4, Name: 'Test Name', JobTitle: 'Test Job Title' });
320341
fix.detectChanges();
321-
GridFunctions.verifyGridPager(fix, 3, '1', '2\xA0of\xA03', [false, false, false, false]);
342+
verifyGridPager(fix, 3, '1', '2\xA0of\xA03', [false, false, false, false]);
322343
expect(grid.totalPages).toBe(3);
323344

324345
// Go to last page and delete the row
325346
grid.nextPage();
326347
fix.detectChanges();
327348
grid.deleteRow(4);
328349
fix.detectChanges();
329-
GridFunctions.verifyGridPager(fix, 3, '1', '2\xA0of\xA02', [false, false, true, true]);
350+
verifyGridPager(fix, 3, '1', '2\xA0of\xA02', [false, false, true, true]);
330351
});
331352

332353
it('should not throw when initialized in a grid with % height', () => {
@@ -338,16 +359,80 @@ describe('IgxGrid - Grid Paging #grid', () => {
338359
});
339360

340361
it('"paginate" method should paginate correctly', () => {
341-
GridFunctions.testPagingAPI(fix, grid, (pageIndex) => grid.paginate(pageIndex));
362+
const page = (index: number) => grid.paginate(index);
363+
let desiredPageIndex = 2;
364+
page(2);
365+
fix.detectChanges();
366+
367+
expect(grid.page).toBe(desiredPageIndex);
368+
369+
// non-existent page, should not paginate
370+
page(-2);
371+
fix.detectChanges();
372+
expect(grid.page).toBe(desiredPageIndex);
373+
374+
// non-existent page, should not paginate
375+
page(666);
376+
fix.detectChanges();
377+
expect(grid.page).toBe(desiredPageIndex);
378+
379+
// first page
380+
desiredPageIndex = 0;
381+
page(desiredPageIndex);
382+
fix.detectChanges();
383+
expect(grid.page).toBe(desiredPageIndex);
384+
385+
// last page
386+
desiredPageIndex = grid.totalPages - 1;
387+
page(desiredPageIndex);
388+
fix.detectChanges();
389+
expect(grid.page).toBe(desiredPageIndex);
390+
391+
// last page + 1, should not paginate
392+
page(grid.totalPages);
393+
fix.detectChanges();
394+
expect(grid.page).toBe(desiredPageIndex);
342395
});
343396

344397
it('"page" property should paginate correctly', () => {
345-
GridFunctions.testPagingAPI(fix, grid, (pageIndex) => grid.page = pageIndex);
398+
const page = (index: number) => grid.page = index;
399+
let desiredPageIndex = 2;
400+
page(2);
401+
fix.detectChanges();
402+
403+
expect(grid.page).toBe(desiredPageIndex);
404+
405+
// non-existent page, should not paginate
406+
page(-2);
407+
fix.detectChanges();
408+
expect(grid.page).toBe(desiredPageIndex);
409+
410+
// non-existent page, should not paginate
411+
page(666);
412+
fix.detectChanges();
413+
expect(grid.page).toBe(desiredPageIndex);
414+
415+
// first page
416+
desiredPageIndex = 0;
417+
page(desiredPageIndex);
418+
fix.detectChanges();
419+
expect(grid.page).toBe(desiredPageIndex);
420+
421+
// last page
422+
desiredPageIndex = grid.totalPages - 1;
423+
page(desiredPageIndex);
424+
fix.detectChanges();
425+
expect(grid.page).toBe(desiredPageIndex);
426+
427+
// last page + 1, should not paginate
428+
page(grid.totalPages);
429+
fix.detectChanges();
430+
expect(grid.page).toBe(desiredPageIndex);
346431
});
347432

348433
it('should hide paginator when there is no data or all records are filtered out.', () => {
349434

350-
GridFunctions.verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
435+
verifyGridPager(fix, 3, '1', '1\xA0of\xA04', [true, true, false, false]);
351436

352437
// Filter out all records
353438
grid.filter('ID', 1000, IgxNumberFilteringOperand.instance().condition('greaterThan'));
@@ -386,28 +471,28 @@ describe('IgxGrid - Grid Paging #grid', () => {
386471
expect(grid.paging).toBeTruthy();
387472
expect(grid.page).toEqual(0);
388473
expect(grid.perPage).toMatch('4', 'Invalid page size');
389-
GridFunctions.verifyGridPager(fix, 4, '1', '1\xA0of\xA03', []);
474+
verifyGridPager(fix, 4, '1', '1\xA0of\xA03', []);
390475

391476
// Next page button click
392477
GridFunctions.clickOnPaginatorButton(nextBtn);
393478
fix.detectChanges();
394479

395480
expect(grid.page).toEqual(1, 'Invalid page index');
396-
GridFunctions.verifyGridPager(fix, 4, '5', '2\xA0of\xA03', []);
481+
verifyGridPager(fix, 4, '5', '2\xA0of\xA03', []);
397482

398483
// Previous page button click
399484
GridFunctions.clickOnPaginatorButton(prevBtn);
400485
fix.detectChanges();
401486

402487
expect(grid.page).toEqual(0, 'Invalid page index');
403-
GridFunctions.verifyGridPager(fix, 4, '1', '1\xA0of\xA03', []);
488+
verifyGridPager(fix, 4, '1', '1\xA0of\xA03', []);
404489

405490
// Go to 3rd page button click
406491
GridFunctions.clickOnPaginatorButton(idxPageBtn);
407492
fix.detectChanges();
408493

409494
expect(grid.page).toEqual(2, 'Invalid page index');
410-
GridFunctions.verifyGridPager(fix, 2, '9', '3\xA0of\xA03', []);
495+
verifyGridPager(fix, 2, '9', '3\xA0of\xA03', []);
411496
});
412497

413498
it('should not throw error when data is undefined', fakeAsync(() => {

0 commit comments

Comments
 (0)