strom/src/functions/map/map.spec.ts

110 lines
2.9 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";
import { map } from ".";
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);
});
test.cb("map() emits errors during synchronous mapping", t => {
2019-08-15 19:42:54 +00:00
t.plan(3);
2019-08-15 15:54:50 +00:00
const source = new Readable({ objectMode: true });
2019-08-15 19:42:54 +00:00
const mapStream = map((element: string) => {
if (element !== "b") {
throw new Error("Failed mapping");
}
return element.toUpperCase();
});
2019-08-15 15:54:50 +00:00
source
2019-08-15 19:42:54 +00:00
.pipe(mapStream)
.on("data", data => {
expect(data).to.equal("B");
t.pass();
})
2019-08-15 15:54:50 +00:00
.on("error", err => {
2019-08-15 19:42:54 +00:00
source.pipe(mapStream);
mapStream.resume();
2019-08-15 15:54:50 +00:00
expect(err.message).to.equal("Failed mapping");
t.pass();
})
.on("end", t.end);
source.push("a");
source.push("b");
source.push("c");
source.push(null);
});
test("map() emits errors during asynchronous mapping", t => {
t.plan(1);
2019-08-15 19:42:54 +00:00
return new Promise((resolve, _) => {
2019-08-15 15:54:50 +00:00
const source = new Readable({ objectMode: true });
2019-08-15 19:42:54 +00:00
const mapStream = map(async (element: string) => {
await Promise.resolve();
if (element === "b") {
throw new Error("Failed mapping");
}
return element.toUpperCase();
});
2019-08-15 15:54:50 +00:00
source
2019-08-15 19:42:54 +00:00
.pipe(mapStream)
2019-08-15 15:54:50 +00:00
.on("error", err => {
expect(err.message).to.equal("Failed mapping");
t.pass();
resolve();
})
2019-08-15 19:42:54 +00:00
.on("end", () => t.fail);
2019-08-15 15:54:50 +00:00
source.push("a");
source.push("b");
source.push("c");
source.push(null);
2019-08-15 19:42:54 +00:00
source.push(null);
source.push(null);
2019-08-15 15:54:50 +00:00
});
});