Browse Source

feat: add utils tests

pull/1/head
DoneDeal0 2 years ago
parent
commit
f3b9fe3adb
  1. 6
      src/object-diff.ts
  2. 8
      src/utils.ts
  3. 65
      test/utils.test.ts

6
src/object-diff.ts

@ -5,7 +5,7 @@ import { @@ -5,7 +5,7 @@ import {
STATUS,
Subproperties,
} from "./model";
import { hasNestedValues, isEqual } from "./utils";
import { isObject, isEqual } from "./utils";
function getObjectStatus(diff: ObjectDiff["diff"]): DiffStatus {
return diff.some((property) => property.status !== STATUS.EQUAL)
@ -26,7 +26,7 @@ function formatSingleObjectDiff( @@ -26,7 +26,7 @@ function formatSingleObjectDiff(
}
const diff: ObjectDiff["diff"] = [];
Object.entries(data).forEach(([property, value]) => {
if (hasNestedValues(value)) {
if (isObject(value)) {
const subPropertiesDiff: Subproperties[] = [];
Object.entries(value).forEach(([subProperty, subValue]) => {
subPropertiesDiff.push({
@ -79,7 +79,7 @@ export function getObjectDiff( @@ -79,7 +79,7 @@ export function getObjectDiff(
Object.entries(nextData).forEach(([nextProperty, nextValue]) => {
const previousValue = prevData[nextProperty];
if (hasNestedValues(nextValue)) {
if (isObject(nextValue)) {
const prevSubValues = previousValue
? Object.entries(previousValue)
: null;

8
src/utils.ts

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
export function isEqual(a: any, b: any): boolean {
if (typeof a !== typeof b) return true;
if (typeof a !== typeof b) return false;
if (Array.isArray(a)) {
return a.toString() === b.toString();
return a.every((v, i) => JSON.stringify(v) === JSON.stringify(b[i]));
}
if (typeof a === "object") {
return JSON.stringify(a) === JSON.stringify(b);
@ -9,6 +9,6 @@ export function isEqual(a: any, b: any): boolean { @@ -9,6 +9,6 @@ export function isEqual(a: any, b: any): boolean {
return a === b;
}
export function hasNestedValues(value: any): value is Record<string, any> {
return typeof value === "object" && !Array.isArray(value);
export function isObject(value: any): value is Record<string, any> {
return !!value && typeof value === "object" && !Array.isArray(value);
}

65
test/utils.test.ts

@ -1,10 +1,57 @@ @@ -1,10 +1,57 @@
// import { isEqual, hasNestedValues } from "../src/utils";
import { isEqual, isObject } from "../src/utils";
// describe("isEqual", () => {
// it("", () => {
// expect(isEqual(null, null)).toStrictEqual(null);
// });
// it("", () => {
// expect(hasNestedValues(null)).toStrictEqual(null);
// });
// });
describe("isEqual", () => {
it("return true if data are the same", () => {
expect(isEqual(null, null)).toBeTruthy();
expect(isEqual(undefined, undefined)).toBeTruthy();
expect(isEqual("hello", "hello")).toBeTruthy();
expect(isEqual(57, 57)).toBeTruthy();
expect(isEqual(["hello", "world"], ["hello", "world"])).toBeTruthy();
expect(
isEqual(
[
{ name: "joe", age: 99 },
{ name: "nina", age: 23 },
],
[
{ name: "joe", age: 99 },
{ name: "nina", age: 23 },
]
)
).toBeTruthy();
});
it("return false if data are different", () => {
expect(isEqual(null, "hello")).toBeFalsy();
expect(isEqual("hello", undefined)).toBeFalsy();
expect(isEqual("hello", "howdy")).toBeFalsy();
expect(isEqual(57, 51)).toBeFalsy();
expect(isEqual(["hello", "world"], ["howdy", "world"])).toBeFalsy();
expect(
isEqual(
[
{ name: "joe", age: 99 },
{ name: "nina", age: 23 },
],
[
{ name: "joe", age: 98 },
{ name: "nina", age: 23 },
]
)
).toBeFalsy();
});
});
describe("isObject", () => {
it("return true if the value has nested values", () => {
expect(isObject({ name: "joe" })).toBeTruthy();
expect(isObject({ user: { name: "joe" } })).toBeTruthy();
});
it("return false if the value doesn't have nested values", () => {
expect(isObject("joe")).toBeFalsy();
expect(isObject(56)).toBeFalsy();
expect(isObject(true)).toBeFalsy();
expect(isObject(null)).toBeFalsy();
expect(isObject(undefined)).toBeFalsy();
expect(isObject(["hello", "world"])).toBeFalsy();
});
});

Loading…
Cancel
Save