-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
155 lines (134 loc) · 4.36 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*!
* promise2stream <https://github.com/hybridables/promise2stream>
*
* Copyright (c) 2016 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/
'use strict'
var fs = require('fs')
var test = require('assertit')
var promise2stream = require('./index')
var isPromise = require('is-promise')
var isStream = require('is-node-stream')
var isBuffer = require('is-buffer')
var Promize = require('pinkie-promise')
test.true = function (val) {
test.strictEqual(isStream(val), true)
}
test('should throw TypeError if `val` not a promise', function (done) {
function fixture () {
promise2stream(123)
}
test.throws(fixture, TypeError)
test.throws(fixture, /expect `val` be promise/)
done()
})
test('should create stream from a resolved promise', function (done) {
var promiseNumber = Promize.resolve(123)
var promiseString = Promize.resolve('str')
var promiseBuffer = Promize.resolve(new Buffer('buff'))
test.true(promise2stream(promiseNumber))
test.true(promise2stream(promiseString))
test.true(promise2stream(promiseBuffer))
done()
})
test('should fire `end` event if promise resolves `null` value', function (done) {
var promise = Promize.resolve(null)
var stream = promise2stream(promise)
var called = 0
/* istanbul ignore next */
stream.on('data', function (val) {
called++
})
stream.once('error', done)
stream.once('end', function () {
test.strictEqual(called, 0)
done()
})
})
test('should fire `error` event if rejected promise', function (done) {
var rejectedPromise = Promize.reject(new Error('foo bar'))
var stream = promise2stream(rejectedPromise)
stream.once('error', function (err) {
test.strictEqual(err instanceof Error, true)
test.strictEqual(err.message, 'foo bar')
done()
})
})
test('should fire `data` event if resolved promise with number value', function (done) {
var resolvedPromise = Promize.resolve(123)
var stream = promise2stream(resolvedPromise)
stream.on('data', function (val) {
test.strictEqual(val, 123)
})
stream.once('error', done)
stream.once('end', done)
})
test('should fire `data` event if object value', function (done) {
var promise = Promize.resolve({ a: 'b' })
var stream = promise2stream(promise)
stream.on('data', function (val) {
test.deepEqual(val, { a: 'b' })
})
stream.once('error', done)
stream.once('end', done)
})
test('should get buffer if promise resolves buffer value', function (done) {
var buffer = Promize.resolve(new Buffer('a b. c'))
promise2stream(buffer)
.on('data', function (buf) {
test.strictEqual(isBuffer(buf), true)
test.strictEqual(buf.toString('utf8'), 'a b. c')
})
.on('end', done)
})
test('should not be object mode stream (opts.objectMode: false)', function (done) {
var promise = Promize.resolve({ foo: 'bar' })
var stream = promise2stream(promise, { objectMode: false })
stream.on('error', function (err) {
test.ifError(!err)
test.strictEqual(err instanceof TypeError, true)
test.strictEqual(/Invalid non-string\/buffer chunk/gi.test(err.message), true)
done()
})
})
test('should access the promise from returned stream, e.g. `stream.promise`', function (done) {
var promise = Promize.resolve(456)
var stream = promise2stream(promise)
test.strictEqual(isPromise(stream.promise), true)
stream.promise.then(function (val) {
test.strictEqual(val, 456)
done()
}, done)
})
test('should get value of promise that resolves another promise', function (done) {
var promise = Promize.resolve('foo bar')
var resolved = Promize.resolve(promise)
var stream = promise2stream(resolved)
stream
.on('data', function (val) {
test.strictEqual(val, 'foo bar')
})
.once('error', done)
.once('end', done)
})
test('should not get value of resolved stream', function (done) {
var pkgStream = fs.createReadStream('package.json')
pkgStream
.on('data', function (val) {
var str = val.toString('utf8')
test.strictEqual(isBuffer(val), true)
test.strictEqual(str.indexOf('license') !== -1, true)
})
.once('error', done)
.once('end', function () {
var promise = Promize.resolve(pkgStream)
var stream = promise2stream(promise)
stream
.on('data', function (val) {
test.strictEqual(isStream(val), true)
})
.once('error', done)
.once('end', done)
})
})