refactor(core): Extract duplicate utility functions and add unit tests (no-changelog) (#9814)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-06-20 12:09:23 +02:00
committed by GitHub
parent 283d1ca583
commit e4463c62b4
14 changed files with 207 additions and 199 deletions

View File

@@ -1,4 +1,12 @@
import { fuzzyCompare, getResolvables, keysToLowercase, wrapData } from '@utils/utilities';
import {
compareItems,
flattenKeys,
fuzzyCompare,
getResolvables,
keysToLowercase,
shuffleArray,
wrapData,
} from '@utils/utilities';
//most test cases for fuzzyCompare are done in Compare Datasets node tests
describe('Test fuzzyCompare', () => {
@@ -137,3 +145,110 @@ describe('Test getResolvables', () => {
]);
});
});
describe('shuffleArray', () => {
it('should shuffle array', () => {
const array = [1, 2, 3, 4, 5];
const toShuffle = [...array];
shuffleArray(toShuffle);
expect(toShuffle).not.toEqual(array);
expect(toShuffle).toHaveLength(array.length);
expect(toShuffle).toEqual(expect.arrayContaining(array));
});
});
describe('flattenKeys', () => {
const name = 'Lisa';
const city1 = 'Berlin';
const city2 = 'Schoenwald';
const withNestedObject = {
name,
address: { city: city1 },
};
const withNestedArrays = {
name,
addresses: [{ city: city1 }, { city: city2 }],
};
it('should handle empty object', () => {
const flattenedObj = flattenKeys({});
expect(flattenedObj).toEqual({});
});
it('should flatten object with nested object', () => {
const flattenedObj = flattenKeys(withNestedObject);
expect(flattenedObj).toEqual({
name,
'address.city': city1,
});
});
it('should handle object with nested arrays', () => {
const flattenedObj = flattenKeys(withNestedArrays);
expect(flattenedObj).toEqual({
name,
'addresses.0.city': city1,
'addresses.1.city': city2,
});
});
it('should flatten object with nested object and specified prefix', () => {
const flattenedObj = flattenKeys(withNestedObject, ['test']);
expect(flattenedObj).toEqual({
'test.name': name,
'test.address.city': city1,
});
});
it('should handle object with nested arrays and specified prefix', () => {
const flattenedObj = flattenKeys(withNestedArrays, ['test']);
expect(flattenedObj).toEqual({
'test.name': name,
'test.addresses.0.city': city1,
'test.addresses.1.city': city2,
});
});
});
describe('compareItems', () => {
it('should return true if all values of specified keys are equal', () => {
const obj1 = { json: { a: 1, b: 2, c: 3 } };
const obj2 = { json: { a: 1, b: 2, c: 3 } };
const keys = ['a', 'b', 'c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(true);
});
it('should return false if any values of specified keys are not equal', () => {
const obj1 = { json: { a: 1, b: 2, c: 3 } };
const obj2 = { json: { a: 1, b: 2, c: 4 } };
const keys = ['a', 'b', 'c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(false);
});
it('should return true if all values of specified keys are equal using dot notation', () => {
const obj1 = { json: { a: { b: { c: 1 } } } };
const obj2 = { json: { a: { b: { c: 1 } } } };
const keys = ['a.b.c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(true);
});
it('should return false if any values of specified keys are not equal using dot notation', () => {
const obj1 = { json: { a: { b: { c: 1 } } } };
const obj2 = { json: { a: { b: { c: 2 } } } };
const keys = ['a.b.c'];
const result = compareItems(obj1, obj2, keys);
expect(result).toBe(false);
});
it('should return true if all values of specified keys are equal using bracket notation', () => {
const obj1 = { json: { 'a.b': { 'c.d': 1 } } };
const obj2 = { json: { 'a.b': { 'c.d': 1 } } };
const keys = ['a.b.c.d'];
const result = compareItems(obj1, obj2, keys, true);
expect(result).toBe(true);
});
});