Skip to content

Commit

Permalink
CB-4010 add to isArraysEqual edge cases + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
s.teleshev committed Jan 10, 2024
1 parent be9dfac commit 0a5d4b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 14 additions & 4 deletions webapp/packages/core-utils/src/isArraysEqual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,21 @@ describe('Is array equals', () => {
expect(isArraysEqual([{ a: 1 }], [{ a: 1 }], (a, b) => a.a === b.a, true)).toBe(true);
});

test('should pass with array of objects (length > 1)', () => {
expect(isArraysEqual([{ b: 3 }, { a: 1 }], [{ a: 1 }, { b: 3 }])).toBe(true);
test('should not pass with no equal fn and array of objects (length > 1)', () => {
expect(isArraysEqual([{ b: 3 }, { a: 1 }], [{ a: 1 }, { b: 3 }])).toBe(false);
});

test('should pass with primitive and non primitive in array', () => {
expect(isArraysEqual([1, 1, { a: 1 }, 2], [2, { a: 1 }, 1, 1])).toBe(true);
test('should not pass with no equal fn and primitive and non primitive in array', () => {
expect(isArraysEqual([1, 1, { a: 1 }, 2], [2, { a: 1 }, 1, 1])).toBe(false);
});

test('should pass with equal fn and array of objects (length > 1)', () => {
const isEqual = jest.fn((a: { a: number }, b: { a: number }) => a.a === b.a);
expect(isArraysEqual([{ a: 3 }, { a: 1 }], [{ a: 1 }, { a: 3 }], isEqual)).toBe(true);
});

test('should pass with equal fn and primitive and non primitive in array', () => {
const isEqual = jest.fn((a: { a: number }, b: { a: number }) => a.a === b.a);
expect(isArraysEqual([1, { a: 3 }, { a: 1 }] as any, [1, { a: 1 }, { a: 3 }] as any, isEqual)).toBe(true);
});
});
6 changes: 5 additions & 1 deletion webapp/packages/core-utils/src/isArraysEqual.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ export function isArraysEqual<T>(first: T[], second: T[], isEqual: (a: T, b: T)
map.set(currentSecond, Number(map.get(currentSecond)) - 1);
break;
}

if (j === first.length - 1) {
return false;
}
}
continue;
}

const mapValue = map.get(currentSecond);

if (mapValue === undefined || mapValue === 0) {
if (mapValue === undefined || mapValue <= 0) {
return false;
}

Expand Down

0 comments on commit 0a5d4b1

Please sign in to comment.