@@ -187,6 +187,55 @@ describe('computePivot', () => {
187187 expect ( res . grandTotal ) . toBe ( 210 ) // 10 + 200
188188 } )
189189
190+ it ( 'column totals use same aggregation method as cells' , ( ) => {
191+ const res = computePivot < Row > ( {
192+ data : rows ,
193+ rowFields : [ 'region' ] ,
194+ columnFields : [ 'product' ] ,
195+ valueField : 'amount' ,
196+ aggregator : 'avg' ,
197+ } )
198+
199+ // Row totals should sum the cell values (for display purposes)
200+ expect ( res . rowTotals [ 'East' ] ) . toBe ( 105 ) // (10 + 200) / 2 = 105
201+ expect ( res . rowTotals [ 'West' ] ) . toBe ( 105 ) // (90 + 120) / 2 = 105
202+
203+ // Column totals should use the same aggregation method (avg) over all records in that column
204+ // Gadget column: values [90, 10] -> avg = 50
205+ expect ( res . colTotals [ 'Gadget' ] ) . toBe ( 50 )
206+ // Widget column: values [120, 200] -> avg = 160
207+ expect ( res . colTotals [ 'Widget' ] ) . toBe ( 160 )
208+
209+ // Grand total should also use avg over all records
210+ expect ( res . grandTotal ) . toBe ( 105 ) // (90 + 10 + 120 + 200) / 4 = 105
211+ } )
212+
213+ it ( 'column totals with count aggregator' , ( ) => {
214+ const res = computePivot < Row > ( {
215+ data : rows ,
216+ rowFields : [ 'region' ] ,
217+ columnFields : [ 'product' ] ,
218+ aggregator : 'count' , // No valueField, just count records
219+ } )
220+
221+ // Each cell should count records
222+ expect ( res . cells [ 'East' ] [ 'Gadget' ] . value ) . toBe ( 2 ) // 2 records
223+ expect ( res . cells [ 'East' ] [ 'Widget' ] . value ) . toBe ( 1 ) // 1 record
224+ expect ( res . cells [ 'West' ] [ 'Gadget' ] . value ) . toBe ( 1 ) // 1 record
225+ expect ( res . cells [ 'West' ] [ 'Widget' ] . value ) . toBe ( 1 ) // 1 record
226+
227+ // Row totals should sum the counts
228+ expect ( res . rowTotals [ 'East' ] ) . toBe ( 3 ) // 2 + 1
229+ expect ( res . rowTotals [ 'West' ] ) . toBe ( 2 ) // 1 + 1
230+
231+ // Column totals should count total records in each column
232+ expect ( res . colTotals [ 'Gadget' ] ) . toBe ( 3 ) // 2 + 1 = 3 records
233+ expect ( res . colTotals [ 'Widget' ] ) . toBe ( 2 ) // 1 + 1 = 2 records
234+
235+ // Grand total should count all records
236+ expect ( res . grandTotal ) . toBe ( 5 ) // Total records
237+ } )
238+
190239 it ( 'supports custom aggregator' , ( ) => {
191240 const maxAgg : Aggregator < Row > = ( values ) =>
192241 values . length ? Math . max ( ...values ) : 0
0 commit comments