Update tests to write to sink

This commit is contained in:
Jerry Kurian 2019-08-29 08:50:11 -04:00
parent 685215bee6
commit 9765e6cb49

View File

@ -1,6 +1,7 @@
import test from "ava"; import test from "ava";
import { expect } from "chai"; import { expect } from "chai";
import { demux, map } from "../src"; import { demux, map } from "../src";
import { Writable } from "stream";
interface Test { interface Test {
key: string; key: string;
@ -10,45 +11,48 @@ test.cb("should spread per key", t => {
t.plan(5); t.plan(5);
const input = [ const input = [
{ key: "a", val: 1 }, { key: "a", val: 1 },
{ key: "a", val: 2 }, { key: "b", val: 2 },
{ key: "b", val: 3 }, { key: "a", val: 3 },
{ key: "c", val: 4 }, { key: "c", val: 4 },
]; ];
const results = [ const results = [
{ key: "a", val: 2 }, { key: "a", val: 2 },
{ key: "a", val: 3 }, { key: "b", val: 3 },
{ key: "b", val: 4 }, { key: "a", val: 4 },
{ key: "c", val: 5 }, { key: "c", val: 5 },
]; ];
const destKeys = []; const destinationStreamKeys = [];
const dests = [];
let i = 0; let i = 0;
const sink = new Writable({
objectMode: true,
write(chunk, enc, cb) {
i++;
expect(results).to.deep.include(chunk);
expect(input).to.not.deep.include(chunk);
t.pass();
cb();
if (i === 4) {
t.end();
}
},
});
const construct = (destKey: string) => { const construct = (destKey: string) => {
destKeys.push(destKey); destinationStreamKeys.push(destKey);
const dest = map((chunk: Test) => ({ const dest = map((chunk: Test) => {
...chunk, return {
val: chunk.val + 1, ...chunk,
})) val: chunk.val + 1,
.on("data", (d: Test) => { };
expect(results).to.deep.include(d); });
t.pass();
}) dest.pipe(sink);
.on("end", () => {
i++;
if (i === dests.length) {
t.end();
}
});
dests.push(dest);
return dest; return dest;
}; };
const demuxed = demux(construct, { key: "key" }, { objectMode: true }); const demuxed = demux(construct, { key: "key" }, { objectMode: true });
demuxed.on("finish", () => { demuxed.on("finish", () => {
expect(destKeys).to.deep.equal(["a", "b", "c"]); expect(destinationStreamKeys).to.deep.equal(["a", "b", "c"]);
t.pass(); t.pass();
dests.forEach(dest => dest.end());
}); });
input.forEach(event => demuxed.write(event)); input.forEach(event => demuxed.write(event));
@ -59,37 +63,41 @@ test.cb("should spread per key using keyBy", t => {
t.plan(5); t.plan(5);
const input = [ const input = [
{ key: "a", val: 1 }, { key: "a", val: 1 },
{ key: "a", val: 2 }, { key: "b", val: 2 },
{ key: "b", val: 3 }, { key: "a", val: 3 },
{ key: "c", val: 4 }, { key: "c", val: 4 },
]; ];
const results = [ const results = [
{ key: "a", val: 2 }, { key: "a", val: 2 },
{ key: "a", val: 3 }, { key: "b", val: 3 },
{ key: "b", val: 4 }, { key: "a", val: 4 },
{ key: "c", val: 5 }, { key: "c", val: 5 },
]; ];
const destKeys = []; const destinationStreamKeys = [];
const dests = [];
let i = 0; let i = 0;
const sink = new Writable({
objectMode: true,
write(chunk, enc, cb) {
i++;
expect(results).to.deep.include(chunk);
expect(input).to.not.deep.include(chunk);
t.pass();
cb();
if (i === 4) {
t.end();
}
},
});
const construct = (destKey: string) => { const construct = (destKey: string) => {
destKeys.push(destKey); destinationStreamKeys.push(destKey);
const dest = map((chunk: Test) => ({ const dest = map((chunk: Test) => {
...chunk, return {
val: chunk.val + 1, ...chunk,
})) val: chunk.val + 1,
.on("data", (d: Test) => { };
expect(results).to.deep.include(d); });
t.pass();
}) dest.pipe(sink);
.on("end", () => {
i++;
if (i === dests.length) {
t.end();
}
});
dests.push(dest);
return dest; return dest;
}; };
@ -99,9 +107,8 @@ test.cb("should spread per key using keyBy", t => {
{ objectMode: true }, { objectMode: true },
); );
demuxed.on("finish", () => { demuxed.on("finish", () => {
expect(destKeys).to.deep.equal(["a", "b", "c"]); expect(destinationStreamKeys).to.deep.equal(["a", "b", "c"]);
t.pass(); t.pass();
dests.forEach(dest => dest.end());
}); });
input.forEach(event => demuxed.write(event)); input.forEach(event => demuxed.write(event));