"use strict";

var _spotlight = _interopRequireDefault(require("@enact/spotlight"));
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
var _Input = _interopRequireDefault(require("../Input"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
var isPaused = function isPaused() {
  return _spotlight["default"].isPaused() ? 'paused' : 'not paused';
};
var keyDownUp = function keyDownUp(keyCode) {
  return function (elm) {
    _react.fireEvent.keyDown(elm, {
      keyCode: keyCode
    });
    return _react.fireEvent.keyUp(elm, {
      keyCode: keyCode
    });
  };
};
var pressEnterKey = keyDownUp(13);
var pressLeftKey = keyDownUp(37);
var pressRightKey = keyDownUp(39);
describe('Input Specs', function () {
  test('should have an input element', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {}));
    var expected = 'INPUT';
    var actual = _react.screen.getByLabelText('Input field').lastElementChild.tagName;
    expect(actual).toBe(expected);
  });
  test('should include a placeholder if specified', function () {
    var placeholder = 'hello';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      placeholder: placeholder
    }));
    var expected = 'placeholder';
    var actual = _react.screen.getByLabelText('hello Input field').children[0];
    expect(actual).toHaveAttribute(expected, placeholder);
  });
  test('should have size large by default', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {}));
    var expected = 'large';
    var actual = _react.screen.getByLabelText('Input field');
    expect(actual).toHaveClass(expected);
  });
  test('should have size small when size prop equals "small"', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      size: "small"
    }));
    var expected = 'small';
    var actual = _react.screen.getByLabelText('Input field');
    expect(actual).toHaveClass(expected);
  });
  test('should support iconAfter', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      iconAfter: "0x0F0014"
    }));
    var expected = 983060; // decimal converted charCode of `happyface` character
    var actual = _react.screen.getByLabelText('Input field').children[1].textContent.codePointAt();
    expect(actual).toBe(expected);
  });
  test('should support iconBefore', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      iconBefore: "0x0F0014"
    }));
    var expected = 983060; // decimal converted charCode of `happyface` character
    var actual = _react.screen.getByLabelText('Input field').children[0].textContent.codePointAt();
    expect(actual).toBe(expected);
  });
  test('should have icon size large by default', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      iconAfter: "happyface"
    }));
    var expected = 'large';
    var actual = _react.screen.getByLabelText('Input field').children[1];
    expect(actual).toHaveClass(expected);
  });
  test('should set icon size to small when size equals small', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      iconAfter: "happyface",
      size: "small"
    }));
    var expected = 'small';
    var actual = _react.screen.getByLabelText('Input field').children[1];
    expect(actual).toHaveClass(expected);
  });
  test('should set `spellcheck=false` attribute when type is `password`', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      minLength: 8,
      type: "password",
      value: "passwordValue"
    }));
    var input = _react.screen.getByLabelText('13 characters Input field').children[0];
    var expectedAttribute = 'spellcheck';
    var expectedValue = 'false';
    expect(input).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should callback onChange when the text changes', function () {
    var handleChange = jest.fn();
    var value = 'blah';
    var evt = {
      target: {
        value: value
      }
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      onChange: handleChange
    }));
    var input = _react.screen.getByLabelText('Input field').lastElementChild;
    _react.fireEvent.change(input, evt);
    var actual = handleChange.mock.calls[0][0].value;
    expect(actual).toBe(value);
  });
  test('should forward an event with a stopPropagation method from onChange handler', function () {
    var handleChange = jest.fn();
    var value = 'blah';
    var evt = {
      target: {
        value: value
      },
      stopPropagation: jest.fn()
    };
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      onChange: handleChange
    }));
    var inputField = _react.screen.getByLabelText('Input field').children[0];
    _react.fireEvent.change(inputField, evt);
    var actual = typeof handleChange.mock.calls[0][0].stopPropagation === 'function';
    expect(actual).toBeTruthy();
  });
  test('should blur input on enter if `dismissOnEnter`', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      dismissOnEnter: true,
      onBlur: handleChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    _react.fireEvent.mouseDown(inputText);
    pressEnterKey(inputText);
    expect(handleChange).toHaveBeenCalled();
  });
  test('should not call onBlur event on enter if `dismissOnEnter` and `autofocus`', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      autoFocus: true,
      dismissOnEnter: true,
      onBlur: handleChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    _react.fireEvent.mouseDown(inputText);
    pressEnterKey(inputText);
    expect(handleChange).not.toHaveBeenCalled();
  });
  test('should prevent onChange if onBeforeChange prevents', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleBeforeChange = jest.fn(function (ev) {
      return ev.preventDefault();
    });
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 'blah';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      onBeforeChange: handleBeforeChange,
      onChange: handleChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    yield user.type(inputText, value);
    // bluring input onSpotlightRight for code coverage purposes
    pressRightKey(inputText);
    pressRightKey(inputText);
    pressRightKey(inputText);
    pressRightKey(inputText);
    pressRightKey(inputText);
    pressRightKey(inputText);
    expect(handleChange).not.toHaveBeenCalled();
  }));
  test('should not bubble the native event when stopPropagation from onChange is called', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 'smt';
    function stop(ev) {
      ev.stopPropagation();
    }
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      onChange: handleChange,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
        onChange: stop
      })
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    yield user.type(inputText, value);
    expect(handleChange).not.toHaveBeenCalled();
  }));
  test('should callback onBeforeChange before the text changes', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleBeforeChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 'blah';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      onBeforeChange: handleBeforeChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    yield user.type(inputText, value);
    // bluring input onSpotlightLeft for code coverage purposes
    pressLeftKey(inputText);
    expect(handleBeforeChange).toHaveBeenCalled();
  }));
  test('should activate input on enter', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      onActivate: handleChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    pressEnterKey(inputText);
    expect(handleChange).toHaveBeenCalled();
  });
  test('should not activate input on enter when disabled', function () {
    var handleChange = jest.fn();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      disabled: true,
      onActivate: handleChange
    }));
    var inputText = _react.screen.getByLabelText('Input field').children[0];
    pressEnterKey(inputText);
    expect(handleChange).not.toHaveBeenCalled();
  });
  test('should be able to be disabled', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      disabled: true
    }));
    var actual = _react.screen.getByLabelText('Input field');
    var expected = 'disabled';
    expect(actual).toHaveAttribute(expected);
  });
  test('should reflect the value if specified', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      value: "hello"
    }));
    var actual = _react.screen.getByLabelText('hello Input field').children[0];
    var expected = 'hello';
    expect(actual).toHaveAttribute('value', expected);
  });
  test('should have dir equal to rtl when there is rtl text', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_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 inputField = _react.screen.getByLabelText('שועל החום הזריז קפץ מעל הכלב העצלן.ציפור עפה השעועית עם שקי' + ' Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'rtl';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should have dir equal to ltr when there is ltr text', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      value: "content"
    }));
    var inputField = _react.screen.getByLabelText('content Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'ltr';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should have dir equal to rtl when there is rtl text in the placeholder', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_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"
    }));
    var inputField = _react.screen.getByLabelText('שועל החום הזריז קפץ מעל הכלב העצלן.ציפור עפה השעועית עם שקי' + ' Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'rtl';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should have dir equal to ltr when there is ltr text in the placeholder', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      placeholder: "content"
    }));
    var inputField = _react.screen.getByLabelText('content Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'ltr';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should have dir equal to rtl when there is ltr text in the placeholder, but rtl text in value', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_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 inputField = _react.screen.getByLabelText('שועל החום הזריז קפץ מעל הכלב העצלן.ציפור עפה השעועית עם שקי' + ' Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'rtl';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should have dir equal to ltr when there is rtl text in the placeholder, but ltr text in value', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_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 inputField = _react.screen.getByLabelText('content Input field').children[0];
    var expectedAttribute = 'dir';
    var expectedValue = 'ltr';
    expect(inputField).toHaveAttribute(expectedAttribute, expectedValue);
  });
  test('should pause spotlight when input has focus', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {}));
    var inputField = _react.screen.getByLabelText('Input field').children[0];
    _react.fireEvent.mouseDown(inputField);
    var expected = 'paused';
    var actual = isPaused();
    _spotlight["default"].resume();
    expect(actual).toBe(expected);
  });
  test('should resume spotlight on unmount', function () {
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {})),
      unmount = _render.unmount;
    var inputField = _react.screen.getByLabelText('Input field').children[0];
    _react.fireEvent.mouseDown(inputField);
    unmount();
    var expected = 'not paused';
    var actual = isPaused();
    _spotlight["default"].resume();
    expect(actual).toBe(expected);
  });
  test('should display invalid message if it invalid and invalid message exists', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      invalid: true,
      invalidMessage: "invalid message"
    }));
    var actual = _react.screen.getByText('invalid message');
    var expected = 'tooltipLabel';
    expect(actual).toHaveClass(expected);
  });
  test('should not display invalid message if it is valid', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      invalidMessage: "invalid message"
    }));
    var actual = _react.screen.queryByText('invalid message');
    expect(actual).toBeNull();
  });
  test('should support clearButton', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      clearButton: true
    }));
    var expected = 983097; // decimal converted charCode of character
    var actual = _react.screen.getByLabelText('Input field').children[1].textContent.codePointAt();
    expect(actual).toBe(expected);
  });
  test('should support custom icon for clearButton', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      clearButton: true,
      clearIcon: "0x0F0014"
    }));
    var expected = 983060; // decimal converted charCode of character
    var actual = _react.screen.getByLabelText('Input field').children[1].textContent.codePointAt();
    expect(actual).toBe(expected);
  });
  test('should clear input value when clearButton is clicked', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      clearButton: true,
      onChange: handleChange,
      value: "Hello Input"
    }));
    var clearButton = _react.screen.getByLabelText('Hello Input Input field').children[1];
    yield user.click(clearButton);
    var expectedValue = '';

    // waitFor is used here to give input some time to call onChange
    yield (0, _react.waitFor)(function () {
      var actual = handleChange.mock.calls[0][0].value;
      expect(actual).toBe(expectedValue);
    });
  }));
  test('should not call onChange when clearButton is disabled', /*#__PURE__*/_asyncToGenerator(function* () {
    var handleChange = jest.fn();
    var user = _userEvent["default"].setup();
    var value = 'Hello Input';
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Input["default"], {
      clearButton: true,
      disabled: true,
      onChange: handleChange,
      value: value
    }));
    var clearButton = _react.screen.getByLabelText('Hello Input Input field').children[1];
    yield user.click(clearButton);
    var expectedValue = 0;

    // waitFor is used here to give input some time to call onChange
    yield (0, _react.waitFor)(function () {
      var actual = handleChange.mock.calls.length;
      expect(actual).toBe(expectedValue);
    });
  }));
});