Skip to content

Commit bf6487a

Browse files
MKirovaMKirova
authored andcommitted
Add basic aggregation tests.
1 parent 199bf86 commit bf6487a

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

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

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { fakeAsync, TestBed } from '@angular/core/testing';
2+
import { By } from '@angular/platform-browser';
23
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
34
import { IgxPivotGridModule } from 'igniteui-angular';
45
import { configureTestSuite } from '../../test-utils/configure-suite';
5-
import { IgxPivotGridTestBaseComponent } from '../../test-utils/pivot-grid-samples.spec';
6+
import { IgxPivotGridTestBaseComponent, IgxTotalSaleAggregate } from '../../test-utils/pivot-grid-samples.spec';
7+
import { UIInteractions } from '../../test-utils/ui-interactions.spec';
8+
const CSS_CLASS_DROP_DOWN_BASE = 'igx-drop-down';
9+
const CSS_CLASS_LIST = 'igx-drop-down__list';
10+
const CSS_CLASS_ITEM = 'igx-drop-down__item';
611

712
describe('Basic IgxPivotGrid #pivotGrid', () => {
813
let fixture;
@@ -37,4 +42,81 @@ describe('Basic IgxPivotGrid #pivotGrid', () => {
3742
expect(cells.first.nativeElement.classList).toContain('test');
3843
expect(cells.last.nativeElement.classList).not.toContain('test');
3944
});
45+
46+
describe('IgxPivotGrid Features #pivotGrid', () => {
47+
it('should allow changing default aggregation via value chip drop-down.', () => {
48+
fixture.detectChanges();
49+
const pivotGrid = fixture.componentInstance.pivotGrid;
50+
const headerRow = fixture.nativeElement.querySelector('igx-pivot-header-row');
51+
const valueChip = headerRow.querySelector('igx-chip[id="UnitsSold"]');
52+
let content = valueChip.querySelector('.igx-chip__content');
53+
expect(content.textContent.trim()).toBe('SUM(UnitsSold)');
54+
55+
const aggregatesIcon = valueChip.querySelectorAll('igx-icon')[1];
56+
aggregatesIcon.click();
57+
fixture.detectChanges();
58+
const items = fixture.debugElement.queryAll(By.css(`.${CSS_CLASS_ITEM}`));
59+
expect(items.length).toBe(5);
60+
// select count
61+
items[0].triggerEventHandler('click', UIInteractions.getMouseEvent('click'));
62+
fixture.detectChanges();
63+
64+
// check chip and row
65+
content = valueChip.querySelector('.igx-chip__content');
66+
expect(content.textContent.trim()).toBe('COUNT(UnitsSold)');
67+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'Bulgaria-UnitsSold').value).toBe(2);
68+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'USA-UnitsSold').value).toBe(3);
69+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'Uruguay-UnitsSold').value).toBe(2);
70+
71+
});
72+
it('should allow showing custom aggregations via pivot configuration.', () => {
73+
const pivotGrid = fixture.componentInstance.pivotGrid;
74+
pivotGrid.pivotConfiguration.values.push({
75+
member: 'AmountOfSale',
76+
displayName: 'Amount of Sale',
77+
aggregate: {
78+
key: 'SUM',
79+
aggregator: IgxTotalSaleAggregate.totalSale,
80+
label: 'Sum of Sale'
81+
},
82+
aggregateList: [{
83+
key: 'SUM',
84+
aggregator: IgxTotalSaleAggregate.totalSale,
85+
label: 'Sum of Sale'
86+
},{
87+
key: 'MIN',
88+
aggregator: IgxTotalSaleAggregate.totalMin,
89+
label: 'Minimum of Sale'
90+
},{
91+
key: 'MAX',
92+
aggregator: IgxTotalSaleAggregate.totalMax,
93+
label: 'Maximum of Sale'
94+
}],
95+
enabled: true
96+
});
97+
pivotGrid.pipeTrigger++;
98+
pivotGrid.setupColumns();
99+
fixture.detectChanges();
100+
const headerRow = fixture.nativeElement.querySelector('igx-pivot-header-row');
101+
const valueChip = headerRow.querySelector('igx-chip[id="AmountOfSale"]');
102+
let content = valueChip.querySelector('.igx-chip__content');
103+
expect(content.textContent.trim()).toBe('SUM(Amount of Sale)');
104+
105+
const aggregatesIcon = valueChip.querySelectorAll('igx-icon')[1];
106+
aggregatesIcon.click();
107+
fixture.detectChanges();
108+
109+
const items = fixture.debugElement.queryAll(By.css(`.${CSS_CLASS_ITEM}`));
110+
expect(items.length).toBe(3);
111+
// select min
112+
items[1].triggerEventHandler('click', UIInteractions.getMouseEvent('click'));
113+
fixture.detectChanges();
114+
// check chip and row values
115+
content = valueChip.querySelector('.igx-chip__content');
116+
expect(content.textContent.trim()).toBe('MIN(Amount of Sale)');
117+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'Bulgaria-AmountOfSale').value).toBe(3612.42);
118+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'USA-AmountOfSale').value).toBe(0);
119+
expect(pivotGrid.gridAPI.get_cell_by_index(0, 'Uruguay-AmountOfSale').value).toBe(242.08);
120+
});
121+
});
40122
});

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

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { IgxPivotNumericAggregate } from '../grids/pivot-grid/pivot-grid-aggregate';
33
import { IgxPivotGridComponent } from '../grids/pivot-grid/pivot-grid.component';
4-
import { IPivotConfiguration } from '../grids/pivot-grid/pivot-grid.interface';
4+
import { IPivotConfiguration, PivotAggregation } from '../grids/pivot-grid/pivot-grid.interface';
55

66
@Component({
77
template: `
@@ -73,8 +73,8 @@ export class IgxPivotGridTestBaseComponent {
7373
member: 'UnitsSold',
7474
aggregate: {
7575
aggregator: IgxPivotNumericAggregate.sum,
76-
key: 'sum',
77-
label: 'SUM',
76+
key: 'SUM',
77+
label: 'Sum',
7878
},
7979
enabled: true,
8080
// dataType: 'currency',
@@ -85,8 +85,8 @@ export class IgxPivotGridTestBaseComponent {
8585
member: 'UnitPrice',
8686
aggregate:{
8787
aggregator: IgxPivotNumericAggregate.sum,
88-
key: 'sum',
89-
label: 'SUM',
88+
key: 'SUM',
89+
label: 'Sum',
9090
},
9191
enabled: true,
9292
dataType: 'currency'
@@ -98,3 +98,28 @@ export class IgxPivotGridTestBaseComponent {
9898
public callback = (rowData: any, columnKey: any) => rowData[columnKey] >= 5;
9999
public callback1 = (rowData: any, columnKey: any) => rowData[columnKey] < 5;
100100
}
101+
102+
export class IgxTotalSaleAggregate {
103+
public static totalSale: PivotAggregation = (members, data: any) =>
104+
data.reduce((accumulator, value) => accumulator + value.UnitPrice * value.UnitsSold, 0);
105+
106+
public static totalMin: PivotAggregation = (members, data: any) => {
107+
let min = 0;
108+
if (data.length === 1) {
109+
min = data[0].UnitPrice * data[0].UnitsSold || 0;
110+
} else if (data.length > 1) {
111+
min = data.reduce((a, b) => Math.min(a.UnitPrice * a.UnitsSold || 0, b.UnitPrice * b.UnitsSold || 0));
112+
}
113+
return min;
114+
};
115+
116+
public static totalMax: PivotAggregation = (members, data: any) => {
117+
let max = 0;
118+
if (data.length === 1) {
119+
max = data[0].UnitPrice * data[0].UnitsSold;
120+
} else if (data.length > 1) {
121+
max = data.reduce((a, b) => Math.max(a.UnitPrice * a.UnitsSold, b.UnitPrice * b.UnitsSold));
122+
}
123+
return max;
124+
};
125+
}

0 commit comments

Comments
 (0)