File size: 783 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { sumByProperty } from '@/utils/array/sumByProperty';

describe('sumByProperty', () => {
  it('should sum numeric property values', () => {
    const items = [
      { id: '1', amount: 10 },
      { id: '2', amount: 20 },
      { id: '3', amount: 30 },
    ];

    expect(items.reduce(sumByProperty('amount'), 0)).toBe(60);
  });

  it('should skip non-numeric values', () => {
    const items = [
      { id: '1', amount: 10 },
      { id: '2', amount: 'not-a-number' as unknown as number },
      { id: '3', amount: 30 },
    ];

    expect(items.reduce(sumByProperty('amount'), 0)).toBe(40);
  });

  it('should handle an empty array', () => {
    const items: { id: string; amount: number }[] = [];

    expect(items.reduce(sumByProperty('amount'), 0)).toBe(0);
  });
});