/* jshint node:true */
/* jshint esversion: 6 */
/* jshint -W030 */

var chai = require('chai');
var expect = chai.expect;
var jsonexport = require('../lib/index');
var os = require('os');
var stream = require('stream');

describe('Stream', () => {
  it('simple', () => {
    var read = new stream.Readable();
    var write = new stream.Writable();

    write._write = function(chunk, enc, next) {
      var csv = chunk.toString();
      console.log(csv);
      expect(csv).to.equal(`name,lastname,escaped${os.EOL}Bob,Smith${os.EOL}James,David,I am a ""quoted"" field`);
      next();
    };
    read.pipe(jsonexport()).pipe(write);

    read.push(JSON.stringify([{
      name: 'Bob',
      lastname: 'Smith'
    }, {
      name: 'James',
      lastname: 'David',
      escaped: 'I am a "quoted" field'
    }]));
    read.push(null);
  });
  it('simple with options', () => {
    var read = new stream.Readable();
    var write = new stream.Writable();

    write._write = function(chunk, enc, next) {
      var csv = chunk.toString();
      expect(csv).to.equal(`name|lastname|escaped${os.EOL}Bob|Smith${os.EOL}James|David|I am a ""quoted"" field`);
      next();
    };

    read.pipe(jsonexport({
      rowDelimiter: '|'
    })).pipe(write);

    read.push(JSON.stringify([{
      name: 'Bob',
      lastname: 'Smith'
    }, {
      name: 'James',
      lastname: 'David',
      escaped: 'I am a "quoted" field'
    }]));
    read.push(null);
  });
  it('complex', () => {
    var read = new stream.Readable();
    var write = new stream.Writable();

    write._write = function(chunk, enc, next) {
      var csv = chunk.toString();
      expect(csv).to.equal(`id,name,lastname,family.name,family.type${os.EOL}1,Bob,Smith,Peter,Father${os.EOL}2,James,David,Julie,Mother`);
      next();
    };

    read.pipe(jsonexport()).pipe(write);

    read.push(JSON.stringify([{
      id: 1,
      name: 'Bob',
      lastname: 'Smith',
      family: {
        name: 'Peter',
        type: 'Father'
      }
    }, {
      id: 2,
      name: 'James',
      lastname: 'David',
      family: {
        name: 'Julie',
        type: 'Mother'
      }
    }]));
    read.push(null);
  });
});

@@ -254,24 +254,28 @@
 ('simple', (
+done
 ) =%3E %7B%0A    v
@@ -338,32 +338,49 @@
 eam.Writable();%0A
+    var csv = %22%22;
 %0A    write._writ
@@ -410,39 +410,37 @@
 , next) %7B%0A      
-var csv
+chunk
  = chunk.toStrin
@@ -455,24 +455,72 @@
    c
-onsole.log(csv);
+sv += chunk;%0A      next();%0A    %7D;%0A    write.on('finish', () =%3E %7B
 %0A   
@@ -629,36 +629,36 @@
  field%60);%0A      
+do
 ne
-xt
 ();%0A    %7D;%0A    r
@@ -646,24 +646,25 @@
 one();%0A    %7D
+)
 ;%0A    read.p
@@ -926,24 +926,28 @@
  options', (
+done
 ) =%3E %7B%0A    v
@@ -1010,32 +1010,49 @@
 eam.Writable();%0A
+    var csv = %22%22;
 %0A    write._writ
@@ -1082,39 +1082,37 @@
 , next) %7B%0A      
-var csv
+chunk
  = chunk.toStrin
@@ -1108,32 +1108,104 @@
 unk.toString();%0A
+      csv += chunk;%0A      next();%0A    %7D;%0A    write.on('finish', () =%3E %7B%0A
       expect(csv
@@ -1301,36 +1301,36 @@
  field%60);%0A      
+do
 ne
-xt
 ();%0A    %7D;%0A%0A    
@@ -1314,32 +1314,33 @@
    done();%0A    %7D
+)
 ;%0A%0A    read.pipe
@@ -1618,24 +1618,28 @@
 'complex', (
+done
 ) =%3E %7B%0A    v
@@ -1710,16 +1710,33 @@
 able();%0A
+    var csv = %22%22;
 %0A    wri
@@ -1782,23 +1782,21 @@
 %7B%0A      
-var csv
+chunk
  = chunk
@@ -1804,24 +1804,96 @@
 toString();%0A
+      csv += chunk;%0A      next();%0A    %7D;%0A    write.on('finish', () =%3E %7B%0A
       expect
@@ -2018,36 +2018,36 @@
 Mother%60);%0A      
+do
 ne
-xt
 ();%0A    %7D;%0A%0A    
@@ -2039,16 +2039,17 @@
 );%0A    %7D
+)
 ;%0A%0A    r

fix stream test