test.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. var test = require('tap').test
  3. var callback = require('./')
  4. var fs = require('fs')
  5. test('call the callback after end with object mode', function (t) {
  6. var opts = { objectMode: true }
  7. var stream = callback(opts, function (err, results) {
  8. t.deepEqual(results, ['hello'], 'should return the ending value')
  9. t.end()
  10. })
  11. stream.end('hello')
  12. })
  13. test('support multiple writes with object mode', function (t) {
  14. var opts = { objectMode: true }
  15. var stream = callback(opts, function (err, results) {
  16. t.deepEqual(results, ['hello', 'world'], 'should return the ending value')
  17. t.end()
  18. })
  19. stream.write('hello')
  20. stream.end('world')
  21. })
  22. test('works without object mode', function (t) {
  23. var stream = callback(function (err, results) {
  24. t.equal(results.length, 1, 'should contain only one value')
  25. t.deepEqual(results[0].toString(), 'world', 'should return the ending value')
  26. t.end()
  27. })
  28. stream.end('world')
  29. })
  30. test('is pipeable', function (t) {
  31. var write = callback(function (err, results) {
  32. var actual = Buffer.concat(results).toString()
  33. var expected = fs.readFileSync('README.md').toString()
  34. t.equal(actual, expected, 'should have the same content of the file')
  35. t.end()
  36. })
  37. var read = fs.createReadStream('README.md')
  38. read.pipe(write)
  39. })
  40. test('callback.obj shortcut for objectMode', function (t) {
  41. var stream = callback.obj(function (err, results) {
  42. t.deepEqual(results, ['hello'], 'should return the ending value')
  43. t.end()
  44. })
  45. stream.end('hello')
  46. })