"use strict";

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _sinon = _interopRequireDefault(require("sinon"));

var _Input = _interopRequireDefault(require("../Input"));

var _spotlight = _interopRequireDefault(require("@enact/spotlight"));

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

var isPaused = function isPaused() {
  return _spotlight.default.isPaused() ? 'paused' : 'not paused';
};

describe('Input Specs', function () {
  it('should have an input element', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, null));
    expect(subject.find('input')).to.have.length(1);
  });
  it('should include a placeholder if specified', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      placeholder: "hello"
    }));
    expect(subject.find('input').prop('placeholder')).to.equal('hello');
  });
  it('should callback onChange when the text changes', function () {
    var handleChange = _sinon.default.spy();

    var value = 'blah';
    var evt = {
      target: {
        value: value
      }
    };
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      onChange: handleChange
    }));
    subject.find('input').simulate('change', evt);
    var expected = value;
    var actual = handleChange.firstCall.args[0].value;
    expect(actual).to.equal(expected);
  });
  it('should blur input on enter if dismissOnEnter', function () {
    var node = document.body.appendChild(document.createElement('div'));

    var handleChange = _sinon.default.spy();

    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      onBlur: handleChange,
      dismissOnEnter: true
    }), {
      attachTo: node
    });
    var input = subject.find('input');
    node.querySelector('input').focus();
    input.simulate('keyDown', {
      nativeEvent: {
        which: 13,
        keyCode: 13
      }
    });
    node.remove();
    var expected = true;
    var actual = handleChange.calledOnce;
    expect(actual).to.equal(expected);
  });
  it('should be able to be disabled', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      disabled: true
    }));
    expect(subject.find('input').prop('disabled')).to.true();
  });
  it('should reflect the value if specified', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      value: "hello"
    }));
    expect(subject.find('input').prop('value')).to.equal('hello');
  });
  it('should have dir equal to rtl when there is rtl text', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      value: "\u05E9\u05D5\u05E2\u05DC \u05D4\u05D7\u05D5\u05DD \u05D4\u05D6\u05E8\u05D9\u05D6 \u05E7\u05E4\u05E5 \u05DE\u05E2\u05DC \u05D4\u05DB\u05DC\u05D1 \u05D4\u05E2\u05E6\u05DC\u05DF.\u05E6\u05D9\u05E4\u05D5\u05E8 \u05E2\u05E4\u05D4 \u05D4\u05E9\u05E2\u05D5\u05E2\u05D9\u05EA \u05E2\u05DD \u05E9\u05E7\u05D9"
    }));
    var expected = 'rtl';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should have dir equal to ltr when there is ltr text', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      value: "content"
    }));
    var expected = 'ltr';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should have dir equal to rtl when there is rtl text in the placeholder', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      value: "\u05E9\u05D5\u05E2\u05DC \u05D4\u05D7\u05D5\u05DD \u05D4\u05D6\u05E8\u05D9\u05D6 \u05E7\u05E4\u05E5 \u05DE\u05E2\u05DC \u05D4\u05DB\u05DC\u05D1 \u05D4\u05E2\u05E6\u05DC\u05DF.\u05E6\u05D9\u05E4\u05D5\u05E8 \u05E2\u05E4\u05D4 \u05D4\u05E9\u05E2\u05D5\u05E2\u05D9\u05EA \u05E2\u05DD \u05E9\u05E7\u05D9"
    }));
    var expected = 'rtl';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should have dir equal to ltr when there is ltr text in the placeholder', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      placeholder: "content"
    }));
    var expected = 'ltr';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should have dir equal to rtl when there is ltr text in the placeholder, but rtl text in value', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      placeholder: "content",
      value: "\u05E9\u05D5\u05E2\u05DC \u05D4\u05D7\u05D5\u05DD \u05D4\u05D6\u05E8\u05D9\u05D6 \u05E7\u05E4\u05E5 \u05DE\u05E2\u05DC \u05D4\u05DB\u05DC\u05D1 \u05D4\u05E2\u05E6\u05DC\u05DF.\u05E6\u05D9\u05E4\u05D5\u05E8 \u05E2\u05E4\u05D4 \u05D4\u05E9\u05E2\u05D5\u05E2\u05D9\u05EA \u05E2\u05DD \u05E9\u05E7\u05D9"
    }));
    var expected = 'rtl';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should have dir equal to ltr when there is rtl text in the placeholder, but ltr text in value', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      placeholder: "\u05E9\u05D5\u05E2\u05DC \u05D4\u05D7\u05D5\u05DD \u05D4\u05D6\u05E8\u05D9\u05D6 \u05E7\u05E4\u05E5 \u05DE\u05E2\u05DC \u05D4\u05DB\u05DC\u05D1 \u05D4\u05E2\u05E6\u05DC\u05DF.\u05E6\u05D9\u05E4\u05D5\u05E8 \u05E2\u05E4\u05D4 \u05D4\u05E9\u05E2\u05D5\u05E2\u05D9\u05EA \u05E2\u05DD \u05E9\u05E7\u05D9",
      value: "content"
    }));
    var expected = 'ltr';
    var actual = subject.find('input').prop('dir');
    expect(actual).to.equal(expected);
  });
  it('should pause spotlight when input has focus', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, null));
    subject.simulate('mouseDown');
    var expected = 'paused';
    var actual = isPaused();

    _spotlight.default.resume();

    expect(actual).to.equal(expected);
  });
  it('should resume spotlight on unmount', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, null));
    subject.simulate('mouseDown');
    subject.unmount();
    var expected = 'not paused';
    var actual = isPaused();

    _spotlight.default.resume();

    expect(actual).to.equal(expected);
  });
  it('should display invalid message if it invalid and invalid message exists', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      invalid: true,
      invalidMessage: "invalid message"
    }));
    expect(subject.find('Tooltip').prop('children')).to.equal('INVALID MESSAGE');
  });
  it('should not display invalid message if it is valid', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_Input.default, {
      invalidMessage: "invalid message"
    }));
    expect(subject.find('Tooltip')).to.have.length(0);
  });
});