/* eslint-disable max-nested-callbacks,no-regex-spaces */

'use strict';

const cli = require('../lib/cli'),
  mockStream = require('mock-utf8-stream');

describe('KssBuilderHandlebars builder (default)', function() {
  before(function(done) {
    let stdout = new mockStream.MockWritableStream(),
      stderr = new mockStream.MockWritableStream();

    stdout.startCapture();
    stderr.startCapture();

    // Create a closure with the file name stored.
    let fileCounter;
    this.files = {};
    let fileReader = fileName => {
      return (error, data) => {
        if (error) {
          throw error;
        }

        this.files[fileName] = data;
        fileCounter -= 1;
        if (!fileCounter) {
          done();
        }
      };
    };

    cli({
      stdout: stdout,
      stderr: stderr,
      argv: ['node', 'bin/kss-node', 'test/fixtures/with-include', 'test/output/nested', '--builder', 'test/fixtures/builder', '--helpers', 'test/fixtures/builder/helpers']
    }).catch(function(error) {
      // Pass the error on to the next .then().
      return error;
    }).then(result => {
      expect(result).to.not.be.instanceOf(Error);
      this.stdout = stdout.capturedData;
      let files = [
        'index',
        'section-2',
        'section-3'
      ];
      fileCounter = files.length;
      files.forEach(function(file) {
        fs.readFile(path.join(__dirname, 'output', 'nested', file + '.html'), 'utf8', fileReader(file));
      });
    });
  });

  describe('given --helpers option', function() {
    it('should load additional Handlebars helpers', function(done) {
      expect(this.files.index).to.include('Handlebars helper loaded into template!');
      done();
    });
  });

  describe('KssBuilderBaseHandlebar\'s Handlebars helpers', function() {
    it('should load Handlebars helper: {{{markup}}}', function(done) {
      expect(this.files['section-2']).to.include('Handlebars markup Helper: pseudo-class-hover');
      expect(this.files['section-2']).to.include('Handlebars markup Helper: stars-given<');
      expect(this.files['section-2']).to.include('Handlebars markup Helper: stars-given pseudo-class-hover');
      expect(this.files['section-2']).to.include('Handlebars markup Helper: disabled');
      expect(this.files['section-2']).to.include('Nested Handlebars partial part 1:part 2 of Nested Handlebars partial');
      expect(this.files['section-2']).to.include('Test of Handlebars partial data');
      done();
    });
  });

  describe('builder\'s Handlebars helpers', function() {
    it('should load Handlebars helper: {{section [arg]}}', function(done) {
      expect(this.files['section-3']).to.include('Handlebars Section Helper Test 3');
      expect(this.files['section-3']).to.include('Section 3 has been successfully loaded.');
      done();
    });

    it('should load Handlebars helper: {{eachSection [arg]}}', function(done) {
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper Test 2.1.3');
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper Test 2.1.4');
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper: #each modifiers: :hover');
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper: #each modifiers: .stars-given<');
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper: #each modifiers: .stars-given:hover');
      expect(this.files['section-2']).to.include('Handlebars eachSection Helper: #each modifiers: .disabled');
      done();
    });
  });
});

@@ -221,36 +221,32 @@
 before(function(
-done
 ) %7B%0A    let stdo
@@ -403,359 +403,37 @@
     
-// Create a closure with the file name stored.%0A    let fileCounter;%0A    this.files = %7B%7D;%0A    let fileReader = fileName =%3E %7B
+this.files = %7B%7D;%0A
 %0A    
-  
 return 
-(error, data) =%3E %7B%0A        if (error) %7B%0A          throw error;%0A        %7D%0A%0A        this.files%5BfileName%5D = data;%0A        fileCounter -= 1;%0A        if (!fileCounter) %7B%0A          done();%0A        %7D%0A      %7D;%0A    %7D;%0A%0A    
 cli(
@@ -875,19 +875,35 @@
     
-let files =
+return Promise.all(%0A       
  %5B%0A 
@@ -909,16 +909,18 @@
         
+  
 'index',
@@ -920,16 +920,18 @@
 index',%0A
+  
         
@@ -943,32 +943,34 @@
 ion-2',%0A        
+  
 'section-3'%0A    
@@ -975,89 +975,46 @@
     
-%5D;%0A      fileCounter = files.length;%0A      files.forEach(function(file) %7B%0A       
+  %5D.map(fileName =%3E %7B%0A          return
  fs.
@@ -1021,16 +1021,21 @@
 readFile
+Async
 (path.jo
@@ -1072,16 +1072,20 @@
 d', file
+Name
  + '.htm
@@ -1099,36 +1099,97 @@
 tf8'
-, fileReader(file));%0A      %7D
+).then(data =%3E %7B%0A            this.files%5BfileName%5D = data;%0A          %7D);%0A        %7D)%0A      
 );%0A 
@@ -1305,36 +1305,32 @@
 pers', function(
-done
 ) %7B%0A      expect
@@ -1399,38 +1399,24 @@
 emplate!');%0A
-      done();%0A
     %7D);%0A  %7D)
@@ -1546,36 +1546,32 @@
 p%7D%7D%7D', function(
-done
 ) %7B%0A      expect
@@ -2149,38 +2149,24 @@
 ial data');%0A
-      done();%0A
     %7D);%0A  %7D)
@@ -2285,36 +2285,32 @@
 g%5D%7D%7D', function(
-done
 ) %7B%0A      expect
@@ -2472,38 +2472,24 @@
  loaded.');%0A
-      done();%0A
     %7D);%0A%0A   
@@ -2557,20 +2557,16 @@
 unction(
-done
 ) %7B%0A    
@@ -3207,22 +3207,8 @@
 ');%0A
-      done();%0A
     

Convert test to use Promisified fs.readFile().