"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _chai() {
  const data = require("chai");

  _chai = function () {
    return data;
  };

  return data;
}

function _dependencyFileLinkGenerator() {
  const data = _interopRequireDefault(require("./dependency-file-link-generator"));

  _dependencyFileLinkGenerator = function () {
    return data;
  };

  return data;
}

function _consumerComponent() {
  const data = _interopRequireDefault(require("../consumer/component/consumer-component"));

  _consumerComponent = function () {
    return data;
  };

  return data;
}

function _barFoo() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/plain-javascript/bar-foo.json"));

  _barFoo = function () {
    return data;
  };

  return data;
}

function _utilsIsString() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/plain-javascript/utils-is-string.json"));

  _utilsIsString = function () {
    return data;
  };

  return data;
}

function _barFoo2() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/es6/bar-foo.json"));

  _barFoo2 = function () {
    return data;
  };

  return data;
}

function _utilsIsString2() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/es6/utils-is-string.json"));

  _utilsIsString2 = function () {
    return data;
  };

  return data;
}

function _barFoo3() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/custom-resolved-modules/bar-foo.json"));

  _barFoo3 = function () {
    return data;
  };

  return data;
}

function _utilsIsString3() {
  const data = _interopRequireDefault(require("../../fixtures/consumer-components/custom-resolved-modules/utils-is-string.json"));

  _utilsIsString3 = function () {
    return data;
  };

  return data;
}

function globalConfig() {
  const data = _interopRequireWildcard(require("../api/consumer/lib/global-config"));

  globalConfig = function () {
    return data;
  };

  return data;
}

const mockBitMap = () => {
  return {
    getComponent: () => ({
      rootDir: '.dependencies/utils/is-string/remote-scope/0.0.1',

      getRootDir() {
        return this.rootDir;
      }

    })
  };
};

const mockConsumer = (distIsInsideTheComponent = true) => {
  const consumer = {
    bitMap: mockBitMap(),
    getPath: () => '/root',
    shouldDistsBeInsideTheComponent: () => true,
    toAbsolutePath: str => `/root/${str}`
  };

  if (!distIsInsideTheComponent) {
    consumer.config = {
      distEntry: 'src',
      distTarget: 'dist'
    };

    consumer.shouldDistsBeInsideTheComponent = () => false;
  }

  return consumer;
};

const mockGetSync = () => {
  globalConfig().getSync = () => '@bit';
};

describe('DependencyFileLinkGenerator', () => {
  before(() => {
    mockGetSync();
  });
  describe('generate()',
  /*#__PURE__*/
  (0, _bluebird().coroutine)(function* () {
    describe('using plain javascript', () => {
      let dependencyFileLinkGenerator;
      let linkResult;
      before(
      /*#__PURE__*/
      (0, _bluebird().coroutine)(function* () {
        const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo().default));
        component.componentMap = {
          rootDir: 'components/bar/foo',

          getRootDir() {
            return this.rootDir;
          }

        };
        const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString().default));
        dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({
          consumer: mockConsumer(),
          bitMap: mockBitMap(),
          component,
          relativePath: component.dependencies.get()[0].relativePaths[0],
          dependencyComponent,
          createNpmLinkFiles: false,
          targetDir: ''
        });
        const linkResults = dependencyFileLinkGenerator.generate();
        linkResult = linkResults[0];
      }));
      it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => {
        (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/utils/is-string.js'));
      });
      it('should generate linkContent that points to the package', () => {
        (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
      });
      it('should set isEs6 to false as it does not have ImportSpecifiers', () => {
        (0, _chai().expect)(linkResult.isEs6).to.be.false;
      });
    });
    describe('using ES6', () => {
      describe('when dist is inside the component dir', () => {
        let dependencyFileLinkGenerator;
        let linkResults;
        before(
        /*#__PURE__*/
        (0, _bluebird().coroutine)(function* () {
          const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo2().default));
          component.componentMap = {
            rootDir: 'components/bar/foo',

            getRootDir() {
              return this.rootDir;
            }

          };
          const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString2().default));
          dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({
            consumer: mockConsumer(),
            bitMap: mockBitMap(),
            component,
            relativePath: component.dependencies.get()[0].relativePaths[0],
            dependencyComponent,
            createNpmLinkFiles: false,
            targetDir: ''
          });
          linkResults = dependencyFileLinkGenerator.generate();
        }));
        it('should generate two link files, one for the source and one for the dist', () => {
          (0, _chai().expect)(linkResults).to.have.lengthOf(2);
        });
        describe('link file of the source', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[0];
          });
          it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/utils/is-string.js'));
          });
          it('should generate linkContent that points to the package', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should set isEs6 to true as it has ImportSpecifiers', () => {
            (0, _chai().expect)(linkResult.isEs6).to.be.true;
          });
        });
        describe('link file of the dist', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[1];
          });
          it('should generate linkPath that consist of component rootDir + dist + sourceRelativePath', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/dist/utils/is-string.js'));
          });
          it('should generate linkContent that points to the package', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should set isEs6 to true as it has ImportSpecifiers', () => {
            (0, _chai().expect)(linkResult.isEs6).to.be.true;
          });
        });
      });
      describe('when dist is outside the component dir', () => {
        let dependencyFileLinkGenerator;
        let linkResults;
        before(
        /*#__PURE__*/
        (0, _bluebird().coroutine)(function* () {
          const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo2().default));
          component.componentMap = {
            rootDir: 'components/bar/foo',

            getRootDir() {
              return this.rootDir;
            }

          };
          const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString2().default));
          dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({
            consumer: mockConsumer(false),
            bitMap: mockBitMap(),
            component,
            relativePath: component.dependencies.get()[0].relativePaths[0],
            dependencyComponent,
            createNpmLinkFiles: false,
            targetDir: ''
          });
          linkResults = dependencyFileLinkGenerator.generate();
        }));
        it('should generate two link files, one for the source and one for the dist', () => {
          (0, _chai().expect)(linkResults).to.have.lengthOf(2);
        });
        describe('link file of the source', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[0];
          });
          it('should generate linkPath that consist of component rootDir + sourceRelativePath', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/utils/is-string.js'));
          });
          it('should generate linkContent that is relative to the linkPath', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should set isEs6 to true as it has ImportSpecifiers', () => {
            (0, _chai().expect)(linkResult.isEs6).to.be.true;
          });
        });
        describe('link file of the dist', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[1];
          });
          it('should generate linkPath that consist of dist + component rootDir + sourceRelativePath', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('dist/components/bar/foo/utils/is-string.js'));
          });
          it('should generate linkContent that points to the package', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should set isEs6 to true as it has ImportSpecifiers', () => {
            (0, _chai().expect)(linkResult.isEs6).to.be.true;
          });
        });
      });
    });
    describe('using custom resolved modules with ES6', () => {
      describe('when dist is inside the component dir', () => {
        let dependencyFileLinkGenerator;
        let linkResults;
        let linkResult;
        before(
        /*#__PURE__*/
        (0, _bluebird().coroutine)(function* () {
          const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo3().default));
          component.componentMap = {
            rootDir: 'components/bar/foo',

            getRootDir() {
              return this.rootDir;
            }

          };
          const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString3().default));
          dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({
            consumer: mockConsumer(),
            bitMap: mockBitMap(),
            component,
            relativePath: component.dependencies.get()[0].relativePaths[0],
            dependencyComponent,
            createNpmLinkFiles: false,
            targetDir: ''
          });
          linkResults = dependencyFileLinkGenerator.generate();
          linkResult = linkResults[0];
        }));
        it('should generate one link file because it is stored in node_modules which is shared between the src and the dist', () => {
          (0, _chai().expect)(linkResults).to.have.lengthOf(1);
        });
        it('should generate linkPath that consist of component rootDir + node_modules + importSource (stored in relativePaths) + index.js', () => {
          (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/node_modules/utils/is-string/index.js'));
        });
        it('should generate linkContent that points to the package', () => {
          (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
        });
        describe('when createNpmLinkFiles is set to true', () => {
          before(() => {
            dependencyFileLinkGenerator.createNpmLinkFiles = true;
            linkResults = dependencyFileLinkGenerator.generate();
            linkResult = linkResults[0];
          });
          it('should generate one link file because it is stored in node_modules which is shared between the src and the dist', () => {
            (0, _chai().expect)(linkResults).to.have.lengthOf(1);
          });
          it('should generate the link as postInstallLink (so then npm install will rewrite the links that are written in node_modules)', () => {
            (0, _chai().expect)(linkResult.postInstallLink).to.be.true;
          });
          it('should generate linkContent that points to the package name instead of a file', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should generate linkPath that consist of node_modules + importSource + index.js (it must be relative because it is written by postinstall script)', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('node_modules/utils/is-string/index.js'));
          });
        });
      });
      describe('when dist is outside the component dir', () => {
        let dependencyFileLinkGenerator;
        let linkResults;
        before(
        /*#__PURE__*/
        (0, _bluebird().coroutine)(function* () {
          const component = yield _consumerComponent().default.fromString(JSON.stringify(_barFoo3().default));
          component.componentMap = {
            rootDir: 'components/bar/foo',

            getRootDir() {
              return this.rootDir;
            }

          };
          const dependencyComponent = yield _consumerComponent().default.fromString(JSON.stringify(_utilsIsString3().default));
          dependencyFileLinkGenerator = new (_dependencyFileLinkGenerator().default)({
            consumer: mockConsumer(false),
            bitMap: mockBitMap(),
            component,
            relativePath: component.dependencies.get()[0].relativePaths[0],
            dependencyComponent,
            createNpmLinkFiles: false,
            targetDir: ''
          });
          linkResults = dependencyFileLinkGenerator.generate();
        }));
        it('should generate two link files, one for the source and one for the dist', () => {
          (0, _chai().expect)(linkResults).to.have.lengthOf(2);
        });
        describe('link file of the source', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[0];
          });
          it('should generate linkPath that consist of component rootDir + importSource + index.js', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('components/bar/foo/node_modules/utils/is-string/index.js'));
          });
          it('should generate linkContent that points to the package', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
          it('should set isEs6 to true as it has ImportSpecifiers', () => {
            (0, _chai().expect)(linkResult.isEs6).to.be.true;
          });
        });
        describe('link file of the dist', () => {
          let linkResult;
          before(() => {
            linkResult = linkResults[1];
          });
          it('should generate linkPath that consist of dist + component rootDir + importSource + index.js', () => {
            (0, _chai().expect)(linkResult.linkPath).to.equal(_path().default.normalize('dist/components/bar/foo/node_modules/utils/is-string/index.js'));
          });
          it('should generate linkContent that points to the package', () => {
            (0, _chai().expect)(linkResult.linkContent).to.equal("module.exports = require('@bit/remote-scope.utils.is-string');");
          });
        });
      });
    });
  }));
});