From ba53693ee246c6817b60b29c7618b2e42150e218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Szentgr=C3=B3ti?= Date: Fri, 21 Jun 2024 12:09:54 +0200 Subject: [PATCH] feat: add tests --- test/object-diff.test.ts | 82 ++++++++++++++++++++++++++++++++++++++++ test/utils.test.ts | 2 + 2 files changed, 84 insertions(+) diff --git a/test/object-diff.test.ts b/test/object-diff.test.ts index bb9d831..96758ae 100644 --- a/test/object-diff.test.ts +++ b/test/object-diff.test.ts @@ -858,4 +858,86 @@ describe("getObjectDiff", () => { ], }); }); + it("detects changes when comparing an array value property to a non-array value property", () => { + expect( + getObjectDiff( + { + name: "joe", + age: 55, + hobbies: ["golf", "football"] + }, + { + name: "joe", + age: 55, + hobbies: null + })) + .toStrictEqual({ + type: "object", + status: "updated", + diff: [ + { + currentValue: "joe", + previousValue: "joe", + property: "name", + status: "equal", + }, + { + currentValue: 55, + previousValue: 55, + property: "age", + status: "equal", + }, + { + currentValue: null, + previousValue: [ + "golf", + "football", + ], + property: "hobbies", + status: "updated", + } + ] + }) + }) + it("detects changes when comparing a non-array value property to an array value property", () => { + expect( + getObjectDiff( + { + name: "joe", + age: 55, + hobbies: null + }, + { + name: "joe", + age: 55, + hobbies: ["golf", "football"] + })) + .toStrictEqual({ + type: "object", + status: "updated", + diff: [ + { + currentValue: "joe", + previousValue: "joe", + property: "name", + status: "equal", + }, + { + currentValue: 55, + previousValue: 55, + property: "age", + status: "equal", + }, + { + currentValue: [ + "golf", + "football", + ], + previousValue: null, + property: "hobbies", + status: "updated", + } + ] + }) + }); }); diff --git a/test/utils.test.ts b/test/utils.test.ts index 4db4c47..c0c145d 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -39,6 +39,8 @@ describe("isEqual", () => { ) ).toBeFalsy(); expect(isEqual(["psg"], ["psg", "nantes"])).toBeFalsy(); + expect(isEqual(null, ["hello", "world"])).toBeFalsy(); + expect(isEqual(["hello", "world"], null)).toBeFalsy(); }); it("return true if ignoreArrayOrder option is activated and arrays contains the same values regardless of their positions", () => { expect(