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