'use strict';

const expect        = require('../../helpers/expect');
const fs            = require('fs');
const SerializeIcon = require('../../../src/utils/serialize-icon');

const SaveCdvXML    = require('../../../src/utils/save-cordova-xml');

describe('SaveCordovaXML', function() {
  const fixturePath = 'node-tests/fixtures/config.xml';
  const tmpFixturePath = 'tmp/config.xml';

  const projectPath = 'tmp';

  before(() => {
    if (!fs.existsSync(projectPath)) {
      fs.mkdirSync(projectPath);
    }
  });

  afterEach(() => {
    fs.unlinkSync(tmpFixturePath);
  });

  context('when projectPath, desiredNodes, keyName, and serializeFn', () => {
    const projectPath = 'tmp/';
    const desiredNodes = {
      ios: {
        itemKey: 'width',
        items: [
          { size: 60,  name: 'icon-60', src: 'tmp/ios/icon-60.png' }
        ]
      }
    };
    const keyName = 'icon';
    const serializeFn = SerializeIcon;
    const args = {
      projectPath: projectPath,
      desiredNodes: desiredNodes,
      keyName: keyName,
      serializeFn: serializeFn
    };

    context('when config.xml has no platform nodes', () => {
      before(() => {
        fs.createReadStream(`${fixturePath}/no-platform-nodes.xml`)
        .pipe(fs.createWriteStream(tmpFixturePath));
      });

      it('it adds the platform node with icon nodes', (done) => {
        SaveCdvXML(args).then(() => {
          const configXML = fs.readFileSync(tmpFixturePath, 'utf8');
          const expectedConfigXML = fs.readFileSync(
            `${fixturePath}/no-and-ios-platform-node-expected.xml`, 'utf8'
          );

          try {
            expect(configXML).to.equal(expectedConfigXML);
            done();
          } catch(e) {
            done(e);
          }
        })
        .catch((e) => {
          done(e);
        });
      });
    });

    context('when config.xml has desiredNodes platform', () => {
      before(() => {
        fs.createReadStream(`${fixturePath}/ios-platform-node.xml`)
        .pipe(fs.createWriteStream(tmpFixturePath));
      });

      it('it replaces the icon nodes', (done) => {
        SaveCdvXML(args).then(() => {
          const configXML = fs.readFileSync(tmpFixturePath, 'utf8');
          const expectedConfigXML = fs.readFileSync(
            `${fixturePath}/no-and-ios-platform-node-expected.xml`, 'utf8'
          );

          try {
            expect(configXML).to.equal(expectedConfigXML);
            done();
          } catch(e) {
            done(e);
          }
        })
        .catch((e) => {
          done(e);
        });
      });
    });

    context('when config.xml does not have desiredNodes platform', () => {
      before(() => {
        fs.createReadStream(`${fixturePath}/android-platform-node.xml`)
        .pipe(fs.createWriteStream(tmpFixturePath));
      });

      it('it adds the platform node with icon nodes', (done) => {
        SaveCdvXML(args).then(() => {
          const configXML = fs.readFileSync(tmpFixturePath, 'utf8');
          const expectedConfigXML = fs.readFileSync(
            `${fixturePath}/android-platform-node-expected.xml`, 'utf8'
          );

          try {
            expect(configXML).to.equal(expectedConfigXML);
            done();
          } catch(e) {
            done(e);
          }
        })
        .catch((e) => {
          done(e);
        });
      });
    });
  });
});

@@ -1143,32 +1143,36 @@
 %7B%0A      before((
+done
 ) =%3E %7B%0A        f
@@ -1161,32 +1161,54 @@
 ne) =%3E %7B%0A       
+ const fixtureStream =
  fs.createReadSt
@@ -1252,38 +1252,57 @@
 s.xml%60)%0A        
-.pipe(
+const tmpFixtureStream = 
 fs.createWriteSt
@@ -1313,32 +1313,153 @@
 (tmpFixturePath)
+%0A        fixtureStream.pipe(tmpFixtureStream);%0A%0A        tmpFixtureStream.on('finish', () =%3E %7B%0A          done();%0A        %7D
 );%0A      %7D);%0A%0A  
@@ -2078,32 +2078,36 @@
 %7B%0A      before((
+done
 ) =%3E %7B%0A        f
@@ -2096,32 +2096,54 @@
 ne) =%3E %7B%0A       
+ const fixtureStream =
  fs.createReadSt
@@ -2187,38 +2187,57 @@
 e.xml%60)%0A        
-.pipe(
+const tmpFixtureStream = 
 fs.createWriteSt
@@ -2248,32 +2248,153 @@
 (tmpFixturePath)
+%0A        fixtureStream.pipe(tmpFixtureStream);%0A%0A        tmpFixtureStream.on('finish', () =%3E %7B%0A          done();%0A        %7D
 );%0A      %7D);%0A%0A  
@@ -3008,32 +3008,36 @@
 %7B%0A      before((
+done
 ) =%3E %7B%0A        f
@@ -3030,24 +3030,46 @@
 =%3E %7B%0A       
+ const fixtureStream =
  fs.createRe
@@ -3129,22 +3129,41 @@
         
-.pipe(
+const tmpFixtureStream = 
 fs.creat
@@ -3190,16 +3190,137 @@
 urePath)
+%0A        fixtureStream.pipe(tmpFixtureStream);%0A%0A        tmpFixtureStream.on('finish', () =%3E %7B%0A          done();%0A        %7D
 );%0A     

Update test to fix wait for the before hook to complete before executing test