Skip to content

Commit

Permalink
feat: improve test and isqual function
Browse files Browse the repository at this point in the history
  • Loading branch information
Puppo committed Aug 17, 2023
1 parent b4f5115 commit a609bd2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions packages/graphql-hooks/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ export function isEqualFirstLevel<T extends Record<string, any>>(
return false
}

for (const key of keys1) {
if (!keys2.includes(key) || obj1[key] !== obj2[key]) {
return false
}
for (let i = 0; i < keys1.length; i++) {
const key = keys2[i]
if (!(key in obj2) || !Object.is(obj1[key], obj2[key])) return false
}

return true
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql-hooks/test-jsdom/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('GraphQLClient utils', () => {
expect(isEqualFirstLevel({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true)
})

it('returns true for two objects with the same keys and values but different keys order', () => {
expect(isEqualFirstLevel({ a: 1, b: 2 }, { b: 2, a: 1 })).toBe(true)
})

it('returns true with the same object', () => {
const obj = { a: 1, b: 2 }
expect(isEqualFirstLevel(obj, obj)).toBe(true)
Expand Down

0 comments on commit a609bd2

Please sign in to comment.