"use strict";

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

var _propTypes = require("prop-types");

var _CustomPropTypes = _interopRequireWildcard(require("../CustomPropTypes"));

describe('CustomPropTypes', function () {
  it('exports a default object', function () {
    expect(_CustomPropTypes.default).to.be.an('object');
  });
  var _arr = ['attemptHistory', 'higherOrderFunction', 'numberInRange', 'selectors'];

  var _loop = function _loop() {
    var name = _arr[_i];
    it("includes '".concat(name, "' validator"), function () {
      expect(_CustomPropTypes.default[name]).to.be.a('function');
    });
  };

  for (var _i = 0; _i < _arr.length; _i++) {
    _loop();
  }

  describe('higherOrderFunction', function () {
    var higherOrderFunction = _CustomPropTypes.default.higherOrderFunction;
    it('returns an error for non function props', function () {
      var result = higherOrderFunction({
        lolwut: true
      }, 'lolwut');
      expect(result).to.be.an.instanceof(Error);
    });
    it('returns an error for non higher order function props', function () {
      var props = {
        lolwut: function lolwut() {}
      };
      var result = higherOrderFunction(props, 'lolwut');
      expect(result).to.be.an.instanceof(Error);
    });
    it('returns undefined for higher order function props', function () {
      var props = {
        lolwut: function lolwut() {
          return Function.prototype;
        }
      };
      var result = higherOrderFunction(props, 'lolwut');
      expect(result).to.be.undefined();
    });
  });
  describe('requireable', function () {
    it('exports a named requireable utility function', function () {
      expect(_CustomPropTypes.requireable).to.be.a('function');
    });
    it('returns a bound function', function () {
      expect((0, _CustomPropTypes.requireable)()).to.be.a('function');
    });
    it('attaches a bound function on the `isRequired` property', function () {
      expect((0, _CustomPropTypes.requireable)().isRequired).to.be.a('function');
    });
    it('does not invoke the passed function with no props present', function () {
      var spy = sinon.spy();
      (0, _CustomPropTypes.requireable)(spy)({}, 'lolwut');
      expect(spy).to.not.have.been.called();
    });
    it('invokes the passed function with props present', function () {
      var spy = sinon.spy();
      (0, _CustomPropTypes.requireable)(spy)({
        lolwut: true
      }, 'lolwut');
      expect(spy).to.have.been.calledOnce();
    });
  });
  describe('numberInRange', function () {
    beforeEach(function () {
      sinon.stub(console, 'error');
    });
    afterEach(function () {
      console.error.restore();
    });
    var propTypes = {
      percentage: _CustomPropTypes.default.numberInRange(-1, 1)
    };
    var counter = 0;

    function checkProp(value) {
      // PropTypes only logs the first warning for each component. To prevent
      // this, pass a different component name on every call to checkPropTypes.
      // https://github.com/facebook/react/issues/7047#issuecomment-228614964
      counter += 1;

      _propTypes.PropTypes.checkPropTypes(propTypes, {
        percentage: value
      }, 'percentage', "Component".concat(counter));
    }

    it('returns an error if prop is not a number', function () {
      checkProp('foo');
      expect(console.error).to.have.been.calledOnce();
    });
    it('returns an error if prop is less than range start', function () {
      checkProp(-2);
      expect(console.error).to.have.been.calledOnce();
    });
    it('returns an error if prop is greater than range end', function () {
      checkProp(1.1);
      expect(console.error).to.have.been.calledOnce();
    });
    it('does not return an error if prop is within range', function () {
      checkProp(0.5);
      expect(console.error).not.to.have.been.called();
    });
  });
});