'use strict';

const NpmTask = require('../../../lib/tasks/npm-task');
const MockUI = require('console-ui/mock');
const expect = require('../../chai').expect;
const td = require('testdouble');
const SilentError = require('silent-error');

describe('NpmTask', function() {
  describe('checkNpmVersion', function() {
    let task, ui, npm;

    beforeEach(function() {
      ui = new MockUI();
      npm = td.function();
      task = new NpmTask({ ui, npm });
    });

    it('resolves when a compatible version is found', function() {
      td.when(npm(['--version'])).thenResolve({ stdout: '3.2.1' });

      return expect(task.checkNpmVersion()).to.be.fulfilled.then(() => {
        expect(ui.output).to.be.empty;
        expect(ui.errors).to.be.empty;
      });
    });

    it('resolves with warning when a newer version is found', function() {
      td.when(npm(['--version'])).thenResolve({ stdout: '5.0.0' });

      return expect(task.checkNpmVersion()).to.be.fulfilled.then(() => {
        expect(ui.output).to.contain('WARNING');
        expect(ui.errors).to.be.empty;
      });
    });

    it('rejects when an older version is found', function() {
      td.when(npm(['--version'])).thenResolve({ stdout: '2.9.9' });

      return expect(task.checkNpmVersion()).to.be.rejectedWith(SilentError, /npm install -g npm/).then(() => {
        expect(ui.output).to.be.empty;
        expect(ui.errors).to.be.empty;
      });
    });

    it('rejects when npm is not found', function() {
      let error = new Error('npm not found');
      error.code = 'ENOENT';

      td.when(npm(['--version'])).thenReject(error);

      return expect(task.checkNpmVersion()).to.be.rejectedWith(SilentError, /instructions at https:\/\/github.com\/npm\/npm/).then(() => {
        expect(ui.output).to.be.empty;
        expect(ui.errors).to.be.empty;
      });
    });

    it('rejects when npm returns an unreadable version', function() {
      td.when(npm(['--version'])).thenResolve({ stdout: '5' });

      return expect(task.checkNpmVersion()).to.be.rejectedWith(TypeError, /Invalid Version/).then(() => {
        expect(ui.output).to.be.empty;
        expect(ui.errors).to.be.empty;
      });
    });

    it('rejects when an unknown error is thrown', function() {
      td.when(npm(['--version'])).thenReject(new Error('foobar?'));

      return expect(task.checkNpmVersion()).to.be.rejectedWith('foobar?').then(() => {
        expect(ui.output).to.be.empty;
        expect(ui.errors).to.be.empty;
      });
    });
  });
});

@@ -2506,13 +2506,879 @@
 );%0A  %7D);
+%0A%0A  describe('checkYarn', function() %7B%0A    let task, ui, yarn;%0A%0A    beforeEach(function() %7B%0A      ui = new MockUI();%0A      yarn = td.function();%0A      task = new NpmTask(%7B ui, yarn %7D);%0A    %7D);%0A%0A    it('resolves when yarn is found', function() %7B%0A      td.when(yarn(%5B'--version'%5D)).thenResolve(%7B stdout: '0.22.1' %7D);%0A%0A      return expect(task.checkYarn()).to.be.fulfilled;%0A    %7D);%0A%0A    it('rejects when yarn is not found', function() %7B%0A      let error = new Error('yarn not found');%0A      error.code = 'ENOENT';%0A%0A      td.when(yarn(%5B'--version'%5D)).thenReject(error);%0A%0A      return expect(task.checkYarn()).to.be.rejectedWith('yarn not found');%0A    %7D);%0A%0A    it('rejects when an unknown error is thrown', function() %7B%0A      td.when(yarn(%5B'--version'%5D)).thenReject(new Error('foobar?'));%0A%0A      return expect(task.checkYarn()).to.be.rejectedWith('foobar?');%0A    %7D);%0A  %7D);
 %0A%7D);%0A

Add tests for checkYarn() method