nope/lib/helpers/mapMethods.spec.ts

127 lines
3.4 KiB
TypeScript
Raw Permalink Normal View History

2021-11-25 07:43:02 +00:00
/**
* @author Martin Karkowski
* @email m.karkowski@zema.de
* @desc [description]
*/
import { assert, expect } from "chai";
2021-11-25 07:43:02 +00:00
import { describe, it } from "mocha";
import { extractUniqueValues } from "./mapMethods";
describe("mapMethods", function () {
// Describe the required Test:
describe("extractUniqueValues", function () {
it("simple-map", function () {
const m = new Map<string, string>();
m.set("a", "b");
m.set("b", "b");
const result = extractUniqueValues(m);
assert.isTrue(
result.size === 1,
"Elements have the same identity, but should be differend"
);
assert.isTrue([...result][0] === "b", "Element is element");
});
it("nested-map", function () {
const m = new Map<string, { a: string }>();
m.set("a", { a: "b" });
m.set("b", { a: "b" });
const result = extractUniqueValues(m, "a");
assert.isTrue(
result.size === 1,
"Elements have the same identity, but should be differend"
);
assert.isTrue([...result][0] === "b", "Element is element");
});
it("nested-array", function () {
2022-08-16 14:45:30 +00:00
const m = new Map<string, { a?: string[]; b?: string[] }>();
2021-11-25 07:43:02 +00:00
m.set("a", { a: ["b"] });
m.set("b", { a: ["b"] });
2022-08-16 14:45:30 +00:00
m.set("b", { b: ["b"] });
m.set("b", { a: ["c"] });
const result = extractUniqueValues<string>(m, "a/+");
2021-11-25 07:43:02 +00:00
assert.isTrue(
2022-08-16 14:45:30 +00:00
result.size === 2,
2021-11-25 07:43:02 +00:00
"Elements have the same identity, but should be differend"
);
2022-08-16 14:45:30 +00:00
const r = [...result].sort();
assert.isTrue(r[0] === "b", "Element is element");
2021-11-25 07:43:02 +00:00
});
it("flat-array", function () {
const m = new Map<string, string[]>();
m.set("a", ["a"]);
2022-01-25 19:44:42 +00:00
m.set("b", ["a", "b"]);
2022-08-16 14:45:30 +00:00
const result = extractUniqueValues(m, "+");
2022-01-25 19:44:42 +00:00
expect(result.size).to.equal(2);
assert.isTrue(
result.size === 2,
"The Element should include 2 elements. namely 'a' and 'b'"
);
assert.isArray([...result], "Should be an array");
expect([...result]).to.contain("a");
expect([...result]).to.contain("b");
});
2021-11-25 07:43:02 +00:00
it("nested-array multiple elements", function () {
const m = new Map<string, { a: string[] }>();
m.set("a", { a: ["b"] });
m.set("b", { a: ["c", "d"] });
2022-08-16 14:45:30 +00:00
const result = extractUniqueValues(m, "a/+");
2021-11-25 07:43:02 +00:00
assert.isTrue(
result.size === 3,
"Elements have the same identity, but should be differend"
);
assert.deepEqual(["b", "c", "d"], [...result], "Items are missing");
});
2022-08-16 14:45:30 +00:00
it("nested-object, different key", function () {
const m = new Map<
string,
{
a: {
id: number;
content: string;
}[];
}
>();
m.set("a", {
a: [
{
content: "a",
id: 1,
},
{
content: "b",
id: 2,
},
],
});
m.set("b", {
a: [
{
content: "c",
id: 1,
},
{
content: "d",
id: 3,
},
],
});
const result = extractUniqueValues<{
id: number;
content: string;
}>(m, "a/+/content", "a/+/id");
assert.isTrue(
result.size === 3,
"Elements have the same identity, but should be differend"
);
assert.deepEqual(["a", "b", "d"], [...result], "Items are missing");
});
2021-11-25 07:43:02 +00:00
});
});