Skip to content

Commit ed875ef

Browse files
authored
Merge branch 'master' into master
2 parents e08f154 + 6d54479 commit ed875ef

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-28
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ function Xray (options) {
182182
return state.stream
183183
}
184184

185-
node.then = function (cb) {
186-
return streamToPromise(node.stream()).then(cb)
185+
node.then = function (resHandler, errHandler) {
186+
return streamToPromise(node.stream()).then(resHandler, errHandler)
187187
}
188188

189189
return node

lib/stream.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ module.exports = {
55
* Streaming array helper
66
*
77
* @param {Stream} data (optional)
8+
* @return {Function}
89
*/
910
array: function stream_array (stream) {
1011
if (!stream) return function () {}
1112
var first = true
1213

1314
return function _stream_array (data, end) {
14-
var json = JSON.stringify(data, true, 2)
15+
var string = JSON.stringify(data, true, 2)
16+
var json = isArray(data) ? string.slice(1, -1) : string
17+
var empty = json.trim() === ''
1518

16-
if (first) {
17-
stream.write('[\n')
18-
first = false
19-
}
20-
21-
if (isArray(data)) {
22-
json = json.slice(1, -1)
23-
}
19+
if (first && empty && !end) return
20+
if (first) { stream.write('[\n') }
21+
if (!first && !empty) { stream.write(',') }
2422

2523
if (end) {
2624
stream.end(json + ']')
2725
} else {
28-
stream.write(json + ',')
26+
stream.write(json)
2927
}
28+
29+
first = false
3030
}
3131
},
3232

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"mocha-lcov-reporter": "1.3.0",
5252
"multiline": "1.0.2",
5353
"rimraf": "2.6.3",
54+
"sinon": "^7.3.2",
5455
"standard": "6.0.8"
5556
},
5657
"engines": {

test/stream_spec.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* global it describe */
2+
3+
/**
4+
* Module Dependencies
5+
*/
6+
7+
var assert = require('assert')
8+
var EventEmitter = require('events')
9+
var streamHelper = require('../lib/stream')
10+
11+
function createStream () {
12+
var instance = new EventEmitter()
13+
instance._data = ''
14+
instance._open = true
15+
instance.on('write', function (chunk) { instance._data += chunk })
16+
instance.once('end', function () { instance._open = false })
17+
18+
instance.write = function write (chunk) { instance.emit('write', String(chunk) || '') }
19+
instance.error = function error (err) { instance.emit('error', err) }
20+
instance.end = function end (chunk) {
21+
if (!instance._open) return
22+
instance.emit('write', chunk)
23+
instance.emit('end')
24+
}
25+
26+
return instance
27+
}
28+
29+
function getSessionResult () {
30+
var events = Array.prototype.slice.call(arguments)
31+
var stream = createStream()
32+
var helper = streamHelper.array(stream)
33+
events.forEach(function (data, index) { helper(data, index === events.length - 1) })
34+
while (stream._open) { /* wait for stream to close */ }
35+
return JSON.stringify(JSON.parse(stream._data))
36+
}
37+
38+
/**
39+
* Tests
40+
*/
41+
42+
describe('stream.array helper', function () {
43+
it('accepts non-empty arrays', function () {
44+
var result = getSessionResult([1, 2], [3])
45+
assert.equal(result, '[1,2,3]')
46+
})
47+
it('accepts one non-empty array', function () {
48+
var result = getSessionResult([1])
49+
assert.equal(result, '[1]')
50+
})
51+
it('accepts one empty array', function () {
52+
var result = getSessionResult([])
53+
assert.equal(result, '[]')
54+
})
55+
it('accepts one single value', function () {
56+
var result = getSessionResult(1)
57+
assert.equal(result, '[1]')
58+
})
59+
it('accepts multiple values', function () {
60+
var result = getSessionResult(1, 2, 3)
61+
assert.equal(result, '[1,2,3]')
62+
})
63+
it('accepts one empty array at the end', function () {
64+
var result = getSessionResult([1, 2], [3], [])
65+
assert.equal(result, '[1,2,3]')
66+
})
67+
it('accepts multiple empty arrays', function () {
68+
var result = getSessionResult([], [], [], [])
69+
assert.equal(result, '[]')
70+
})
71+
it('accepts arrays', function () {
72+
var result = getSessionResult([1], [], [], [2], [])
73+
assert.equal(result, '[1,2]')
74+
})
75+
it('accepts all weird things', function () {
76+
var result = getSessionResult([], [1], [2], [], [], 3, 4, [])
77+
var result2 = getSessionResult([], [1], [2], [], [], 3, 4, [], [])
78+
var result3 = getSessionResult([], [], [1], [2], [], [], 3, 4, [], [])
79+
var result4 = getSessionResult([1], [2], [], [], 3, 4, [], [])
80+
var result5 = getSessionResult([1, 2, 3, 4])
81+
assert.equal(result, '[1,2,3,4]')
82+
assert.equal(result2, '[1,2,3,4]')
83+
assert.equal(result3, '[1,2,3,4]')
84+
assert.equal(result4, '[1,2,3,4]')
85+
assert.equal(result5, '[1,2,3,4]')
86+
})
87+
})

test/xray_spec.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var join = require('path').join
1212
var rm = require('rimraf').sync
1313
var assert = require('assert')
1414
var isUrl = require('is-url')
15+
var sinon = require('sinon')
1516
var Xray = require('..')
1617

1718
/**
@@ -468,25 +469,41 @@ describe('Xray basics', function () {
468469
})
469470
})
470471

471-
describe('.then(cb)', function () {
472-
it('should Promisify and pass cb to .then(cb)', function (done) {
473-
var html = '<ul class="tags"><li>a</li><li>b</li><li>c</li></ul><ul class="tags"><li>d</li><li>e</li></ul>'
474-
var $ = cheerio.load(html)
475-
var x = Xray()
472+
describe('.then(cb, err)', function () {
473+
var noop = function () { }
474+
var html = '<ul class="tags"><li>a</li><li>b</li><li>c</li></ul><ul class="tags"><li>d</li><li>e</li></ul>'
475+
var expected = [['a', 'b', 'c'], ['d', 'e']]
476+
var $ = cheerio.load(html)
477+
var x = Xray()
478+
479+
it('should Promisify and pass cb to promise', function () {
480+
var resHandler = sinon.fake()
481+
var errorHandler = sinon.fake()
476482

477483
var xray = x($, '.tags', [['li']])
484+
var promise = xray.then(resHandler, errorHandler)
478485

479-
xray
480-
.then(function (arr) {
481-
assert(arr[0].length === 3)
482-
assert(arr[0][0] === 'a')
483-
assert(arr[0][1] === 'b')
484-
assert(arr[0][2] === 'c')
485-
assert(arr[1].length === 2)
486-
assert(arr[1][0] === 'd')
487-
assert(arr[1][1] === 'e')
488-
done()
489-
})
486+
return promise.then(function () {
487+
assert(resHandler.calledOnce === true, 'result handler called once')
488+
assert.deepStrictEqual(resHandler.firstCall.args[0], expected)
489+
assert(errorHandler.called === false, 'error handler never called')
490+
})
491+
})
492+
493+
it('should Promisify and pass rejections to promise', function () {
494+
var resHandler = sinon.fake()
495+
var errorHandler = sinon.fake()
496+
497+
var xray = x('https://127.0.0.1:666/', '.tags', [['li']])
498+
process.once('unhandledRejection', noop)
499+
var promise = xray.then(resHandler, errorHandler)
500+
501+
return promise.then(function () {
502+
process.removeListener('unhandledRejection', noop)
503+
assert(resHandler.called === false, 'result handler never called')
504+
assert(errorHandler.calledOnce === true, 'error handler called once')
505+
assert(errorHandler.firstCall.args[0] instanceof Error, 'called with error')
506+
})
490507
})
491508
})
492509
})

0 commit comments

Comments
 (0)