nope/lib/helpers/objectMethods.spec.ts

80 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-11-14 22:16:07 +00:00
/**
* @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);
2021-12-04 07:25:26 +00:00
assert.notStrictEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
2021-11-14 22:16:07 +00:00
clone.deep.nested = "changed";
2021-12-04 07:25:26 +00:00
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
2021-11-14 22:16:07 +00:00
});
});
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,
2021-12-04 07:25:26 +00:00
onlyPathToSimpleValue: false,
2021-11-14 22:16:07 +00:00
});
assert.isTrue(result.has("deep"), "Key is missing");
2021-12-04 07:25:26 +00:00
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"
);
2021-11-14 22:16:07 +00:00
result = flattenObject(data, {
maxDepth: 1,
2021-12-04 07:25:26 +00:00
onlyPathToSimpleValue: true,
2021-11-14 22:16:07 +00:00
});
result.size.should.be.equal(0);
});
it("adding a prefix", function () {
const data = { deep: { nested: "test" } };
const result = flattenObject(data, {
2021-12-04 07:25:26 +00:00
prefix: "test",
2021-11-14 22:16:07 +00:00
});
assert.isTrue(result.has("test/deep/nested"), "Key is missing");
2021-12-04 07:25:26 +00:00
assert.deepEqual(
result.get("test/deep/nested"),
"test",
"Object are not matching"
);
2021-11-14 22:16:07 +00:00
});
});
2021-12-04 07:25:26 +00:00
});