import type { ColumnAggregationMapType } from '../state' import type { Column } from '../types' import { getAggregationValueFn, getRowValues, getAggregationResults, } from './aggregation' describe('aggregation', () => { describe('getAggregationValueFn', () => { it('should use footer over cell function', () => { const footer: Column['footer'] = { value: () => 1, } const cell: Column['cell'] = { value: () => 2, } const result = getAggregationValueFn( footer, cell )?.({ columnId: '1', row: {}, rowMeta: {} }) expect(result).toBe(1) }) it('should use cell if no footer function', () => { const footer: Column['footer'] = {} const cell: Column['cell'] = { value: () => 2, } const result = getAggregationValueFn( footer, cell )?.({ columnId: '1', row: {}, rowMeta: {} }) expect(result).toBe(2) }) it('should return null if no defined functions', () => { const footer: Column['footer'] = {} const cell: Column['cell'] = {} const fn = getAggregationValueFn(footer, cell) expect(fn).toBe(null) }) }) describe('getRowValues', () => { it('should get row values', () => { const rowIds = ['1', '2', '3', '4'] const entities = new Map( Object.entries({ 1: { id: '1', title: 'Zeta', tags: 2, }, 2: { id: '2', title: 'Gamma', tags: 4 }, 3: { id: '3', title: 'Alpha', tags: 5, }, 4: { id: '4', title: 'Beta' }, }) ) const meta = new Map() const configs: ColumnAggregationMapType[] = [ { id: 'tags', value: null, }, { id: 'title', value: ({ row }) => row.title.length, }, ] expect(getRowValues(rowIds, entities, meta, configs)).toEqual( new Map([ ['tags', [2, 4, 5]], ['title', [4, 5, 5, 4]], ]) ) }) }) describe('getAggregationResults', () => { it('should get results', () => { const values = new Map([ ['tags', [2, 4, 5]], ['title', [1, 1, 1, 1]], ['custom', [1, 2, 1, 1]], ]) const configs: ColumnAggregationMapType[] = [ { id: 'tags', fn: 'sum', value: null, }, { id: 'title', fn: 'size', value: null, }, { id: 'custom', fn: (values) => (values as number[]).reduce( (prev, curr: number) => prev * curr, 1 ), value: () => 1, }, ] const result = getAggregationResults(configs, values) expect(result).toEqual( new Map([ ['tags', { result: 11, values: [2, 4, 5] }], ['title', { result: 4, values: [1, 1, 1, 1] }], ['custom', { result: 2, values: [1, 2, 1, 1] }], ]) ) }) }) })