Skip to content

Commit 949acba

Browse files
author
Dylan Huang
committed
Add support for composite columns in computePivot tests
- Introduced a new test case to validate the handling of multiple column fields in the computePivot function. - Verified correct computation of cell values, row totals, column totals, and grand total for the pivot table with composite columns.
1 parent 83dcf47 commit 949acba

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

vite-app/src/util/pivot.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const rows: Row[] = [
1616
{ region: 'East', rep: 'A', product: 'Widget', amount: 200 },
1717
{ region: 'East', rep: 'B', product: 'Gadget', amount: '10' },
1818
{ region: 'East', rep: 'B', product: 'Gadget', amount: 'not-a-number' },
19+
1920
]
2021

2122
describe('computePivot', () => {
@@ -102,6 +103,44 @@ describe('computePivot', () => {
102103
expect(res.cells[rKeyWest][cKeyWidget].value).toBe(120)
103104
})
104105

106+
it('supports multiple column fields (composite columns)', () => {
107+
const res = computePivot<Row>({
108+
data: rows,
109+
rowFields: ['region'],
110+
columnFields: ['product', 'rep'],
111+
valueField: 'amount',
112+
aggregator: 'sum',
113+
})
114+
115+
// Row and column key tuples
116+
expect(res.rowKeyTuples).toEqual([
117+
['East'],
118+
['West'],
119+
])
120+
expect(res.colKeyTuples).toEqual([
121+
['Gadget', 'B'],
122+
['Widget', 'A'],
123+
])
124+
125+
const rEast = 'East'
126+
const rWest = 'West'
127+
const cGadgetB = 'Gadget||B'
128+
const cWidgetA = 'Widget||A'
129+
130+
// Cell values (sum of numeric amounts)
131+
expect(res.cells[rEast][cGadgetB].value).toBe(10)
132+
expect(res.cells[rWest][cGadgetB].value).toBe(90)
133+
expect(res.cells[rEast][cWidgetA].value).toBe(200)
134+
expect(res.cells[rWest][cWidgetA].value).toBe(120)
135+
136+
// Totals
137+
expect(res.rowTotals[rEast]).toBe(210)
138+
expect(res.rowTotals[rWest]).toBe(210)
139+
expect(res.colTotals[cGadgetB]).toBe(100)
140+
expect(res.colTotals[cWidgetA]).toBe(320)
141+
expect(res.grandTotal).toBe(420)
142+
})
143+
105144
it('skips records with undefined row field values', () => {
106145
type LooseRow = {
107146
region?: string

0 commit comments

Comments
 (0)