Skip to content

Commit

Permalink
test: enhance test suite for array distinction
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 19, 2023
1 parent d11ebdd commit 99aa15c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/utils/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,13 @@ export function isEqual(x: any, y: any): boolean {

const keysX = Reflect.ownKeys(x) as string[];
const keysY = Reflect.ownKeys(y) as string[];
if (keysX.length !== keysY.length) return false;
if (keysX.length !== keysY.length) {
return false;
}

for (let i = 0; i < keysX.length; i++) {
const key = keysX[i];
if (!Reflect.has(y, key)) {
return false;
}

if (!isEqual(x[key], y[key])) {
if (!Reflect.has(y, key) || !isEqual(x[key], y[key])) {
return false;
}
}
Expand Down
13 changes: 13 additions & 0 deletions test/unit/utils/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ describe('src/utils/array', function () {

expect(distinctArray(arr)).toEqual([{ foo: 'bar' }]);

arr = [{foo: 'bar'}, {foo: 'baz'}]
expect(distinctArray(arr)).toEqual(arr);

let circ : any = {foo: 'bar'};
circ.bar = circ;

arr = [{foo: { bar: 'baz'}}, circ];
expect(distinctArray(arr)).toEqual(arr);

let now = Date.now();
let firstDate = new Date(now);
let secondData = new Date(now);

expect(distinctArray([firstDate, secondData])).toEqual([firstDate]);

let firstRegex = new RegExp(/foo/);
let secondRegex = new RegExp(/foo/);
expect(distinctArray([firstRegex, secondRegex])).toEqual([firstRegex]);
})

it('should merge arrays', () => {
Expand Down

0 comments on commit 99aa15c

Please sign in to comment.