Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lathropd authored Jul 7, 2019
2 parents e08f154 + 6d54479 commit ed875ef
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 28 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ function Xray (options) {
return state.stream
}

node.then = function (cb) {
return streamToPromise(node.stream()).then(cb)
node.then = function (resHandler, errHandler) {
return streamToPromise(node.stream()).then(resHandler, errHandler)
}

return node
Expand Down
20 changes: 10 additions & 10 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ module.exports = {
* Streaming array helper
*
* @param {Stream} data (optional)
* @return {Function}
*/
array: function stream_array (stream) {
if (!stream) return function () {}
var first = true

return function _stream_array (data, end) {
var json = JSON.stringify(data, true, 2)
var string = JSON.stringify(data, true, 2)
var json = isArray(data) ? string.slice(1, -1) : string
var empty = json.trim() === ''

if (first) {
stream.write('[\n')
first = false
}

if (isArray(data)) {
json = json.slice(1, -1)
}
if (first && empty && !end) return
if (first) { stream.write('[\n') }
if (!first && !empty) { stream.write(',') }

if (end) {
stream.end(json + ']')
} else {
stream.write(json + ',')
stream.write(json)
}

first = false
}
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"mocha-lcov-reporter": "1.3.0",
"multiline": "1.0.2",
"rimraf": "2.6.3",
"sinon": "^7.3.2",
"standard": "6.0.8"
},
"engines": {
Expand Down
87 changes: 87 additions & 0 deletions test/stream_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* global it describe */

/**
* Module Dependencies
*/

var assert = require('assert')
var EventEmitter = require('events')
var streamHelper = require('../lib/stream')

function createStream () {
var instance = new EventEmitter()
instance._data = ''
instance._open = true
instance.on('write', function (chunk) { instance._data += chunk })
instance.once('end', function () { instance._open = false })

instance.write = function write (chunk) { instance.emit('write', String(chunk) || '') }
instance.error = function error (err) { instance.emit('error', err) }
instance.end = function end (chunk) {
if (!instance._open) return
instance.emit('write', chunk)
instance.emit('end')
}

return instance
}

function getSessionResult () {
var events = Array.prototype.slice.call(arguments)
var stream = createStream()
var helper = streamHelper.array(stream)
events.forEach(function (data, index) { helper(data, index === events.length - 1) })
while (stream._open) { /* wait for stream to close */ }
return JSON.stringify(JSON.parse(stream._data))
}

/**
* Tests
*/

describe('stream.array helper', function () {
it('accepts non-empty arrays', function () {
var result = getSessionResult([1, 2], [3])
assert.equal(result, '[1,2,3]')
})
it('accepts one non-empty array', function () {
var result = getSessionResult([1])
assert.equal(result, '[1]')
})
it('accepts one empty array', function () {
var result = getSessionResult([])
assert.equal(result, '[]')
})
it('accepts one single value', function () {
var result = getSessionResult(1)
assert.equal(result, '[1]')
})
it('accepts multiple values', function () {
var result = getSessionResult(1, 2, 3)
assert.equal(result, '[1,2,3]')
})
it('accepts one empty array at the end', function () {
var result = getSessionResult([1, 2], [3], [])
assert.equal(result, '[1,2,3]')
})
it('accepts multiple empty arrays', function () {
var result = getSessionResult([], [], [], [])
assert.equal(result, '[]')
})
it('accepts arrays', function () {
var result = getSessionResult([1], [], [], [2], [])
assert.equal(result, '[1,2]')
})
it('accepts all weird things', function () {
var result = getSessionResult([], [1], [2], [], [], 3, 4, [])
var result2 = getSessionResult([], [1], [2], [], [], 3, 4, [], [])
var result3 = getSessionResult([], [], [1], [2], [], [], 3, 4, [], [])
var result4 = getSessionResult([1], [2], [], [], 3, 4, [], [])
var result5 = getSessionResult([1, 2, 3, 4])
assert.equal(result, '[1,2,3,4]')
assert.equal(result2, '[1,2,3,4]')
assert.equal(result3, '[1,2,3,4]')
assert.equal(result4, '[1,2,3,4]')
assert.equal(result5, '[1,2,3,4]')
})
})
49 changes: 33 additions & 16 deletions test/xray_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var join = require('path').join
var rm = require('rimraf').sync
var assert = require('assert')
var isUrl = require('is-url')
var sinon = require('sinon')
var Xray = require('..')

/**
Expand Down Expand Up @@ -468,25 +469,41 @@ describe('Xray basics', function () {
})
})

describe('.then(cb)', function () {
it('should Promisify and pass cb to .then(cb)', function (done) {
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>'
var $ = cheerio.load(html)
var x = Xray()
describe('.then(cb, err)', function () {
var noop = function () { }
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>'
var expected = [['a', 'b', 'c'], ['d', 'e']]
var $ = cheerio.load(html)
var x = Xray()

it('should Promisify and pass cb to promise', function () {
var resHandler = sinon.fake()
var errorHandler = sinon.fake()

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

xray
.then(function (arr) {
assert(arr[0].length === 3)
assert(arr[0][0] === 'a')
assert(arr[0][1] === 'b')
assert(arr[0][2] === 'c')
assert(arr[1].length === 2)
assert(arr[1][0] === 'd')
assert(arr[1][1] === 'e')
done()
})
return promise.then(function () {
assert(resHandler.calledOnce === true, 'result handler called once')
assert.deepStrictEqual(resHandler.firstCall.args[0], expected)
assert(errorHandler.called === false, 'error handler never called')
})
})

it('should Promisify and pass rejections to promise', function () {
var resHandler = sinon.fake()
var errorHandler = sinon.fake()

var xray = x('https://127.0.0.1:666/', '.tags', [['li']])
process.once('unhandledRejection', noop)
var promise = xray.then(resHandler, errorHandler)

return promise.then(function () {
process.removeListener('unhandledRejection', noop)
assert(resHandler.called === false, 'result handler never called')
assert(errorHandler.calledOnce === true, 'error handler called once')
assert(errorHandler.firstCall.args[0] instanceof Error, 'called with error')
})
})
})
})

0 comments on commit ed875ef

Please sign in to comment.