From 729d5ed68fd7d10ee3a1e219ffb6f9e3633c965c Mon Sep 17 00:00:00 2001 From: gaeaehrlich Date: Sun, 13 Feb 2022 15:42:08 +0200 Subject: [PATCH] pref(#77) --- src/utility/object/object.test.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/utility/object/object.test.js b/src/utility/object/object.test.js index 54fbe78..bc8d362 100644 --- a/src/utility/object/object.test.js +++ b/src/utility/object/object.test.js @@ -1,4 +1,4 @@ -import {get, set, isEqual, omit, clone} from './object'; +import {get, set, isEqual, omit, clone, EqualityIterators} from './object'; describe('Object', () => { @@ -86,6 +86,32 @@ describe('Object', () => { expect(isEqual(parent1, parent2)).toEqual(false); }); + it('isEqual() with shallow comparison', () => { + expect(isEqual(1, 1, EqualityIterators.SHALLOW)).toEqual(true); + expect(isEqual(true, false, EqualityIterators.SHALLOW)).toEqual(false); + expect(isEqual('hello', 'hello', EqualityIterators.SHALLOW)).toEqual(true); + expect(isEqual([1, 2, 3], [1, 2, 3], EqualityIterators.SHALLOW)).toEqual(true); + expect(isEqual([1, 2, 3], [3, 2, 1], EqualityIterators.SHALLOW)).toEqual(false); + expect(isEqual([{id: 1}], [{id: 2}], EqualityIterators.SHALLOW)).toEqual(false); + expect(isEqual({foo: 'bar'}, {foo: 'bar'}, EqualityIterators.SHALLOW)).toEqual(true); + expect(isEqual({foo: 'bar'}, {bar: 'foo'}, EqualityIterators.SHALLOW)).toEqual(false); + expect(isEqual({foo: [1, 2, {a: 1}]}, {foo: [1, 2, {a: 1}]}, EqualityIterators.SHALLOW)).toEqual(false); + expect(isEqual({foo: [1, 2, {a: 1}]}, {foo: [1, 2, {a: 2}]}, EqualityIterators.SHALLOW)).toEqual(false); + + /* Check Circular Traversal */ + const sub1 = {foo: [1, 2, {a: 1}]}; + const sub2 = {foo: [1, 2, {a: 1}]}; + const parent1 = {foo: [1, 2, {a: 1}], sub1, sub2}; + const parent2 = {foo: [1, 2, {a: 1}], sub1, sub2}; + parent1.aaa = parent2; + parent1.bbb = parent1; + parent2.aaa = parent1; + parent2.bbb = parent2; + expect(isEqual(parent1, parent2, EqualityIterators.SHALLOW)).toEqual(false); + parent2.ccc = 6; + expect(isEqual(parent1, parent2, EqualityIterators.SHALLOW)).toEqual(false); + }); + it('clone()', () => { const source = {foo: 1, bar: 'hello', baz: {baq: 23, test: [1, 2, 3]}}; const target = clone(source);