"use strict";

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

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

  _chai = function () {
    return data;
  };

  return data;
}

function _componentsIndex() {
  const data = _interopRequireDefault(require("./components-index"));

  _componentsIndex = function () {
    return data;
  };

  return data;
}

function _bitId() {
  const data = require("../../bit-id");

  _bitId = function () {
    return data;
  };

  return data;
}

function _models() {
  const data = require("../models");

  _models = function () {
    return data;
  };

  return data;
}

function _object() {
  const data = _interopRequireDefault(require("./object"));

  _object = function () {
    return data;
  };

  return data;
}

describe('ComponentsIndex', () => {
  describe('addOne', () => {
    let componentsIndex;
    beforeEach(() => {
      componentsIndex = new (_componentsIndex().default)('scope-path');
    });
    it('should add to the index array', () => {
      const modelComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-string'
      });
      componentsIndex.addOne(modelComponent);
      const allIds = componentsIndex.getIds();
      const id = new (_bitId().BitId)({
        scope: 'my-scope',
        name: 'is-string'
      });
      (0, _chai().expect)(allIds[0]).to.deep.equal(id);
    });
    it('should not add the same component multiple times', () => {
      const modelComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-string'
      });
      componentsIndex.addOne(modelComponent);
      componentsIndex.addOne(modelComponent);
      componentsIndex.addOne(modelComponent);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(1);
    });
    it('should not add BitObjects that are not Symlink nor ModelComponent', () => {
      const bitObject = new (_object().default)({
        scope: 'my-scope',
        name: 'is-string'
      });
      const result = componentsIndex.addOne(bitObject);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(0);
      (0, _chai().expect)(result).to.be.false;
    });
  });
  describe('remove', () => {
    let componentsIndex;
    beforeEach(() => {
      componentsIndex = new (_componentsIndex().default)('scope-path');
    });
    it('should remove from the index array', () => {
      const modelComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-string'
      });
      componentsIndex.addOne(modelComponent);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(1);
      componentsIndex.removeOne(modelComponent.hash().toString());
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove the correct one when there are multiple', () => {
      const isStringComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-string'
      });
      const isTypeComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-type'
      });
      componentsIndex.addOne(isStringComponent);
      componentsIndex.addOne(isTypeComponent);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeOne(isStringComponent.hash().toString());
      const allIds = componentsIndex.getIds();
      (0, _chai().expect)(allIds).to.have.lengthOf(1);
      const id = new (_bitId().BitId)({
        scope: 'my-scope',
        name: 'is-type'
      });
      (0, _chai().expect)(allIds[0]).to.deep.equal(id);
    });
  });
  describe('removeMany', () => {
    let componentsIndex;
    let isStringComponent;
    let isTypeComponent;
    beforeEach(() => {
      componentsIndex = new (_componentsIndex().default)('scope-path');
      isStringComponent = new (_models().ModelComponent)({
        name: 'is-string'
      });
      isTypeComponent = new (_models().ModelComponent)({
        name: 'is-type'
      });
      componentsIndex.addMany([isStringComponent, isTypeComponent]);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(2);
    });
    it('should remove multiple when removing them at the same removeMany call', () => {
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeMany([isStringComponent.hash().toString(), isTypeComponent.hash().toString()]);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove multiple when removing them with separate removeMany calls', () => {
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      componentsIndex.removeMany([isStringComponent.hash().toString()]);
      componentsIndex.removeMany([isTypeComponent.hash().toString()]);
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
    it('should remove multiple when calling them with separate removeMany calls using array.map', () => {
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(2);
      [isStringComponent.hash().toString(), isTypeComponent.hash().toString()].map(h => componentsIndex.removeMany([h]));
      (0, _chai().expect)(componentsIndex.getIds()).to.have.lengthOf(0);
    });
  });
  describe('getIds', () => {
    let componentsIndex;
    beforeEach(() => {
      componentsIndex = new (_componentsIndex().default)('scope-path');
    });
    it('should not return symlinks', () => {
      const symlink = new (_models().Symlink)({
        scope: 'my-scope',
        name: 'is-type'
      });
      componentsIndex.addOne(symlink);
      const allIds = componentsIndex.getIds();
      (0, _chai().expect)(allIds).to.have.lengthOf(0);
    });
  });
  describe('getIdsIncludesSymlinks', () => {
    let componentsIndex;
    beforeEach(() => {
      componentsIndex = new (_componentsIndex().default)('scope-path');
    });
    it('should return symlinks', () => {
      const symlink = new (_models().Symlink)({
        scope: 'my-scope',
        name: 'is-type'
      });
      componentsIndex.addOne(symlink);
      const allIds = componentsIndex.getIdsIncludesSymlinks();
      (0, _chai().expect)(allIds).to.have.lengthOf(1);
    });
    it('should return components and symlinks if both exist', () => {
      const symlink = new (_models().Symlink)({
        scope: 'my-scope',
        name: 'is-type'
      });
      const modelComponent = new (_models().ModelComponent)({
        scope: 'my-scope',
        name: 'is-string'
      });
      componentsIndex.addMany([symlink, modelComponent]);
      const allIds = componentsIndex.getIdsIncludesSymlinks();
      (0, _chai().expect)(allIds).to.have.lengthOf(2);
    });
  });
});