UNPKG

4.25 kBJavaScriptView Raw
1const Readable = require('stream').Readable
2const Writable = require('stream').Writable
3const assert = require('assert')
4const Catchment = require('catchment')
5const wrote = require('../../src/')
6
7function createWs(nextArg) {
8 const allData = []
9 const allRawData = []
10 const ws = new Writable({
11 write: (chunk, encoding, next) => {
12 allData.push(String(chunk))
13 allRawData.push(chunk)
14 next(nextArg)
15 },
16 })
17 return { ws, allData, allRawData }
18}
19
20const writeTestSuite = {
21 'should write a string to the stream': () => {
22 const testString = 'hello world'
23 const ws = createWs()
24 return wrote.write(ws.ws, testString)
25 .then(() => {
26 assert.deepEqual(ws.allData, [
27 testString,
28 ])
29 assert(!ws.writable)
30 })
31 },
32 'should pipe a readable to the stream': () => {
33 const testString = 'hello world'
34 const ws = createWs()
35 const rs = new Readable({
36 read: () => {
37 rs.push(testString)
38 rs.push(null)
39 },
40 })
41 return wrote.write(ws.ws, rs)
42 .then((res) => {
43 assert.deepEqual(ws.allData, [
44 testString,
45 ])
46 assert(res._writableState.ended)
47 assert.strictEqual(res, ws.ws)
48 })
49 },
50 'should reject when reabable is not readable': () => {
51 const testString = 'hello world'
52 const ws = createWs()
53 const rs = new Readable({
54 read: () => {
55 rs.push(testString)
56 rs.push(null)
57 },
58 })
59 const catchment = new Catchment()
60 rs.pipe(catchment)
61 return catchment.promise
62 .then(() => {
63 return wrote.write(ws.ws, rs)
64 })
65 .then(() => {
66 throw new Error('Should have been rejected')
67 }, (err) => {
68 if (!/Stream is not readable/.test(err.message)) {
69 throw err
70 }
71 })
72 },
73 'should reject with an error when readable throws': () => {
74 const ws = createWs()
75 const error = new Error('test-error')
76 const rs = new Readable({
77 read() {
78 process.nextTick(() => this.emit('error', error))
79 return
80 },
81 })
82 return wrote.write(ws.ws, rs)
83 .then(() => {
84 throw new Error('Should have been rejected')
85 }, (err) => {
86 assert.strictEqual(err, error)
87 })
88 },
89 'should reject when writable throws': () => {
90 const testString = 'hello world'
91 const error = new Error('test-error')
92 const ws = createWs(error)
93 const rs = new Readable({
94 read: () => {
95 rs.push(testString)
96 rs.push(null)
97 },
98 })
99 return wrote.write(ws.ws, rs)
100 .then(() => {
101 throw new Error('Should have been rejected')
102 }, (err) => {
103 assert.strictEqual(err, error)
104 })
105 },
106 'should write nothing when null given': () => {
107 const ws = createWs()
108 return wrote.write(ws.ws, null)
109 .then(() => {
110 assert.deepEqual(ws.allData, [])
111 assert(!ws.writable)
112 })
113 },
114 'should write buffer': () => {
115 const testString = 'hello world'
116 const buffer = Buffer.from(testString)
117 const ws = createWs()
118 return wrote.write(ws.ws, buffer)
119 .then(() => {
120 assert.deepEqual(ws.allRawData, [
121 buffer,
122 ])
123 assert(!ws.writable)
124 })
125 },
126 'should reject if writable is not Writable': () => {
127 return wrote.write('string')
128 .then(() => {
129 throw new Error('Should have been rejected')
130 }, (err) => {
131 if (!/Writable stream expected/.test(err.message)) {
132 throw err
133 }
134 })
135 },
136}
137
138module.exports = writeTestSuite