test: improve utils coverage (#4230)
* test: improve utils coverage * refactor: review - rename isDefined to isNonNullable, update tests and return statement
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
import { groupArrayItemsBy } from '../groupArrayItemsBy';
|
||||
|
||||
describe('groupArrayItemsBy', () => {
|
||||
it('groups an array of objects by a computed key', () => {
|
||||
// Given
|
||||
const array = [
|
||||
{ id: '1', type: 'fruit', value: 'apple' },
|
||||
{ id: '2', type: 'fruit', value: 'banana' },
|
||||
{ id: '3', type: 'vegetable', value: 'carrot' },
|
||||
];
|
||||
const computeGroupKey = ({ type }: (typeof array)[0]) => type;
|
||||
|
||||
// When
|
||||
const result = groupArrayItemsBy(array, computeGroupKey);
|
||||
|
||||
// Then
|
||||
expect(result).toEqual({
|
||||
fruit: [
|
||||
{ id: '1', type: 'fruit', value: 'apple' },
|
||||
{ id: '2', type: 'fruit', value: 'banana' },
|
||||
],
|
||||
vegetable: [{ id: '3', type: 'vegetable', value: 'carrot' }],
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,23 @@
|
||||
import { mapArrayToObject } from '~/utils/array/mapArrayToObject';
|
||||
|
||||
describe('mapArrayToObject', () => {
|
||||
it('maps an array of objects to an object with computed keys', () => {
|
||||
// Given
|
||||
const array = [
|
||||
{ id: '1', value: 'one' },
|
||||
{ id: '2', value: 'two' },
|
||||
{ id: '3', value: 'three' },
|
||||
];
|
||||
const computeItemKey = ({ id }: { id: string }) => id;
|
||||
|
||||
// When
|
||||
const result = mapArrayToObject(array, computeItemKey);
|
||||
|
||||
// Then
|
||||
expect(result).toEqual({
|
||||
'1': { id: '1', value: 'one' },
|
||||
'2': { id: '2', value: 'two' },
|
||||
'3': { id: '3', value: 'three' },
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user