-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkTransforms.spec.ts
87 lines (80 loc) · 2.89 KB
/
chunkTransforms.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { chunkTransforms } from "./chunkTransforms";
import { nils } from "./nils";
type MockController<T> = {
enqueued: T[];
enqueue: (chunk: T) => void;
};
const createMockController = <T>(): MockController<T> => {
const enqueued: T[] = [];
return {
enqueued,
enqueue: (chunk: T) => enqueued.push(chunk),
};
};
describe("chunkTransforms", () => {
it("should call start transformer", async () => {
const startMock = jest.fn().mockResolvedValue([]);
const transformStream = chunkTransforms({
start: startMock,
});
const writer = transformStream.writable.getWriter();
await writer.ready;
expect(startMock).toHaveBeenCalled();
await writer.close();
});
it("should call transform transformer", async () => {
const transformMock = jest.fn().mockResolvedValue([]);
const transformStream = chunkTransforms({
transform: transformMock,
});
transformStream.readable.pipeTo(nils()); // kick stream
const writer = transformStream.writable.getWriter();
await writer.write("test");
expect(transformMock).toHaveBeenCalledWith(["test"], expect.anything());
await writer.close();
});
it("should call flush transformer", async () => {
const flushMock = jest.fn().mockResolvedValue([]);
const transformStream = chunkTransforms({
flush: flushMock,
});
transformStream.readable.pipeTo(nils()); // kick stream
const writer = transformStream.writable.getWriter();
await writer.write("test");
await writer.ready;
await writer.close();
expect(flushMock).toHaveBeenCalledWith(["test"], expect.anything());
});
it("should accumulate chunks with the transform function", async () => {
const mockController = createMockController<string>();
const transformMock = jest.fn((chunks: string[]) => {
mockController.enqueue(chunks.join(""));
return Promise.resolve(chunks);
});
const transformStream = chunkTransforms({
transform: transformMock,
});
transformStream.readable.pipeTo(nils()); // kick stream
const writer = transformStream.writable.getWriter();
await writer.write("chunk1");
await writer.write("chunk2");
await writer.close();
expect(mockController.enqueued).toEqual(["chunk1", "chunk1chunk2"]);
});
it("should can modify chunks with the transform function", async () => {
const mockController = createMockController<string>();
const transformMock = jest.fn(async (chunks: string[]) => {
mockController.enqueue(chunks.join(""));
return chunks.toSpliced(0,1)
});
const transformStream = chunkTransforms({
transform: transformMock,
});
transformStream.readable.pipeTo(nils()); // kick stream
const writer = transformStream.writable.getWriter();
await writer.write("chunk1");
await writer.write("chunk2");
await writer.close();
expect(mockController.enqueued).toEqual(["chunk1", "chunk2"]);
});
});