Skip to content

Commit

Permalink
add tests for objectsEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev committed Feb 21, 2021
1 parent 5b4d42a commit 6a22b59
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/datastore/__tests__/outbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('Outbox tests', () => {
let modelId: string;

beforeAll(async () => {
// jest.resetModules();
jest.resetAllMocks();

await initializeOutbox();
Expand Down
31 changes: 31 additions & 0 deletions packages/datastore/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
objectsEqual,
isAWSDate,
isAWSDateTime,
isAWSEmail,
Expand All @@ -11,6 +12,36 @@ import {
} from '../src/util';

describe('datastore util', () => {
test('objectsEqual', () => {
expect(objectsEqual({}, {})).toEqual(true);
expect(objectsEqual([], [])).toEqual(true);
expect(objectsEqual([], {})).toEqual(false);
expect(objectsEqual([1, 2, 3], [1, 2, 3])).toEqual(true);
expect(objectsEqual([1, 2, 3], [1, 2, 3, 4])).toEqual(false);
expect(objectsEqual({ a: 1 }, { a: 1 })).toEqual(true);
expect(objectsEqual({ a: 1 }, { a: 2 })).toEqual(false);
expect(
objectsEqual({ a: [{ b: 2 }, { c: 3 }] }, { a: [{ b: 2 }, { c: 3 }] })
).toEqual(true);
expect(
objectsEqual({ a: [{ b: 2 }, { c: 3 }] }, { a: [{ b: 2 }, { c: 4 }] })
).toEqual(false);
expect(objectsEqual(new Set([1, 2, 3]), new Set([1, 2, 3]))).toEqual(true);
expect(objectsEqual(new Set([1, 2, 3]), new Set([1, 2, 3, 4]))).toEqual(
false
);

const map1 = new Map();
map1.set('a', 1);

const map2 = new Map();
map2.set('a', 1);

expect(objectsEqual(map1, map2)).toEqual(true);
map2.set('b', 2);
expect(objectsEqual(map1, map2)).toEqual(false);
});

test('isAWSDate', () => {
const valid = [
'2020-01-01',
Expand Down
2 changes: 1 addition & 1 deletion packages/datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"es2015",
"dom",
"esnext.asynciterable",
"es2017.object"
"es2019.object"
],
"allowJs": true,
"esModuleInterop": true,
Expand Down
12 changes: 8 additions & 4 deletions packages/datastore/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,19 @@ export function objectsEqual(objA: object, objB: object): boolean {
let a = objA;
let b = objB;

if (Array.isArray(a) && !Array.isArray(b)) {
return false;
}

if (a instanceof Set && b instanceof Set) {
a = [...a];
b = [...b];
}

// if (a instanceof Map && b instanceof Map) {
// a = Object.fromEntries(a);
// b = Object.fromEntries(b);
// }
if (a instanceof Map && b instanceof Map) {
a = Object.fromEntries(a);
b = Object.fromEntries(b);
}

const aKeys = Object.keys(a);
const bKeys = Object.keys(b);
Expand Down

0 comments on commit 6a22b59

Please sign in to comment.