Skip to content

Commit 0c27de9

Browse files
committed
add tests
1 parent 44f6277 commit 0c27de9

3 files changed

Lines changed: 394 additions & 0 deletions

File tree

packages/devextreme/js/__internal/grids/data_grid/__tests__/__mock__/model/data_grid.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import type { Column } from '@js/ui/data_grid';
22
import DataGrid from '@js/ui/data_grid';
33
import { DataGridBaseModel } from '@ts/grids/grid_core/__tests__/__mock__/model/data_grid_base';
44

5+
const SELECTORS = {
6+
summaryItem: 'dx-datagrid-summary-item',
7+
groupFooter: 'dx-datagrid-group-footer',
8+
footerRow: 'dx-footer-row',
9+
};
10+
511
export class DataGridModel extends DataGridBaseModel<DataGrid> {
612
protected NAME = 'dxDataGrid';
713

@@ -35,4 +41,16 @@ export class DataGridModel extends DataGridBaseModel<DataGrid> {
3541
instance.columnOption(columnName, optionName, optionValue);
3642
});
3743
}
44+
45+
public getFooterRow(): HTMLElement | null {
46+
return this.root.querySelector(`.${SELECTORS.footerRow}`);
47+
}
48+
49+
public getGroupFooterRows(): NodeListOf<HTMLElement> {
50+
return this.root.querySelectorAll(`.${SELECTORS.groupFooter}`);
51+
}
52+
53+
public getSummaryItems(row: HTMLElement): NodeListOf<HTMLElement> {
54+
return row.querySelectorAll(`.${SELECTORS.summaryItem}`);
55+
}
3856
}
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import {
2+
afterEach, beforeEach, describe, expect, it, jest,
3+
} from '@jest/globals';
4+
5+
import {
6+
afterTest,
7+
beforeTest,
8+
createDataGrid,
9+
} from '../../../grid_core/__tests__/__mock__/helpers/utils';
10+
11+
describe('Summary', () => {
12+
beforeEach(beforeTest);
13+
afterEach(afterTest);
14+
15+
describe('column lookup map performance optimization', () => {
16+
const dataSource = [
17+
{
18+
id: 1, name: 'Alice', value: 10, category: 'A',
19+
},
20+
{
21+
id: 2, name: 'Bob', value: 20, category: 'A',
22+
},
23+
{
24+
id: 3, name: 'Carol', value: 30, category: 'B',
25+
},
26+
{
27+
id: 4, name: 'Dave', value: 40, category: 'B',
28+
},
29+
];
30+
31+
it('should correctly calculate total summary', async () => {
32+
const { instance } = await createDataGrid({
33+
dataSource,
34+
columns: ['id', 'name', 'value', 'category'],
35+
summary: {
36+
totalItems: [
37+
{ column: 'value', summaryType: 'sum' },
38+
{ column: 'value', summaryType: 'avg' },
39+
{ column: 'id', summaryType: 'count' },
40+
],
41+
},
42+
});
43+
44+
jest.runAllTimers();
45+
46+
expect(instance.getTotalSummaryValue('sum_value')).toBe(100);
47+
expect(instance.getTotalSummaryValue('avg_value')).toBe(25);
48+
expect(instance.getTotalSummaryValue('count_id')).toBe(4);
49+
});
50+
51+
it('should render total footer with summary items', async () => {
52+
const { component } = await createDataGrid({
53+
dataSource,
54+
columns: ['id', 'name', 'value', 'category'],
55+
summary: {
56+
totalItems: [
57+
{ column: 'value', summaryType: 'sum' },
58+
],
59+
},
60+
});
61+
62+
jest.runAllTimers();
63+
64+
const footerRow = component.getFooterRow() as HTMLElement;
65+
66+
expect(footerRow).not.toBeNull();
67+
68+
const summaryItems = component.getSummaryItems(footerRow);
69+
const summary = dataSource.reduce((acc, item) => acc + item.value, 0);
70+
71+
expect(summaryItems.length).toBe(1);
72+
expect(summaryItems[0].textContent).toContain(summary.toString());
73+
});
74+
75+
it('should calculate group summary with many groupItems', async () => {
76+
const { component } = await createDataGrid({
77+
dataSource,
78+
columns: [
79+
{ dataField: 'id' },
80+
{ dataField: 'name' },
81+
{ dataField: 'value' },
82+
{ dataField: 'category', groupIndex: 0 },
83+
],
84+
summary: {
85+
groupItems: [
86+
{ column: 'value', summaryType: 'sum', showInGroupFooter: false },
87+
{ column: 'value', summaryType: 'avg', showInGroupFooter: false },
88+
{ column: 'id', summaryType: 'count', showInGroupFooter: false },
89+
],
90+
},
91+
});
92+
93+
jest.runAllTimers();
94+
95+
const groupRows = component.getGroupRows();
96+
97+
expect(groupRows.length).toBe(2);
98+
99+
// Group summary items with showInGroupFooter: false
100+
// are rendered inline in the group row cell text
101+
const firstGroupRowText = groupRows[0].textContent ?? '';
102+
103+
expect(firstGroupRowText).toContain('Sum');
104+
expect(firstGroupRowText).toContain('Avg');
105+
expect(firstGroupRowText).toContain('Count');
106+
});
107+
108+
it('should render group footer summary', async () => {
109+
const { component } = await createDataGrid({
110+
dataSource,
111+
columns: [
112+
{ dataField: 'id' },
113+
{ dataField: 'name' },
114+
{ dataField: 'value' },
115+
{ dataField: 'category', groupIndex: 0 },
116+
],
117+
summary: {
118+
groupItems: [
119+
{
120+
column: 'value',
121+
summaryType: 'sum',
122+
showInGroupFooter: true,
123+
},
124+
],
125+
},
126+
});
127+
128+
jest.runAllTimers();
129+
130+
const groupFooterRows = component.getGroupFooterRows();
131+
132+
expect(groupFooterRows.length).toBe(2);
133+
134+
const summaryItems = component.getSummaryItems(
135+
groupFooterRows[0],
136+
);
137+
138+
expect(summaryItems.length).toBe(1);
139+
expect(summaryItems[0].textContent).toContain('30');
140+
});
141+
142+
it('should correctly calculate summary with showInColumn option', async () => {
143+
const { component } = await createDataGrid({
144+
dataSource,
145+
columns: [
146+
{ dataField: 'id' },
147+
{ dataField: 'name' },
148+
{ dataField: 'value' },
149+
{ dataField: 'category', groupIndex: 0 },
150+
],
151+
summary: {
152+
groupItems: [
153+
{
154+
column: 'value',
155+
summaryType: 'sum',
156+
showInColumn: 'name',
157+
showInGroupFooter: false,
158+
},
159+
],
160+
},
161+
});
162+
163+
jest.runAllTimers();
164+
165+
const groupRows = component.getGroupRows();
166+
167+
expect(groupRows.length).toBe(2);
168+
});
169+
170+
it('should handle many summary items without errors', async () => {
171+
const groupItems = Array.from(
172+
{ length: 50 },
173+
(_, i) => ({
174+
column: i % 2 === 0 ? 'value' : 'id',
175+
summaryType: 'sum' as const,
176+
showInGroupFooter: false,
177+
name: `summary_${i}`,
178+
}),
179+
);
180+
181+
const { component } = await createDataGrid({
182+
dataSource,
183+
columns: [
184+
{ dataField: 'id' },
185+
{ dataField: 'name' },
186+
{ dataField: 'value' },
187+
{ dataField: 'category', groupIndex: 0 },
188+
],
189+
summary: {
190+
groupItems,
191+
},
192+
});
193+
194+
jest.runAllTimers();
195+
196+
const groupRows = component.getGroupRows();
197+
198+
expect(groupRows.length).toBe(2);
199+
});
200+
201+
it('should handle combined total and group summary', async () => {
202+
const { instance, component } = await createDataGrid({
203+
dataSource,
204+
columns: [
205+
{ dataField: 'id' },
206+
{ dataField: 'name' },
207+
{ dataField: 'value' },
208+
{ dataField: 'category', groupIndex: 0 },
209+
],
210+
summary: {
211+
totalItems: [
212+
{ column: 'value', summaryType: 'sum' },
213+
],
214+
groupItems: [
215+
{
216+
column: 'value',
217+
summaryType: 'sum',
218+
showInGroupFooter: false,
219+
},
220+
],
221+
},
222+
});
223+
224+
jest.runAllTimers();
225+
226+
expect(instance.getTotalSummaryValue('sum_value')).toBe(100);
227+
228+
const footerRow = component.getFooterRow();
229+
230+
expect(footerRow).not.toBeNull();
231+
232+
const groupRows = component.getGroupRows();
233+
234+
expect(groupRows.length).toBe(2);
235+
});
236+
});
237+
});

0 commit comments

Comments
 (0)