-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.test.ts
36 lines (27 loc) · 951 Bytes
/
stream.test.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
import { asserts } from './deps.ts'
import { collectStream, readStreamToFn } from './stream.ts'
const items = ['This', 'is', 'the', 'day', 'that the', 'Lord', 'has', 'made']
Deno.test('readStreamToFn reads a stream to the fn', async () => {
const stream = new ReadableStream<string>({
start(controller) {
for (const item of items) controller.enqueue(item)
controller.close()
},
})
let index = 0
await readStreamToFn(stream, (item) => {
asserts.assertEquals(item, items[index])
index++
})
// The reason why we don't do `items.length - 1` here is because index is incremented after the last item assertion
asserts.assertEquals(index, items.length)
})
Deno.test('collectStream collects a stream', async () => {
const stream = new ReadableStream<string>({
start(controller) {
for (const item of items) controller.enqueue(item)
controller.close()
},
})
asserts.assertEquals(await collectStream(stream), items)
})