File: test.js

package info (click to toggle)
node-flush-write-stream 1.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 100 kB
  • sloc: makefile: 6; sh: 2
file content (85 lines) | stat: -rw-r--r-- 1,596 bytes parent folder | download | duplicates (3)
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
var tape = require('tape')
var writer = require('./')

tape('is a write stream', function (t) {
  var expected = ['hello', 'world', 'verden']
  var ws = writer.obj(write)

  ws.write('hello')
  ws.write('world')
  ws.write('verden')
  ws.end(function () {
    t.same(expected.length, 0)
    t.end()
  })

  function write (data, enc, cb) {
    t.same(data, expected.shift())
    cb()
  }
})

tape('is flushable', function (t) {
  var expected = ['hello', 'world', 'verden']
  var flushed = false

  var ws = writer.obj(write, flush)

  ws.write('hello')
  ws.write('world')
  ws.write('verden')
  ws.end(function () {
    t.same(expected.length, 0)
    t.ok(flushed, 'was flushed')
    t.end()
  })

  function write (data, enc, cb) {
    t.same(data, expected.shift())
    cb()
  }

  function flush (cb) {
    flushed = true
    process.nextTick(cb)
  }
})

tape('can pass options', function (t) {
  var expected = ['hello', 'world', 'verden']
  var flushed = false

  var ws = writer({objectMode: true}, write, flush)

  ws.write('hello')
  ws.write('world')
  ws.write('verden')
  ws.end(function () {
    t.same(expected.length, 0)
    t.ok(flushed, 'was flushed')
    t.end()
  })

  function write (data, enc, cb) {
    t.same(data, expected.shift())
    cb()
  }

  function flush (cb) {
    flushed = true
    process.nextTick(cb)
  }
})

tape('emits error on destroy', function (t) {
  var expected = new Error()

  var ws = writer({objectMode: true}, function () {})

  ws.on('error', function (err) {
    t.equal(err, expected)
  })
  ws.on('close', t.end)

  ws.destroy(expected)
})