import {ObjectHelper} from '../../lib'; const exampleObject = { name: 'test', contact: { phone: '01234566', }, }; describe('get', () => { test('should get name from object', () => { const value = ObjectHelper.get(exampleObject, 'name'); expect(value).toBe(exampleObject.name); }); test('should get concated property', () => { const value = ObjectHelper.get(exampleObject, 'contact.phone'); expect(value).toBe(exampleObject.contact.phone); }); test('should return undefined if unknown key', () => { const value = ObjectHelper.get(exampleObject, 'fullname'); expect(value).toBe(undefined); }); test('should return undefined if broken key', () => { const value = ObjectHelper.get(exampleObject, 'not.existing.key'); expect(value).toBe(undefined); }); }); describe('set', () => { test('should set name from object', () => { const expectedValue = 'NewName'; ObjectHelper.set(exampleObject, 'name', expectedValue ); expect(exampleObject.name).toBe(expectedValue); }); test('should set inherited property', () => { const expectedValue = '011234835345'; ObjectHelper.set(exampleObject, 'contact.phone', expectedValue ); expect(exampleObject.contact.phone).toBe(expectedValue); }); test('should raise error if property unknown', () => { const expectedValue = 'Test'; try { ObjectHelper.set(exampleObject, 'now.existing.key', expectedValue ); } catch (ex: any) { expect(ex.message).toBe('Unknown Key'); } }); }); describe('objectToStringDeep', () => { test('should transform object to string', () => { const result = ObjectHelper.objectToStringDeep(exampleObject); expect(result).toMatch(exampleObject.contact.phone); expect(result).toMatch(exampleObject.name); }); test('should return empty string if null', () => { let result = ObjectHelper.objectToStringDeep(null); expect(result).toBe(''); result = ObjectHelper.objectToStringDeep(undefined); expect(result).toBe(''); }); }); describe('_iterateThrough', () => { const value = ObjectHelper._iterateThrough(exampleObject, ['contact', 'phone'], -1); expect(value).toBe(undefined); });