import { reduce } from './reduce';

describe('reduce', () => {
  test('should reduce an array of numbers to a sum', () => {
    const array = [1, 2, 3, 4, 5];
    const sum = reduce(array, (accumulator, item) => accumulator + item, 0);
    expect(sum).toBe(15);
  });

  test('should reduce an array of strings to a concatenated string', () => {
    const array = ['Hello', ' ', 'World', '!'];
    const result = reduce(array, (accumulator, item) => accumulator + item, '');
    expect(result).toBe('Hello World!');
  });

  test('should reduce an array of objects to a single object', () => {
    const array = [
      { name: 'Alice', age: 25 },
      { name: 'Bob', age: 30 },
      { name: 'Charlie', age: 35 }
    ];

    const result = reduce(
      array,
      (accumulator, item) => {
        accumulator[item.name] = item.age;
        return accumulator;
      },
      {}
    );

    expect(result).toEqual({ Alice: 25, Bob: 30, Charlie: 35 });
  });
});
