"use strict";

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

var _ = _interopRequireDefault(require(".."));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

describe('ExtraTimeMultiplier', function () {
  var sandbox = sinon.createSandbox();
  var onChangeStub = sandbox.stub();
  var defaultProps = {
    onChange: onChangeStub,
    timerMultiplierValue: 1,
    maxMultiplier: 99,
    minMultiplier: 1
  };
  var testbed = createTestbed(_.default, defaultProps);

  var getInputByLabelText = function getInputByLabelText(testbed, labelText) {
    var labels = testbed.findChildrenByTagName('label');
    var label = labels.filter(function (el) {
      return testbed.getText(el).indexOf(labelText) > -1;
    });
    return label[0].querySelector ? label[0].querySelector('input') : null;
  };

  beforeEach(function () {
    sandbox.resetHistory();
  });
  it('renders one input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    expect(input).to.exist();
  });
  it('should accept a decimal input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, '2.85');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('2.85');
  });
  it('should callback the correct multiplier', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, '3.81');

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(3.81);
  });
  it('should callback a maximum of maxMultiplier', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, defaultProps.maxMultiplier * 2);

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(defaultProps.maxMultiplier);
  });
  it('should callback a minimum of minMultiplier', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, defaultProps.minMultiplier * -2);

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(defaultProps.minMultiplier);
  });
  it('should pass error message to child on invalid input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, 'apple');
    expect(testbed.getText()).to.include('apple is not a valid number');
  });
  it('should pass error message to child on invalid input followed by blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, 'banana');

    _testUtils.default.Simulate.blur(input);

    expect(testbed.getText()).to.include('banana is not a valid number');
  });
  it('should not pass error message to child on valid input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, 'pear');
    testbed.change(input, '1.51');
    expect(testbed.getText()).not.to.include('is not a valid number');
  });
  it('should reset to valid input after blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, '1.51');
    testbed.change(input, 'pear');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('1.51');
  });
  it('should reset to max on increment above max', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, '9999999');

    _testUtils.default.Simulate.keyDown(input, {
      key: 'ArrowUp',
      keyCode: 38
    });

    expect(Number(input.value)).to.equal(defaultProps.maxMultiplier);
    expect(onChangeStub).to.have.been.calledWith(defaultProps.maxMultiplier);
  });
  it('should reset to min on decrement  below min', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Multiplier');
    testbed.change(input, '-25');

    _testUtils.default.Simulate.keyDown(input, {
      key: 'ArrowDown',
      keyCode: 40
    });

    expect(Number(input.value)).to.equal(defaultProps.minMultiplier);
    expect(onChangeStub).to.have.been.calledWith(defaultProps.minMultiplier);
  });
  it('should be accessible', function (done) {
    testbed.render();
    testbed.checkA11yStandards(done);
  });
});