-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
77 lines (68 loc) · 1.58 KB
/
test.js
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
'use strict'
const test = require('tape')
const {pull, through, values, error} = require('pull-stream')
const find = require('./')
test((t) => {
t.plan(2)
const stream = pull(
values([1, 2, 3, 4]),
find((x) => x > 2)
)
t.ok(stream instanceof Promise, '`stream` is a promise')
stream.then((found) => t.equal(found, 3, 'resolved value'))
})
test('terminated on match', (t) => {
t.plan(2)
const processed = []
pull(
values([1, 2, 3]),
through((x) => processed.push(x)),
find((x) => x > 1)
).then((found) => {
t.equal(found, 2, 'resolved value')
t.deepEqual(processed, [1, 2], 'processed values')
})
})
test('without a match', (t) => {
t.plan(2)
const processed = []
pull(
values([1, 2, 3]),
through((x) => processed.push(x)),
find((x) => x > Infinity)
).then((found) => {
t.equal(found, undefined, 'resolved value')
t.deepEqual(processed, [1, 2, 3], 'processed values')
})
})
test('on an empty stream', (t) => {
t.plan(1)
pull(
values([]),
find((x) => x === {})
).then((found) => t.equal(found, undefined, 'resolved value'))
})
test('find a stream with an error in the beginning', (t) => {
t.plan(1)
const testError = new Error('test')
const stream = pull(
error(testError),
find((x) => x === {})
)
stream.catch((e) => t.equal(e, testError, 'rejection value'))
})
test('find a stream which errors', (t) => {
t.plan(1)
const testError = new Error('test')
let count = 0
pull(
(end, cb) => {
if (count++ < 2) {
cb(null, count)
} else {
cb(testError)
}
},
find((x) => x === {})
).catch((e) => t.equal(e, testError, 'rejection value'))
})