-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
119 lines (105 loc) · 3.39 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*!
* value2stream <https://github.com/hybridables/value2stream>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var test = require('mukla')
var isBuffer = require('is-buffer')
var value2stream = require('./index')
var Promize = require('pinkie-promise')
function toStream (input, ondata, done) {
return value2stream(input)
.on('data', function (val) {
try {
ondata(val)
} catch (err) {
/* istanbul ignore next */
done(err)
}
})
.once('error', done)
.once('end', done)
}
test('should create stream from number value', function (done) {
toStream(123, function (val) {
test.strictEqual(typeof val, 'number')
test.strictEqual(val, 123)
}, done)
})
test('should create stream from string value', function (done) {
toStream('foo bar', function (val) {
test.strictEqual(typeof val, 'string')
test.strictEqual(val, 'foo bar')
}, done)
})
test('should create stream from buffer value', function (done) {
toStream(new Buffer('buf str'), function (val) {
test.strictEqual(isBuffer(val), true)
test.strictEqual(val.toString(), 'buf str')
}, done)
})
test('should create stream from array value', function (done) {
toStream([1, 'foo', 2], function (val) {
test.deepEqual(val, [1, 'foo', 2])
}, done)
})
test('should create stream from resolved promise', function (done) {
var promise = Promize.resolve('abc')
toStream(promise, function (val) {
test.strictEqual(typeof val, 'string')
test.strictEqual(val, 'abc')
}, done)
})
test('should fire `error` event on rejected promise', function (done) {
var rejected = Promize.reject(new Error('foo msg'))
value2stream(rejected).once('error', function (err) {
test.ifError(!err)
test.strictEqual(err instanceof Error, true)
test.strictEqual(err.message, 'foo msg')
done()
})
})
test('should fire `data` event even if value is Error object', function (done) {
var error = new Error('some err here')
toStream(error, function (val) {
test.strictEqual(val instanceof Error, true)
test.strictEqual(val.message, 'some err here')
}, done)
})
test('should allow custom promise constructor to be used when no native promise', function (done) {
value2stream(123, Promize)
.on('data', function (val) {
test.strictEqual(val, 123)
})
.once('error', done)
.once('end', done)
})
test('should fire `data` event on `opts.objectMode: true` (default) and object value', function (done) {
var stream = value2stream({ a: 'b' })
stream
.on('data', function (val) {
test.deepEqual(val, { a: 'b' })
})
.once('error', done)
.once('end', done)
})
test('should fire `error` event on `opts.objectMode: false` and object value', function (done) {
var stream = value2stream({ a: 'b' }, { objectMode: false })
stream.once('error', function (err) {
test.ifError(!err)
test.strictEqual(err instanceof Error, true)
test.strictEqual(/Invalid non-string\/buffer chunk/.test(err.message), true)
done()
})
})
test('should fire `error` event if non-object mode and promise resolves object', function (done) {
var promise = Promize.resolve({ xxx: 'qux' })
value2stream(promise, { objectMode: false })
.once('error', function (err) {
test.ifError(!err)
test.ok(err.message.indexOf('non-string/buffer chunk'))
done()
})
})