strom/tests/compose.spec.ts

69 lines
1.6 KiB
TypeScript
Raw Normal View History

2019-08-21 19:40:19 +00:00
const test = require("ava");
2019-08-22 18:52:39 +00:00
const { expect } = require("chai");
2019-08-22 16:07:30 +00:00
const { compose, composeDuplex, map } = require("../src");
2019-08-21 19:40:19 +00:00
2019-08-22 18:52:39 +00:00
test.cb("compose() chains two streams together in the correct order", t => {
t.plan(3);
let i = 0;
const first = map((chunk: number) => chunk + 1);
const second = map((chunk: number) => chunk * 2);
2019-08-21 19:40:19 +00:00
const composed = compose(
[first, second],
{ objectMode: true },
);
2019-08-22 16:07:30 +00:00
composed.on("data", data => {
2019-08-22 18:52:39 +00:00
expect(data).to.equal(result[i]);
t.pass();
i++;
if (i === 3) {
t.end();
}
2019-08-22 16:07:30 +00:00
});
2019-08-22 18:52:39 +00:00
composed.on("error", err => {
t.end(err);
2019-08-22 16:07:30 +00:00
});
2019-08-22 18:52:39 +00:00
composed.on("end", () => {
t.end();
2019-08-22 16:07:30 +00:00
});
2019-08-21 19:40:19 +00:00
2019-08-22 18:52:39 +00:00
const input = [1, 2, 3];
const result = [4, 6, 8];
input.forEach(item => composed.write(item));
2019-08-22 16:07:30 +00:00
});
2019-08-22 18:52:39 +00:00
test.cb(
"compose() followed by pipe chains streams together in the correct order",
t => {
t.plan(3);
let i = 0;
const first = map((chunk: number) => chunk + 1);
const second = map((chunk: number) => chunk * 2);
2019-08-21 19:40:19 +00:00
2019-08-22 18:52:39 +00:00
const composed = compose(
[first, second],
{ objectMode: true },
);
const third = map((chunk: number) => chunk + 1);
composed.pipe(third).on("data", data => {
expect(data).to.equal(result[i]);
t.pass();
i++;
if (i === 3) {
t.end();
}
});
2019-08-22 16:07:30 +00:00
2019-08-22 18:52:39 +00:00
composed.on("error", err => {
t.end(err);
});
const input = [1, 2, 3];
const result = [5, 7, 9];
input.forEach(item => composed.write(item));
},
);