nope/lib/helpers/objectMethods.spec.ts

401 lines
10 KiB
TypeScript
Raw Permalink Normal View History

2021-11-14 22:16:07 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
2022-01-10 06:52:05 +00:00
import { assert, expect } from "chai";
2021-11-14 22:16:07 +00:00
import { describe, it } from "mocha";
2022-07-07 07:14:22 +00:00
import {
convertData,
deepClone,
flattenObject,
rgetattr,
rqueryAttr,
} from "./objectMethods";
2021-11-14 22:16:07 +00:00
describe("objectMethods", function () {
// Describe the required Test:
describe("deepClone", function () {
it("clone number", function () {
const data = 1;
let clone = deepClone(data);
clone = 2;
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone string", function () {
const data = "test";
let clone = deepClone(data);
clone = "fail";
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone boolean", function () {
const data = true;
let clone = deepClone(data);
clone = false;
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone null", function () {
const data = null;
let clone: any = deepClone(data);
clone = false;
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone undefined", function () {
const data = undefined;
let clone: any = deepClone(data);
clone = false;
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone array", function () {
const data = [0, 1, 2];
const clone = deepClone(data);
assert.notStrictEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
clone.pop();
assert.notDeepEqual(
data,
clone,
"Elements have the same identity, but should be differend"
);
});
it("clone nested object", function () {
2021-11-14 22:16:07 +00:00
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");
2022-01-10 06:52:05 +00:00
expect(result.get("deep/nested")).to.equal("test");
2021-11-14 22:16:07 +00:00
});
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
});
2022-01-10 06:52:05 +00:00
expect(result.size).be.equal(0);
2021-11-14 22:16:07 +00:00
});
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
});
});
describe("rgettattr", function () {
it("pull empty data", function () {
let data = {};
let result = rgetattr(data, "test", "default");
assert.isTrue(
result === "default",
"we expected 'default' but got: " + result.toString()
);
result = rgetattr(data, "test", null);
assert.isTrue(result === null, "we expected 'null' but got: " + result);
});
it("pull data", function () {
let data = { deep: { nested: "test" } };
let result = rgetattr(data, "deep/nested", null);
assert.isTrue(result === "test", "we expected 'test' but got: " + result);
});
});
2022-07-07 07:14:22 +00:00
describe("rgettattrQuery", function () {
it("query empty data", function () {
let data = {};
let result = rqueryAttr(data, "test/+");
assert.isTrue(
result.length === 0,
"we expected 0 entries but got: " + result.length.toString()
);
});
it("query data - single", function () {
let data = { deep: { nested: "test" } };
let result = rqueryAttr(data, "deep/nested");
assert.isTrue(
result.length === 1,
"we expected 1 entries but got: " + result.length.toString()
);
assert.isTrue(
result[0].path === "deep/nested",
"we expected the path to be 'deep/nested', but got" + result[0].path
);
assert.isTrue(
result[0].data === "test",
"we expected the data to be 'test', but got" + result[0].data
);
});
it("query data - singlelevel-wildcard", function () {
let data = {
deep: { nested_01: { nested_02: "test_01" }, nested_03: "test_02" },
not: { nested: "hello" },
};
let result = rqueryAttr(data, "deep/+");
assert.isTrue(
result.length === 2,
"we expected 2 entries but got: " + result.length.toString()
);
const pathes = result.map((item) => item.path);
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()
);
});
it("query data-array - singlelevel-wildcard", function () {
let data = {
array: [
{
data: 0,
},
{
data: 1,
},
],
not: { nested: "hello" },
};
let result = rqueryAttr(data, "array/+/data");
assert.isTrue(
result.length === 2,
"we expected 2 entries but got: " + result.length.toString()
);
const items = result.map((item) => item.data);
assert.isTrue(
items.includes(1) && items.includes(0),
`we expected the data contains "0" and "1", but got` + items.toString()
);
});
it("query data - multilevel-wildcard", function () {
let data = {
deep: { nested_01: { nested_02: "test_01" }, nested_03: "test_02" },
not: { nested: "hello" },
};
let result = rqueryAttr(data, "deep/#");
const pathes = result.map((item) => item.path);
assert.isTrue(
result.length === 3,
"we expected 3 entries but got: " + pathes.toString()
);
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()
);
});
});
describe("convertData", function () {
it("query empty data", function () {
let data = {};
let result = convertData(data, [
{
key: "result",
query: "a/b",
},
]);
assert.isTrue(
result.length === 0,
"we expected 0 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result)
);
});
it("query data - single", function () {
let data = { deep: { nested: "test" } };
let result = convertData<{ result: string }>(data, [
{
key: "result",
query: "deep/nested",
},
]);
assert.isTrue(
result.length === 1,
"we expected 1 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result)
);
assert.isTrue(
result[0].result === "test",
"we expected the path to be 'test', but got" +
result[0] +
"\n" +
JSON.stringify(result)
);
});
it("query data - singlelevel-wildcard", function () {
let data = { deep: { nested: "test" } };
let result = convertData<{ result: string }>(data, [
{
key: "result",
query: "deep/+",
},
]);
assert.isTrue(
result.length === 1,
"we expected 1 entries but got: " +
result.length.toString() +
"\n" +
JSON.stringify(result)
);
assert.isTrue(
result[0].result === "test",
"we expected the path to be 'test', but got" +
result[0] +
"\n" +
JSON.stringify(result)
);
});
it("query data-array - singlelevel-wildcard", function () {
let data = {
array: [
{
data1: 0,
data2: "a",
},
{
data1: 1,
data2: "a",
},
],
not: { nested: "hello" },
};
let result = convertData<{ a: number; b: string }>(data, [
{
key: "a",
query: "array/+/data1",
},
{
key: "b",
query: "array/+/data2",
},
]);
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);
assert.isTrue(
items.includes(1) && items.includes(0),
`we expected the data contains "0" and "1", but got` +
items.toString() +
"\n" +
JSON.stringify(result)
);
});
it("query data-array - test exception", function () {
let data = {
array: [
{
data1: 0,
data2: "a",
},
{
data1: 1,
data2: "a",
},
],
not: { nested: "hello" },
};
try {
let result = convertData<{ a: number; b: string }>(data, [
{
key: "a",
query: "array/+/data1",
},
{
key: "b",
query: "array/+/data2",
},
]);
assert.isTrue(false, "Failed to raise exception");
} catch (e) {}
});
});
2021-12-04 07:25:26 +00:00
});