nope/lib/helpers/objectMethods.spec.ts
2021-12-04 08:25:26 +01:00

80 lines
2.1 KiB
TypeScript

/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @create date 2021-11-13 08:17:19
* @modify date 2021-11-13 09:44:51
* @desc [description]
*/
import { assert } from "chai";
import "chai/register-should";
import { describe, it } from "mocha";
import { deepClone, flattenObject } from "./objectMethods";
describe("objectMethods", function () {
// Describe the required Test:
describe("deepClone", function () {
it("clone", function () {
const data = { deep: { nested: "test" } };
const clone = deepClone(data);
assert.notStrictEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
clone.deep.nested = "changed";
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
});
describe("flattenObject", function () {
it("convert", function () {
const data = { deep: { nested: "test" } };
const result = flattenObject(data);
assert.isTrue(result.has("deep/nested"), "Key is missing");
result.get("deep/nested").should.equal("test");
});
it("limit the depth to 1", function () {
const data = { deep: { nested: "test" } };
let result = flattenObject(data, {
maxDepth: 1,
onlyPathToSimpleValue: false,
});
assert.isTrue(result.has("deep"), "Key is missing");
assert.isFalse(
result.has("deep/nested"),
"Key is present, which should not be the case"
);
assert.deepEqual(
result.get("deep"),
{ nested: "test" },
"Object are not matching"
);
result = flattenObject(data, {
maxDepth: 1,
onlyPathToSimpleValue: true,
});
result.size.should.be.equal(0);
});
it("adding a prefix", function () {
const data = { deep: { nested: "test" } };
const result = flattenObject(data, {
prefix: "test",
});
assert.isTrue(result.has("test/deep/nested"), "Key is missing");
assert.deepEqual(
result.get("test/deep/nested"),
"test",
"Object are not matching"
);
});
});
});