nope/lib/helpers/mergedData.spec.ts

286 lines
7.5 KiB
TypeScript
Raw 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";
import { MapBasedMergeData, MergeData } from "./mergedData";
2021-11-25 07:43:02 +00:00
describe("mergedData", function () {
// Describe the required Test:
it("data subscription", function (done) {
const m = new Map<string, string>();
const d = new MergeData(m, (m) => extractUniqueValues(m));
m.set("a", "b");
m.set("b", "b");
d.update();
d.data.subscribe((result) => {
assert.isTrue(
result.length === 1,
"Exactly one element different data has been added"
2021-11-25 07:43:02 +00:00
);
assert.isTrue([...result][0] === "b", "Element is element");
done();
});
});
it("data subscription. Update called twice", function (done) {
const m = new Map<string, string>();
const d = new MergeData(m, (m) => extractUniqueValues(m));
m.set("a", "b");
m.set("b", "b");
d.update(m);
d.data.subscribe((result) => {
assert.isTrue(
result.length === 1,
"Exactly one element different data has been added"
2021-11-25 07:43:02 +00:00
);
assert.isTrue([...result][0] === "b", "Element is element");
done();
});
d.update(m);
});
it("onchange subscription: added", function (done) {
const m = new Map<string, string>();
const d = new MergeData(m, (m) => extractUniqueValues(m));
m.set("a", "b");
m.set("b", "b");
d.onChange.subscribe((result) => {
assert.isTrue(
result.added.length === 1,
"Exactly one element has been added"
2021-11-25 07:43:02 +00:00
);
assert.isTrue([...result.added][0] === "b", "Element is element");
done();
});
d.update(m);
});
it("onchange subscription: removed", function (done) {
const m = new Map<string, string>();
const d = new MergeData(m, (m) => extractUniqueValues(m));
m.set("a", "b");
m.set("b", "b");
d.onChange.subscribe((result) => {
assert.isTrue(
result.removed.length === 0,
"No element has been removed!"
);
done();
});
d.update(m);
});
});
describe("MapBasedMergeData", function () {
// Describe the required Test:
it("data handeling - flat data", function (done) {
const m = new Map<string, string>();
const d = new MapBasedMergeData(m);
m.set("a", "b");
m.set("b", "b");
d.update();
2022-07-07 07:14:22 +00:00
expect([...d.keyMappingReverse.keys()]).contains("b");
done();
});
it("data handeling - flat data", function (done) {
const m = new Map<string, string[]>();
const d = new MapBasedMergeData(m);
m.set("a", ["a", "b"]);
m.set("b", ["c", "b"]);
d.update();
2022-07-07 07:14:22 +00:00
expect([...d.keyMappingReverse.keys()]).contains("b");
done();
});
// Describe the required Test:
it("data subscription - simple data", function (done) {
const m = new Map<string, string>();
const d = new MapBasedMergeData(m);
m.set("a", "b");
m.set("b", "b");
d.update();
d.data.subscribe((result) => {
assert.isTrue(
result.length === 1,
"Exactly one element different data has been added"
);
assert.isTrue([...result][0] === "b", "Element is element");
done();
});
});
it("data subscription - array data", function (done) {
const m = new Map<string, string[]>();
const d = new MapBasedMergeData(m);
2022-01-25 19:44:42 +00:00
m.set("a", ["a", "b"]);
m.set("b", ["c", "b"]);
d.update();
d.data.subscribe((result) => {
assert.isTrue(
result.length === 3,
"The Element contains 3 different items."
);
expect([...result]).to.contain("a");
expect([...result]).to.contain("b");
expect([...result]).to.contain("c");
done();
});
});
2022-07-07 07:14:22 +00:00
it("data subscription - nested data", function (done) {
const m = new Map<string, { key: string; data: string }>();
const d_1 = new MapBasedMergeData<
string,
{ key: string; data: string },
string,
{ key: string; data: string }
>(m, "", "key");
m.set("a", { key: "keyA", data: "dataA" });
m.set("b", { key: "keyB", data: "dataB" });
d_1.update();
assert.isTrue(
d_1.amountOf.size === 2,
"The Element contains 3 different items."
);
expect([...d_1.keyMapping.keys()]).to.contain("a");
expect([...d_1.keyMappingReverse.keys()]).to.contain("keyA");
expect([...d_1.simplified.keys()]).to.contain("keyA");
expect([...d_1.keyMappingReverse.values()]).to.contain("keyA");
const d_2 = new MapBasedMergeData<
string,
{ key: string; data: string },
string,
string
>(m, "data", "key");
d_2.update();
expect([...d_1.keyMapping.keys()]).to.contain("a");
expect([...d_1.keyMappingReverse.keys()]).to.contain("keyA");
expect([...d_1.simplified.keys()]).to.contain("keyA");
expect([...d_1.keyMappingReverse.keys()]).to.contain("dataB");
done();
});
// it("data subscription - nested data array", function (done) {
// const m = new Map<string, { key: string; data: string }[]>();
// const d_1 = new MapBasedMergeData<
// string,
// { key: string; data: string }[],
// string,
// { key: string; data: string }
// >(m, "", "key");
// m.set("a", [
// { key: "keyA", data: "dataA" },
// { key: "keyB", data: "dataB" },
// ]);
// m.set("b", [{ key: "keyC", data: "dataC" }]);
// d_1.update();
// assert.isTrue(
// d_1.amountOf.size === 2,
// "The Element contains 3 different items."
// );
// expect([...d_1.keyMapping.keys()]).to.contain("a");
// expect([...d_1.keyMappingReverse.keys()]).to.contain("keyA");
// expect([...d_1.simplified.keys()]).to.contain("keyA");
// expect([...d_1.keyMappingReverse.values()]).to.contain("keyA");
// const d_2 = new MapBasedMergeData<
// string,
// { key: string; data: string },
// string,
// string
// >(m, "data", "key");
// d_2.update();
// expect([...d_1.keyMapping.keys()]).to.contain("a");
// expect([...d_1.keyMappingReverse.keys()]).to.contain("keyA");
// expect([...d_1.simplified.keys()]).to.contain("keyA");
// expect([...d_1.keyMappingReverse.keys()]).to.contain("dataB");
// done();
// });
it("data subscription. Update called twice", function (done) {
const m = new Map<string, string>();
const d = new MapBasedMergeData(m);
m.set("a", "b");
m.set("b", "b");
d.update(m);
d.data.subscribe((result) => {
assert.isTrue(
result.length === 1,
"Exactly one element different data has been added"
);
assert.isTrue([...result][0] === "b", "Element is element");
done();
});
d.update(m);
});
it("onchange subscription: added", function (done) {
const m = new Map<string, string>();
const d = new MapBasedMergeData(m);
m.set("a", "b");
m.set("b", "b");
d.onChange.subscribe((result) => {
assert.isTrue(
result.added.length === 1,
"Exactly one element has been added"
);
assert.isTrue([...result.added][0] === "b", "Element is element");
done();
});
d.update(m);
});
it("onchange subscription: removed", function (done) {
const m = new Map<string, string>();
const d = new MapBasedMergeData(m);
m.set("a", "b");
m.set("b", "b");
d.onChange.subscribe((result) => {
assert.isTrue(
result.removed.length === 0,
"No element has been removed!"
2021-11-25 07:43:02 +00:00
);
done();
});
d.update(m);
});
});