strom/tests/map.spec.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-08-15 15:54:50 +00:00
import { Readable } from "stream";
import test from "ava";
import { expect } from "chai";
2019-08-16 13:02:54 +00:00
import { map } from "../src";
2019-08-15 15:54:50 +00:00
test.cb("map() maps elements synchronously", t => {
t.plan(3);
const source = new Readable({ objectMode: true });
2019-08-15 19:42:54 +00:00
const mapStream = map((element: string) => element.toUpperCase());
2019-08-15 15:54:50 +00:00
const expectedElements = ["A", "B", "C"];
let i = 0;
source
2019-08-15 19:42:54 +00:00
.pipe(mapStream)
2019-08-15 15:54:50 +00:00
.on("data", (element: string) => {
expect(element).to.equal(expectedElements[i]);
t.pass();
i++;
})
.on("error", t.end)
.on("end", t.end);
source.push("a");
source.push("b");
source.push("c");
source.push(null);
});
test.cb("map() maps elements asynchronously", t => {
t.plan(3);
const source = new Readable({ objectMode: true });
2019-08-15 19:42:54 +00:00
const mapStream = map(async (element: string) => {
await Promise.resolve();
return element.toUpperCase();
});
2019-08-15 15:54:50 +00:00
const expectedElements = ["A", "B", "C"];
let i = 0;
source
2019-08-15 19:42:54 +00:00
.pipe(mapStream)
2019-08-15 15:54:50 +00:00
.on("data", (element: string) => {
expect(element).to.equal(expectedElements[i]);
t.pass();
i++;
})
.on("error", t.end)
.on("end", t.end);
source.push("a");
source.push("b");
source.push("c");
source.push(null);
});