UNPKG

apphouse

Version:

Component library for React that uses observable state management and theme-able components.

81 lines (62 loc) 1.92 kB
import { fromArray } from './fromArray'; describe('fromArray', () => { test('should convert an array of objects with idKey to an object', () => { const arr = [ { id: '1', name: 'John' }, { id: '2', name: 'Jane' } ]; const result = fromArray(arr); expect(result).toEqual({ '1': { id: '1', name: 'John' }, '2': { id: '2', name: 'Jane' } }); }); test('should convert an array of objects with custom idKey to an object', () => { const arr = [ { _id: '1', name: 'John' }, { _id: '2', name: 'Jane' } ]; const result = fromArray(arr, '_id'); expect(result).toEqual({ '1': { _id: '1', name: 'John' }, '2': { _id: '2', name: 'Jane' } }); }); test('should convert an array of objects with custom valueKey to an object', () => { const arr = [ { id: '1', title: 'Title 1' }, { id: '2', title: 'Title 2' } ]; const result = fromArray(arr, 'id', 'title'); expect(result).toEqual({ '1': 'Title 1', '2': 'Title 2' }); }); test('should convert an array of objects to an object using "all" valueKey', () => { const arr = [ { id: '1', name: 'John' }, { id: '2', name: 'Jane' } ]; const result = fromArray(arr, 'id', 'all'); expect(result).toEqual({ '1': { id: '1', name: 'John' }, '2': { id: '2', name: 'Jane' } }); }); test('should convert an array of strings to an object', () => { const arr = ['foo', 'bar', 'baz']; const result = fromArray(arr); expect(result).toEqual({ foo: 'foo', bar: 'bar', baz: 'baz' }); }); test('should convert an array of non-string values to an object', () => { const arr = [42, true, { key: 'value' }]; const result = fromArray(arr); expect(result).toEqual(expect.any(Object)); expect(Object.keys(result)).toHaveLength(arr.length); }); });