"use strict";

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

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

var _index = _interopRequireDefault(require("../index"));

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

describe('ExtraHoursMinutes', function () {
  var sandbox = sinon.createSandbox();
  var onChangeStub = sandbox.stub();
  var defaultProps = {
    onChange: onChangeStub,
    extraTimeInSeconds: 480,
    maxTimeInSeconds: 72000
  };
  var testbed = createTestbed(_index.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 hour and minute inputs', function () {
    testbed.render();
    expect(getInputByLabelText(testbed, 'Hours')).to.exist();
    expect(getInputByLabelText(testbed, 'Minutes')).to.exist();
  });
  it('should accept input of minutes', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, '17');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('17');
  });
  it('should accept input of hours', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, '2');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('2');
  });
  it('should accept minutes and callback correct number of seconds', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, '27');

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(27 * 60);
  });
  it('should accept hours and callback correct number of seconds', function () {
    testbed.render();
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    testbed.change(minutesInput, '0');
    testbed.change(hoursInput, '3');

    _testUtils.default.Simulate.blur(minutesInput);

    _testUtils.default.Simulate.blur(hoursInput);

    expect(onChangeStub).to.have.been.calledWith(3 * 60 * 60);
  });
  it('should convert minutes to hours when 60 or greater', function () {
    testbed.render();
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    testbed.change(minutesInput, '159');

    _testUtils.default.Simulate.blur(minutesInput);

    expect(hoursInput.value).to.equal('2');
    expect(minutesInput.value).to.equal('39');
  });
  it('should callback a maximum of maxTimeInSeconds', function () {
    testbed.render();
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    testbed.change(hoursInput, defaultProps.maxTimeInSeconds * 2);

    _testUtils.default.Simulate.blur(hoursInput);

    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, defaultProps.maxTimeInSeconds * 2);

    _testUtils.default.Simulate.blur(minutesInput);

    expect(onChangeStub).to.have.been.calledWith(defaultProps.maxTimeInSeconds);
  });
  it('should reset hour and minute inputs to max if needed on blur', function () {
    testbed.render((0, _objectSpread2.default)({}, defaultProps, {
      maxTimeInSeconds: 5700
    }));
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, 9999);
    testbed.change(hoursInput, 9999);

    _testUtils.default.Simulate.blur(hoursInput);

    expect(hoursInput.value).to.equal('1');
    expect(minutesInput.value).to.equal('35');
  });
  it('should callback a minimum of 0', function () {
    testbed.render();
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    testbed.change(hoursInput, -5);

    _testUtils.default.Simulate.blur(hoursInput);

    testbed.change(minutesInput, -17);

    _testUtils.default.Simulate.blur(minutesInput);

    expect(onChangeStub).to.have.been.calledWith(0);
  });
  it('should ignore a non-number input of minutes', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, 'apple');

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(defaultProps.extraTimeInSeconds);
  });
  it('should ignore a non-number input of hours', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, '12banana5');

    _testUtils.default.Simulate.blur(input);

    expect(onChangeStub).to.have.been.calledWith(defaultProps.extraTimeInSeconds);
  });
  it('should pass error message to child on invalid minutes input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, 'apple');
    expect(testbed.getText()).to.include('apple is not a valid number');
  });
  it('should pass error message to child on invalid hours input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, 'ki23wi');
    expect(testbed.getText()).to.include('ki23wi is not a valid number');
  });
  it('should pass error message to child on invalid minutes input with blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, 'apple');

    _testUtils.default.Simulate.blur(input);

    expect(testbed.getText()).to.include('apple is not a valid number');
  });
  it('should pass error message to child on invalid hours input with blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, 'ki23wi');

    _testUtils.default.Simulate.blur(input);

    expect(testbed.getText()).to.include('ki23wi is not a valid number');
  });
  it('should not pass error message to child on valid minutes input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, 'pear');
    testbed.change(input, '7');
    expect(testbed.getText()).not.to.include('is not a valid number');
  });
  it('should not pass error message to child on valid hours input', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, 'pear');
    testbed.change(input, '7');
    expect(testbed.getText()).not.to.include('is not a valid number');
  });
  it('should reset invalid hours to last good value on blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Hours');
    testbed.change(input, '3');
    testbed.change(input, 'pear');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('3');
  });
  it('should reset invalid minutes to last good value on blur', function () {
    testbed.render();
    var input = getInputByLabelText(testbed, 'Minutes');
    testbed.change(input, '52');
    testbed.change(input, 'kiwi');

    _testUtils.default.Simulate.blur(input);

    expect(input.value).to.equal('52');
  });
  it('should increment correctly on keyboard arrow input', function () {
    testbed.render();
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, '59');
    testbed.change(hoursInput, '1');

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

    expect(minutesInput.value).to.equal('0');
    expect(hoursInput.value).to.equal('2');
    expect(onChangeStub).to.have.been.calledWith(7200);
  });
  it('should decrement correctly on keyboard arrow input', function () {
    testbed.render();
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, '0');
    testbed.change(hoursInput, '4');

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

    expect(minutesInput.value).to.equal('59');
    expect(hoursInput.value).to.equal('3');
    expect(onChangeStub).to.have.been.calledWith(14340);
  });
  it('should normalize correctly on increment', function () {
    testbed.render();
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, '65');
    testbed.change(hoursInput, '1');

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

    expect(minutesInput.value).to.equal('6');
    expect(hoursInput.value).to.equal('2');
    expect(onChangeStub).to.have.been.calledWith(7560);
  });
  it('should normalize correctly on decrement', function () {
    testbed.render();
    var hoursInput = getInputByLabelText(testbed, 'Hours');
    var minutesInput = getInputByLabelText(testbed, 'Minutes');
    testbed.change(minutesInput, '65');
    testbed.change(hoursInput, '1');

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

    expect(minutesInput.value).to.equal('4');
    expect(hoursInput.value).to.equal('2');
    expect(onChangeStub).to.have.been.calledWith(7440);
  });
  it('should be accessible', function (done) {
    testbed.render();
    testbed.checkA11yStandards(done);
  });
});