map.test.ts

import { getMapWithValueArray } from './map';

describe('test map utility function: getMapWithValueArray()', () => {
  it('should throw if array type doesnt match', () => {
    expect(() => getMapWithValueArray({}, '')).toThrowError();
  });

  it('should create the map by id from the array', () => {
    const inputArray = [
      { id: 1, value: 'item 1' },
      { id: 1, value: 'item 2' },
      { id: 2, value: 'item 3' }
    ];
    const expectedMap = new Map();
    expectedMap.set(1, [inputArray[0], inputArray[1]]);
    expectedMap.set(2, [inputArray[2]]);
    const mapCreated = getMapWithValueArray(inputArray, 'id');
    expect(mapCreated).toEqual(expectedMap);
  });
});