Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/npm_and_yarn/@babel/preset-env-…
Browse files Browse the repository at this point in the history
…7.4.2
  • Loading branch information
nimjetushar authored Apr 17, 2019
2 parents 8047269 + a3891fa commit 423b5a1
Show file tree
Hide file tree
Showing 13 changed files with 2,487 additions and 18 deletions.
26 changes: 26 additions & 0 deletions __test__/deepCopy.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { deepCopy } from "../core/deepClone";
import { isEqual } from "../core/isEqual";

describe("deepCopy", () => {
it("should verify variable to be deep copy", () => {
Expand All @@ -10,4 +11,29 @@ describe("deepCopy", () => {
const isChanged = data.id !== clonedObj.id;
expect(isChanged).toBeTruthy();
});

it("should copy complex variable", () => {
const data = { id: 123, name: ["a", "v", "d"], obj: { a: 299, list: [1, 2, 3, 4, 34, 35, 75] } };
const clonedObj = deepCopy({}, data);
expect(data).toEqual(clonedObj);

clonedObj.id = 100;
clonedObj.name.push("t");
let isChanged = data.id !== clonedObj.id;
expect(isChanged).toBeTruthy();

isChanged = !isEqual(clonedObj.name, data.name);
expect(isChanged).toBeTruthy();
});

it("should copy object proto", () => {
function Demo() { }
Demo.prototype.getValue = function () { return 100; };

const obj = new Demo();
const clonedObj = deepCopy({}, obj);

expect(clonedObj.getValue).toBeDefined();
expect(clonedObj.getValue()).toBe(100);
});
});
140 changes: 140 additions & 0 deletions __test__/isEqual.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { isEqual } from "../core/isEqual";

describe("isEqual", () => {
it("should compare number", () => {
expect(isEqual(1, 1)).toBeTruthy();

expect(isEqual(1.234, 1.234)).toBeTruthy();

expect(isEqual(NaN, NaN)).toBeTruthy();

let a = 32.22, b = 32.22;
expect(isEqual(a, b)).toBeTruthy();

expect(isEqual(2, 6)).toBeFalsy();

expect(isEqual(21.342, 1.234)).toBeFalsy();

a = 32.42342;
b = 3232;
expect(isEqual(a, b)).toBeFalsy();

a = Number(20);
b = Number(20);
expect(isEqual(a, b)).toBeTruthy();

b = 20;
expect(isEqual(a, b)).toBeTruthy();
});

it("should compare string", () => {
expect(isEqual("hello", "hello")).toBeTruthy();

let a = "new string", b = "new string";
expect(isEqual(a, b)).toBeTruthy();

expect(isEqual(" test ", "test")).toBeFalsy();

a = " update";
b = " update";
expect(isEqual(a, b)).toBeTruthy();

a = String("hello");
b = String("hello");
expect(isEqual(a, b)).toBeTruthy();

a = String("hello");
b = String("hello1");
expect(isEqual(a, b)).toBeFalsy();

b = "hello";
expect(isEqual(a, b)).toBeTruthy();
});

it("should compare Object", () => {
expect(isEqual({}, {})).toBeTruthy();

let a = {}, b = {};
expect(isEqual(a, b)).toBeTruthy();

a = { a: 1 };
b = { a: 1 };
expect(isEqual(a, b)).toBeTruthy();

a = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] }, str: "hello" };
b = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] }, str: "hello" };
expect(isEqual(a, b)).toBeTruthy();

a = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] }, str: "hello " };
b = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] }, str: "hello" };
expect(isEqual(a, b)).toBeFalsy();

a = b;
b.a.push(3);
expect(isEqual(a, b)).toBeTruthy();

a = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } };
b = { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] }, test: 22 };
expect(isEqual(a, b)).toBeFalsy();

expect(isEqual(null, null)).toBeTruthy();

expect(isEqual(undefined, undefined)).toBeTruthy();

expect(isEqual(undefined, null)).toBeFalsy();

expect(isEqual({ a: null }, { a: null })).toBeTruthy();

a = new Object({ a: 1 });
b = new Object({ a: 1 });
expect(isEqual(a, b)).toBeTruthy();
});

it("should compare Array", () => {
let a = [], b = [];
expect(isEqual(a, b)).toBeTruthy();

a = [1, null, undefined, NaN, {}, { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } }, 0];
b = [1, null, undefined, NaN, {}, { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } }, 0];
expect(isEqual(a, b)).toBeTruthy();

a = [1, null, NaN, undefined, {}, { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } }, 0];
b = [1, null, undefined, NaN, {}, { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } }, 0];
expect(isEqual(a, b)).toBeFalsy();

a = [1, null, undefined, NaN, {}, { a: [1, 2, 3, 4, {}], b: { c: [{ a: 1 }] } }, 0];
b = [1, null, undefined, NaN, {}, { a: [1, 2, 3, 4], b: { c: [{ a: 1 }] } }, 0];
expect(isEqual(a, b)).toBeFalsy();

a = new Array(12);
b = new Array(12);
expect(isEqual(a, b)).toBeTruthy();
});

it("should compare other data types", () => {
expect(isEqual(1, "1")).toBeFalsy();

expect(isEqual(true, "true")).toBeFalsy();

let a = function () { };
function test() { }
expect(isEqual(a, test)).toBeFalsy();

let b = function () { };
expect(isEqual(a, b)).toBeFalsy();

a = function () { return true; };
b = function () { return true; };
expect(isEqual(a, b)).toBeFalsy();

class Class1 {
constructor(param) { this.param = param; }
}

class Class2 {
constructor(param) { this.param = param; }
}

expect(new Class1(1)).toEqual(new Class2(1));
});
});
7 changes: 6 additions & 1 deletion __test__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
isEmptyObject,
isObject,
setDataToLocal,
setDataToSession
setDataToSession,
isEqual
} from "../utils";

describe("Utils", () => {
Expand Down Expand Up @@ -46,4 +47,8 @@ describe("Utils", () => {
it("should have setDataToSession method", () => {
expect(setDataToSession).toBeDefined();
});

it("should have isEqual method", () => {
expect(isEqual).toBeDefined();
});
});
Loading

0 comments on commit 423b5a1

Please sign in to comment.