'use strict';

var _chai;

function _load_chai() {
  return _chai = require('chai');
}

var _bitId;

function _load_bitId() {
  return _bitId = _interopRequireDefault(require('../../bit-id/bit-id'));
}

var _consumerOverrides;

function _load_consumerOverrides() {
  return _consumerOverrides = _interopRequireDefault(require('./consumer-overrides'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

describe('ConsumerOverrides', function () {
  describe('getOverrideComponentData()', function () {
    describe('when propagation is not set', function () {
      it('should use only the most specific match', function () {
        var overridesFixture = {
          'src/*': { dependencies: { foo: '0.0.1', bar: '0.0.1' } },
          'src/utils/javascript/*': { dependencies: { baz: '0.0.1' } },
          'src/utils/*': { dependencies: { foo: '0.0.1' } },
          'src/utils/javascript/is-string': { dependencies: { foo: '0.0.5' } }
        };
        var componentsOverrides = new (_consumerOverrides || _load_consumerOverrides()).default(overridesFixture);
        var id = new (_bitId || _load_bitId()).default({ name: 'src/utils/javascript/is-string' });
        var result = componentsOverrides.getOverrideComponentData(id);
        (0, (_chai || _load_chai()).expect)(result).to.have.property('dependencies').that.deep.equal({
          foo: '0.0.5'
        });
      });
    });
    describe('when propagation set to true', function () {
      it('should get env results from the more generic wildcard overrides by the exact match', function () {
        var overridesFixture = {
          'src/*': { env: { compiler: 'bit.envs/compiler/babel@7.0.0', tester: 'bit.envs/tester/jest@0.0.1' } },
          'src/utils/javascript/is-string': { propagate: true, env: { compiler: 'bit.envs/compiler/babel@0.0.20' } }
        };
        var componentsOverrides = new (_consumerOverrides || _load_consumerOverrides()).default(overridesFixture);
        var id = new (_bitId || _load_bitId()).default({ name: 'src/utils/javascript/is-string' });
        var result = componentsOverrides.getOverrideComponentData(id);
        (0, (_chai || _load_chai()).expect)(result.env).to.have.property('compiler').that.equal('bit.envs/compiler/babel@0.0.20');
        (0, (_chai || _load_chai()).expect)(result.env).to.have.property('tester').that.equal('bit.envs/tester/jest@0.0.1');
      });
      it('should get env results from the more generic wildcard overrides by the more specific one', function () {
        var overridesFixture = {
          'src/*': {
            propagate: true,
            env: { compiler: 'bit.envs/compiler/babel@7.0.0', tester: 'bit.envs/tester/jest@0.0.1' }
          },
          'src/utils/javascript/*': { propagate: true },
          'src/utils/*': { propagate: true, env: { compiler: 'bit.envs/compiler/babel@0.0.20' } },
          'utils/*': { propagate: true, env: { compiler: 'bit.envs/compiler/somethingelse@0.0.20' } }
        };
        var componentsOverrides = new (_consumerOverrides || _load_consumerOverrides()).default(overridesFixture);
        var id = new (_bitId || _load_bitId()).default({ name: 'src/utils/javascript/is-string' });
        var result = componentsOverrides.getOverrideComponentData(id);
        (0, (_chai || _load_chai()).expect)(result.env).to.have.property('compiler').that.equal('bit.envs/compiler/babel@0.0.20');
        (0, (_chai || _load_chai()).expect)(result.env).to.have.property('tester').that.equal('bit.envs/tester/jest@0.0.1');
      });
      it('should get dependencies results from the more generic wildcard overrides by the more specific one', function () {
        var overridesFixture = {
          'src/*': { propagate: true, dependencies: { foo: '0.0.1', bar: '0.0.1' } },
          'src/utils/javascript/*': { propagate: true, dependencies: { baz: '0.0.1' } },
          'src/utils/*': { propagate: true, dependencies: { foo: '0.0.1' } },
          'src/utils/javascript/is-string': { propagate: true, dependencies: { foo: '0.0.5' } },
          'utils/*': { propagate: true, dependencies: { 'something/else': '0.0.1' } }
        };
        var componentsOverrides = new (_consumerOverrides || _load_consumerOverrides()).default(overridesFixture);
        var id = new (_bitId || _load_bitId()).default({ name: 'src/utils/javascript/is-string' });
        var result = componentsOverrides.getOverrideComponentData(id);
        (0, (_chai || _load_chai()).expect)(result).to.have.property('dependencies').that.deep.equal({
          foo: '0.0.5',
          bar: '0.0.1',
          baz: '0.0.1'
        });
      });
    });
  });
  describe('sortWildcards', function () {
    it('more namespaces should be first', function () {
      var arr = ['bar/*', 'bar/foo/baz/*', 'bar/foo/*'];
      arr.sort((_consumerOverrides || _load_consumerOverrides()).default.sortWildcards);
      (0, (_chai || _load_chai()).expect)(arr).to.deep.equal(['bar/foo/baz/*', 'bar/foo/*', 'bar/*']);
    });
    it('less wildcards should be first', function () {
      var arr = ['bar/foo/baz/*', 'bar/*/*/*', 'bar/foo/*/*'];
      arr.sort((_consumerOverrides || _load_consumerOverrides()).default.sortWildcards);
      (0, (_chai || _load_chai()).expect)(arr).to.deep.equal(['bar/foo/baz/*', 'bar/foo/*/*', 'bar/*/*/*']);
    });
    it('wildcards located most left should be first', function () {
      var arr = ['foo/*', '*/foo'];
      arr.sort((_consumerOverrides || _load_consumerOverrides()).default.sortWildcards);
      (0, (_chai || _load_chai()).expect)(arr).to.deep.equal(['*/foo', 'foo/*']);
    });
  });
});