nope-js-node
Version:
NoPE Runtime for Nodejs. For Browser-Support please use nope-browser
272 lines (271 loc) • 12.9 kB
JavaScript
;
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const mocha_1 = require("mocha");
const objectMethods_1 = require("./objectMethods");
(0, mocha_1.describe)("objectMethods", function () {
// Describe the required Test:
(0, mocha_1.describe)("deepClone", function () {
(0, mocha_1.it)("clone number", function () {
const data = 1;
let clone = (0, objectMethods_1.deepClone)(data);
clone = 2;
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone string", function () {
const data = "test";
let clone = (0, objectMethods_1.deepClone)(data);
clone = "fail";
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone boolean", function () {
const data = true;
let clone = (0, objectMethods_1.deepClone)(data);
clone = false;
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone null", function () {
const data = null;
let clone = (0, objectMethods_1.deepClone)(data);
clone = false;
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone undefined", function () {
const data = undefined;
let clone = (0, objectMethods_1.deepClone)(data);
clone = false;
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone array", function () {
const data = [0, 1, 2];
const clone = (0, objectMethods_1.deepClone)(data);
chai_1.assert.notStrictEqual(data, clone, "Elements have the same identity, but should be differend");
clone.pop();
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
(0, mocha_1.it)("clone nested object", function () {
const data = { deep: { nested: "test" } };
const clone = (0, objectMethods_1.deepClone)(data);
chai_1.assert.notStrictEqual(data, clone, "Elements have the same identity, but should be differend");
clone.deep.nested = "changed";
chai_1.assert.notDeepEqual(data, clone, "Elements have the same identity, but should be differend");
});
});
(0, mocha_1.describe)("flattenObject", function () {
(0, mocha_1.it)("convert", function () {
const data = { deep: { nested: "test" } };
const result = (0, objectMethods_1.flattenObject)(data);
chai_1.assert.isTrue(result.has("deep/nested"), "Key is missing");
(0, chai_1.expect)(result.get("deep/nested")).to.equal("test");
});
(0, mocha_1.it)("limit the depth to 1", function () {
const data = { deep: { nested: "test" } };
let result = (0, objectMethods_1.flattenObject)(data, {
maxDepth: 1,
onlyPathToSimpleValue: false,
});
chai_1.assert.isTrue(result.has("deep"), "Key is missing");
chai_1.assert.isFalse(result.has("deep/nested"), "Key is present, which should not be the case");
chai_1.assert.deepEqual(result.get("deep"), { nested: "test" }, "Object are not matching");
result = (0, objectMethods_1.flattenObject)(data, {
maxDepth: 1,
onlyPathToSimpleValue: true,
});
(0, chai_1.expect)(result.size).be.equal(0);
});
(0, mocha_1.it)("adding a prefix", function () {
const data = { deep: { nested: "test" } };
const result = (0, objectMethods_1.flattenObject)(data, {
prefix: "test",
});
chai_1.assert.isTrue(result.has("test/deep/nested"), "Key is missing");
chai_1.assert.deepEqual(result.get("test/deep/nested"), "test", "Object are not matching");
});
});
(0, mocha_1.describe)("rgettattr", function () {
(0, mocha_1.it)("pull empty data", function () {
let data = {};
let result = (0, objectMethods_1.rgetattr)(data, "test", "default");
chai_1.assert.isTrue(result === "default", "we expected 'default' but got: " + result.toString());
result = (0, objectMethods_1.rgetattr)(data, "test", null);
chai_1.assert.isTrue(result === null, "we expected 'null' but got: " + result);
});
(0, mocha_1.it)("pull data", function () {
let data = { deep: { nested: "test" } };
let result = (0, objectMethods_1.rgetattr)(data, "deep/nested", null);
chai_1.assert.isTrue(result === "test", "we expected 'test' but got: " + result);
});
});
(0, mocha_1.describe)("rgettattrQuery", function () {
(0, mocha_1.it)("query empty data", function () {
let data = {};
let result = (0, objectMethods_1.rqueryAttr)(data, "test/+");
chai_1.assert.isTrue(result.length === 0, "we expected 0 entries but got: " + result.length.toString());
});
(0, mocha_1.it)("query data - single", function () {
let data = { deep: { nested: "test" } };
let result = (0, objectMethods_1.rqueryAttr)(data, "deep/nested");
chai_1.assert.isTrue(result.length === 1, "we expected 1 entries but got: " + result.length.toString());
chai_1.assert.isTrue(result[0].path === "deep/nested", "we expected the path to be 'deep/nested', but got" + result[0].path);
chai_1.assert.isTrue(result[0].data === "test", "we expected the data to be 'test', but got" + result[0].data);
});
(0, mocha_1.it)("query data - singlelevel-wildcard", function () {
let data = {
deep: { nested_01: { nested_02: "test_01" }, nested_03: "test_02" },
not: { nested: "hello" },
};
let result = (0, objectMethods_1.rqueryAttr)(data, "deep/+");
chai_1.assert.isTrue(result.length === 2, "we expected 2 entries but got: " + result.length.toString());
const pathes = result.map((item) => item.path);
chai_1.assert.isTrue(pathes.includes("deep/nested_01") && pathes.includes("deep/nested_03"), `we expected the "deep/nested_01" and "deep/nested_03" have been found, but got` +
pathes.toString());
});
(0, mocha_1.it)("query data-array - singlelevel-wildcard", function () {
let data = {
array: [
{
data: 0,
},
{
data: 1,
},
],
not: { nested: "hello" },
};
let result = (0, objectMethods_1.rqueryAttr)(data, "array/+/data");
chai_1.assert.isTrue(result.length === 2, "we expected 2 entries but got: " + result.length.toString());
const items = result.map((item) => item.data);
chai_1.assert.isTrue(items.includes(1) && items.includes(0), `we expected the data contains "0" and "1", but got` + items.toString());
});
(0, mocha_1.it)("query data - multilevel-wildcard", function () {
let data = {
deep: { nested_01: { nested_02: "test_01" }, nested_03: "test_02" },
not: { nested: "hello" },
};
let result = (0, objectMethods_1.rqueryAttr)(data, "deep/#");
const pathes = result.map((item) => item.path);
chai_1.assert.isTrue(result.length === 3, "we expected 3 entries but got: " + pathes.toString());
chai_1.assert.isTrue(pathes.includes("deep/nested_01") &&
pathes.includes("deep/nested_01/nested_02") &&
pathes.includes("deep/nested_03"), `we expected the "deep/nested_01" and "deep/nested_01/nested_02" and "deep/nested_03" have been found, but got` +
pathes.toString());
});
});
(0, mocha_1.describe)("convertData", function () {
(0, mocha_1.it)("query empty data", function () {
let data = {};
let result = (0, objectMethods_1.convertData)(data, [
{
key: "result",
query: "a/b",
},
]);
chai_1.assert.isTrue(result.length === 0, "we expected 0 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result));
});
(0, mocha_1.it)("query data - single", function () {
let data = { deep: { nested: "test" } };
let result = (0, objectMethods_1.convertData)(data, [
{
key: "result",
query: "deep/nested",
},
]);
chai_1.assert.isTrue(result.length === 1, "we expected 1 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result));
chai_1.assert.isTrue(result[0].result === "test", "we expected the path to be 'test', but got" +
result[0] +
"\n" +
JSON.stringify(result));
});
(0, mocha_1.it)("query data - singlelevel-wildcard", function () {
let data = { deep: { nested: "test" } };
let result = (0, objectMethods_1.convertData)(data, [
{
key: "result",
query: "deep/+",
},
]);
chai_1.assert.isTrue(result.length === 1, "we expected 1 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result));
chai_1.assert.isTrue(result[0].result === "test", "we expected the path to be 'test', but got" +
result[0] +
"\n" +
JSON.stringify(result));
});
(0, mocha_1.it)("query data-array - singlelevel-wildcard", function () {
let data = {
array: [
{
data1: 0,
data2: "a",
},
{
data1: 1,
data2: "a",
},
],
not: { nested: "hello" },
};
let result = (0, objectMethods_1.convertData)(data, [
{
key: "a",
query: "array/+/data1",
},
{
key: "b",
query: "array/+/data2",
},
]);
chai_1.assert.isTrue(result.length === 2, "we expected 2 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result));
const items = result.map((item) => item.a);
chai_1.assert.isTrue(items.includes(1) && items.includes(0), `we expected the data contains "0" and "1", but got` +
items.toString() +
"\n" +
JSON.stringify(result));
});
(0, mocha_1.it)("query data-array - test exception", function () {
let data = {
array: [
{
data1: 0,
data2: "a",
},
{
data1: 1,
data2: "a",
},
],
not: { nested: "hello" },
};
try {
let result = (0, objectMethods_1.convertData)(data, [
{
key: "a",
query: "array/+/data1",
},
{
key: "b",
query: "array/+/data2",
},
]);
chai_1.assert.isTrue(false, "Failed to raise exception");
}
catch (e) { }
});
});
});