import ObjectUtils from '../../src/utils/ObjectUtils'; describe('ObjectUtils', () => { describe('getProp', () => { it('should get an object value based on its path', async () => { const value = ObjectUtils.getProp(defaultObjectMock, 'food.fruits.banana'); expect(value).toBe(2); }); it('should get an object value based on its path', async () => { const value = ObjectUtils.getProp(defaultObjectMock, 'food'); expect(JSON.stringify(value)).toBe(JSON.stringify(defaultObjectMock.food)); }); it('should return undefined if the path doesn\'t exist in the object', async () => { const value = ObjectUtils.getProp(defaultObjectMock, 'vegetables'); expect(value).toBe(undefined); }); }); describe('hasEquivalentValues', () => { it('should return true if both objects have the same property value', () => { const obj1 = { value: 10 }; const obj2 = { value: 10 }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(true); }); it('should return false if both objects have different property values', () => { const obj1 = { value: 10 }; const obj2 = { value: 20 }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(false); }); it('should return true if both objects are equivalent with custom property to compare', () => { const obj1 = { customProp: 'hello' }; const obj2 = { customProp: 'hello' }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2, 'customProp')).toBe(true); }); it('should return false if both objects are not equivalent with custom property to compare', () => { const obj1 = { customProp: 'hello' }; const obj2 = { customProp: 'world' }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2, 'customProp')).toBe(false); }); it('should return false if one object does not have the property', () => { const obj1 = { value: 10 }; const obj2 = { anotherProp: 10 }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(false); }); it('should return true if comparing primitive values directly', () => { const obj1 = 5; const obj2 = 5; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(true); }); it('should return false if comparing different primitive values', () => { const obj1 = 5; const obj2 = 10; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(false); }); it('should return true when comparing object with a null value', () => { const obj1 = null; const obj2 = null; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(true); }); it('should return false when comparing object with null and non-null value', () => { const obj1 = null; const obj2 = { value: 5 }; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(false); }); it('should return true when comparing non-object values that are equal', () => { const obj1 = 'same'; const obj2 = 'same'; expect(ObjectUtils.hasEquivalentProps(obj1, obj2)).toBe(true); }); }); describe('copy', () => { it('should copy a simple object', () => { const original = { nome: 'Sankhya', cidade: 'Uberlandia' }; const copy = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); }); it('should copy an array of objects', () => { const original = [{ nome: 'Sankhya' }, { cidade: 'Uberlandia' }]; const copy: any = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); expect(copy[0]).not.toBe(original[0]); }); it('should copy an object with nested properties', () => { const original = { pessoa: { nome: 'Sankhya', cidade: 'Uberlandia' } }; const copy: any = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); expect(copy.pessoa).not.toBe(original.pessoa); }); it('should copy an object with null or undefined values', () => { const original = { nome: null, cidade: undefined, idade: 30 }; const copy = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); }); it('should copy an empty object', () => { const original = {}; const copy = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); }); it('should copy an object with mixed types of values', () => { const original = { numero: 123, texto: 'Test', array: [1, 2, 3], objeto: { chave: 'valor' }, }; const copy: any = ObjectUtils.copy(original); expect(copy).toEqual(original); expect(copy).not.toBe(original); expect(copy.array).not.toBe(original.array); expect(copy.objeto).not.toBe(original.objeto); }); }); describe('objectToString', () => { it('should convert a simple object to JSON string', () => { const original = { nome: 'Sankhya', cidade: 'Uberlandia' }; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('{"nome":"Sankhya","cidade":"Uberlandia"}'); }); it('should convert an array of objects to JSON string', () => { const original = [{ nome: 'Sankhya' }, { cidade: 'Uberlandia' }]; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('[{"nome":"Sankhya"},{"cidade":"Uberlandia"}]'); }); it('should convert a nested object to JSON string', () => { const original = { pessoa: { nome: 'Sankhya', cidade: 'Uberlandia' } }; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('{"pessoa":{"nome":"Sankhya","cidade":"Uberlandia"}}'); }); it('should convert an object with null and undefined values to JSON string', () => { const original = { nome: null, cidade: undefined, idade: 30 }; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('{"nome":null,"idade":30}'); }); it('should convert an empty object to JSON string', () => { const original = {}; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('{}'); }); it('should convert an object with mixed types of values to JSON string', () => { const original = { numero: 123, texto: 'Test', array: [1, 2, 3], objeto: { chave: 'valor' }, }; const jsonString = ObjectUtils.objectToString(original); expect(jsonString).toBe('{"numero":123,"texto":"Test","array":[1,2,3],"objeto":{"chave":"valor"}}'); }); }); describe('stringToObject', () => { it('should convert a simple JSON string to an object', () => { const jsonString = '{"nome":"Sankhya","cidade":"Uberlandia"}'; const parsedObject = ObjectUtils.stringToObject(jsonString); expect(parsedObject).toEqual({ nome: 'Sankhya', cidade: 'Uberlandia' }); }); it('should convert a JSON string representing an array of objects to an array of objects', () => { const jsonString = '[{"nome":"Sankhya"},{"cidade":"Uberlandia"}]'; const parsedObject = ObjectUtils.stringToObject(jsonString); expect(parsedObject).toEqual([{ nome: 'Sankhya' }, { cidade: 'Uberlandia' }]); }); it('should convert a JSON string representing a nested object to an object', () => { const jsonString = '{"pessoa":{"nome":"Sankhya","cidade":"Uberlandia"}}'; const parsedObject = ObjectUtils.stringToObject(jsonString); expect(parsedObject).toEqual({ pessoa: { nome: 'Sankhya', cidade: 'Uberlandia' } }); }); it('should convert a JSON string with null and undefined values to an object', () => { const jsonString = '{"nome":null,"cidade":null,"idade":30}'; const parsedObject = ObjectUtils.stringToObject(jsonString); expect(parsedObject).toEqual({ nome: null, cidade: null, idade: 30 }); }); it('should convert an empty JSON string to an empty object', () => { const jsonString = '{}'; const parsedObject = ObjectUtils.stringToObject(jsonString); expect(parsedObject).toEqual({}); }); it('should throw an error for an invalid JSON string', () => { const invalidJsonString = '{"nome": "Sankhya", "cidade": "Uberlandia"'; expect(() => { ObjectUtils.stringToObject(invalidJsonString); }).toThrow(SyntaxError); }); }); describe('sortByProperty', () => { it('should sort an object by a numerical property', () => { const original = { obj1: { id: 3, nome: 'Sankhya' }, obj2: { id: 1, nome: 'Uberlandia' }, obj3: { id: 2, nome: 'Tech' }, }; const sorted = ObjectUtils.sortByProperty(original, 'id'); const expected = { obj2: { id: 1, nome: 'Uberlandia' }, obj3: { id: 2, nome: 'Tech' }, obj1: { id: 3, nome: 'Sankhya' }, }; expect(sorted).toEqual(expected); }); it('should sort an object by a string property', () => { const original = { obj1: { nome: 'Sankhya' }, obj2: { nome: 'Uberlandia' }, obj3: { nome: 'Tech' }, }; const sorted = ObjectUtils.sortByProperty(original, 'nome'); const expected = { obj3: { nome: 'Tech' }, obj2: { nome: 'Uberlandia' }, obj1: { nome: 'Sankhya' }, }; expect(sorted).toEqual(expected); }); it('should handle null values correctly when sorting', () => { const original = { obj1: { id: 3, nome: 'Sankhya' }, obj2: { id: null, nome: 'Uberlandia' }, obj3: { id: 2, nome: 'Tech' }, }; const sorted = ObjectUtils.sortByProperty(original, 'id'); const expected = { obj2: { id: null, nome: 'Uberlandia' }, obj3: { id: 2, nome: 'Tech' }, obj1: { id: 3, nome: 'Sankhya' }, }; expect(sorted).toEqual(expected); }); }); describe('removeEmptyValues', () => { it('should remove properties with null or undefined values in a simple object', () => { const original = { nome: 'Sankhya', cidade: null, idade: undefined, pais: 'Brasil', }; const result = ObjectUtils.removeEmptyValues(original); const expected = { nome: 'Sankhya', pais: 'Brasil' }; expect(result).toEqual(expected); }); it('should remove properties with null or undefined values in a nested object', () => { const original = { pessoa: { nome: 'Sankhya', idade: null, endereco: { rua: 'Av. Brasil', cidade: undefined, }, }, empresa: { nome: 'Tech', sede: null, }, }; const result = ObjectUtils.removeEmptyValues(original); const expected = { pessoa: { nome: 'Sankhya', endereco: { rua: 'Av. Brasil' }, }, empresa: { nome: 'Tech' }, }; expect(result).toEqual(expected); }); it('should remove empty values in an array inside an object', () => { const original = { pessoas: [ { nome: 'Sankhya', idade: null }, { nome: 'Uberlandia', idade: undefined }, { nome: 'Tech', idade: 30 }, ], }; const result = ObjectUtils.removeEmptyValues(original); const expected = { pessoas: { 0: { nome: 'Sankhya' }, 1: { nome: 'Uberlandia' }, 2: { nome: 'Tech', idade: 30 }, }, }; expect(result).toEqual(expected); }); it('should not modify an object with no null or undefined values', () => { const original = { nome: 'Sankhya', cidade: 'Uberlandia', idade: 30, }; const result = ObjectUtils.removeEmptyValues(original); expect(result).toEqual(original); }); it('should return an empty object if all properties are removed', () => { const original = { nome: null, cidade: undefined, }; const result = ObjectUtils.removeEmptyValues(original); const expected = {}; expect(result).toEqual(expected); }); }); describe('equals', () => { it('should return true for identical objects', () => { const obj1 = { nome: 'Sankhya', idade: 30 }; const obj2 = { nome: 'Sankhya', idade: 30 }; expect(ObjectUtils.equals(obj1, obj2)).toBe(true); }); it('should return false for different objects', () => { const obj1 = { nome: 'Sankhya', idade: 30 }; const obj2 = { nome: 'Uberlandia', idade: 25 }; expect(ObjectUtils.equals(obj1, obj2)).toBe(false); }); it('should return true for identical nested objects', () => { const obj1 = { pessoa: { nome: 'Sankhya', idade: 30 }, cidade: 'Uberlandia' }; const obj2 = { pessoa: { nome: 'Sankhya', idade: 30 }, cidade: 'Uberlandia' }; expect(ObjectUtils.equals(obj1, obj2)).toBe(true); }); it('should return false for different nested objects', () => { const obj1 = { pessoa: { nome: 'Sankhya', idade: 30 }, cidade: 'Uberlandia' }; const obj2 = { pessoa: { nome: 'Tech', idade: 25 }, cidade: 'Uberlandia' }; expect(ObjectUtils.equals(obj1, obj2)).toBe(false); }); it('should return false for objects with different types of properties', () => { const obj1 = { nome: 'Sankhya', ativo: true, idade: 30 }; const obj2 = { nome: 'Sankhya', ativo: 'true', idade: 30 }; expect(ObjectUtils.equals(obj1, obj2)).toBe(false); }); it('should return true for identical arrays', () => { const arr1 = [1, 2, 3]; const arr2 = [1, 2, 3]; expect(ObjectUtils.equals(arr1, arr2)).toBe(true); }); it('should return false for different arrays', () => { const arr1 = [1, 2, 3]; const arr2 = [3, 2, 1]; expect(ObjectUtils.equals(arr1, arr2)).toBe(false); }); it('should return true for empty objects', () => { const obj1 = {}; const obj2 = {}; expect(ObjectUtils.equals(obj1, obj2)).toBe(true); }); it('should return false when comparing different types (object vs array)', () => { const obj1 = { nome: 'Sankhya' }; const arr2 = ['Sankhya']; expect(ObjectUtils.equals(obj1, arr2)).toBe(false); }); it('should return true for objects with null and undefined treated as different', () => { const obj1 = { nome: null }; const obj2 = { nome: undefined }; expect(ObjectUtils.equals(obj1, obj2)).toBe(false); }); }); describe('isEmptySafetyCheck', () => { it('should return true for an empty object', () => { const obj = {}; expect(ObjectUtils.isEmptySafetyCheck(obj)).toBe(true); }); it('should return false for an object with properties', () => { const obj = { nome: 'Sankhya' }; expect(ObjectUtils.isEmptySafetyCheck(obj)).toBe(false); }); it('should return false for an object with nested properties', () => { const obj = { pessoa: { nome: 'Sankhya' } }; expect(ObjectUtils.isEmptySafetyCheck(obj)).toBe(false); }); it('should return false for an empty array', () => { const arr: any[] = []; expect(ObjectUtils.isEmptySafetyCheck(arr)).toBe(false); }); it('should return false for an object with null or undefined properties', () => { const obj = { nome: null, idade: undefined }; expect(ObjectUtils.isEmptySafetyCheck(obj)).toBe(false); }); it('should return false for a non-empty array object', () => { const obj = [1, 2, 3]; expect(ObjectUtils.isEmptySafetyCheck(obj)).toBe(false); }); it('should handle null gracefully and return true', () => { const obj = null; expect(ObjectUtils.isEmptySafetyCheck(obj as any)).toBe(true); }); it('should handle undefined gracefully and return true', () => { const obj = undefined; expect(ObjectUtils.isEmptySafetyCheck(obj as any)).toBe(true); }); it('should return false for primitive values (numbers, strings)', () => { expect(ObjectUtils.isEmptySafetyCheck('' as any)).toBe(false); expect(ObjectUtils.isEmptySafetyCheck(0 as any)).toBe(false); expect(ObjectUtils.isEmptySafetyCheck(42 as any)).toBe(false); }); it('should return false for functions', () => { const func = () => { }; expect(ObjectUtils.isEmptySafetyCheck(func as any)).toBe(false); }); }); describe('isNotEmptySafetyCheck', () => { it('should return false for an empty object', () => { const obj = {}; expect(ObjectUtils.isNotEmptySafetyCheck(obj)).toBe(false); }); it('should return true for an object with properties', () => { const obj = { nome: 'Sankhya' }; expect(ObjectUtils.isNotEmptySafetyCheck(obj)).toBe(true); }); it('should return true for an object with nested properties', () => { const obj = { pessoa: { nome: 'Sankhya' } }; expect(ObjectUtils.isNotEmptySafetyCheck(obj)).toBe(true); }); it('should return true for an empty array', () => { const arr: any[] = []; expect(ObjectUtils.isNotEmptySafetyCheck(arr)).toBe(true); }); it('should return true for an object with null or undefined properties', () => { const obj = { nome: null, idade: undefined }; expect(ObjectUtils.isNotEmptySafetyCheck(obj)).toBe(true); }); it('should return true for a non-empty array object', () => { const obj = [1, 2, 3]; expect(ObjectUtils.isNotEmptySafetyCheck(obj)).toBe(true); }); it('should handle null gracefully and return false', () => { const obj = null; expect(ObjectUtils.isNotEmptySafetyCheck(obj as any)).toBe(false); }); it('should handle undefined gracefully and return false', () => { const obj = undefined; expect(ObjectUtils.isNotEmptySafetyCheck(obj as any)).toBe(false); }); it('should return false for primitive values (numbers, strings)', () => { expect(ObjectUtils.isNotEmptySafetyCheck('' as any)).toBe(true); expect(ObjectUtils.isNotEmptySafetyCheck(0 as any)).toBe(true); expect(ObjectUtils.isNotEmptySafetyCheck(42 as any)).toBe(true); }); it('should return true for functions', () => { const func = () => { }; expect(ObjectUtils.isNotEmptySafetyCheck(func as any)).toBe(true); }); }); }); const defaultObjectMock = { food: { fruits: { apple: 1, banana: 2 } } };