"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _Item = _interopRequireDefault(require("../../Item"));
var _VirtualList = _interopRequireDefault(require("../VirtualList"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["index"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var s = Object.getOwnPropertySymbols(e); for (r = 0; r < s.length; r++) o = s[r], t.includes(o) || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
var focus = function focus(elm) {
  return _react.fireEvent.focus(elm);
};
var keyDownUp = function keyDownUp(keyCode) {
  return function (elm) {
    _react.fireEvent.keyDown(elm, {
      keyCode: keyCode
    });
    _react.fireEvent.keyUp(elm, {
      keyCode: keyCode
    });
  };
};
var pressLeftKey = keyDownUp(37);
var pressRightKey = keyDownUp(39);
var pressUpKey = keyDownUp(38);
var pressDownKey = keyDownUp(40);
var pressPageDownKey = keyDownUp(34);
describe('VirtualList useEvent with native `scrollMode`', function () {
  var clientSize, currentFocusIndex, dataSize, handlerOnFocus, items, itemSize, renderItem;
  beforeEach(function () {
    clientSize = {
      clientWidth: 1280,
      clientHeight: 720
    };
    currentFocusIndex = -1;
    dataSize = 200;
    items = [];
    itemSize = 60;
    handlerOnFocus = function handlerOnFocus(index) {
      return function () {
        currentFocusIndex = index;
      };
    };
    renderItem = function renderItem(_ref) {
      var index = _ref.index,
        rest = _objectWithoutProperties(_ref, _excluded);
      // eslint-disable-line enact/display-name
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_Item["default"], _objectSpread(_objectSpread({}, rest), {}, {
        onFocus: handlerOnFocus(index),
        children: items[index].name
      }));
    };
    for (var i = 0; i < dataSize; i++) {
      items.push({
        name: 'Account ' + i
      });
    }
  });
  afterEach(function () {
    clientSize = null;
    dataSize = null;
    items = [];
    itemSize = 60;
    renderItem = null;
  });
  test('should navigate focus using Arrow Up/Down key', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    var item1 = list.children.item(1).children.item(0);
    var item2 = list.children.item(2).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressDownKey(item0);
    expect(currentFocusIndex).toBe(1);
    pressDownKey(item1);
    expect(currentFocusIndex).toBe(2);
    pressUpKey(item2);
    expect(currentFocusIndex).toBe(1);
  });
  test('should navigate focus from first item to last using Arrow Up key when `wrap` is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: 5,
      itemRenderer: renderItem,
      itemSize: itemSize,
      wrap: true
    }));
    jest.useFakeTimers();
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    (0, _react.act)(function () {
      return jest.advanceTimersByTime(1500);
    });
    pressUpKey(item0);
    expect(currentFocusIndex).toBe(4);
    jest.useRealTimers();
  });
  test('should navigate focus from last item to first using Arrow Down key when `wrap` is true', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: 5,
      itemRenderer: renderItem,
      itemSize: itemSize,
      wrap: true
    }));
    jest.useFakeTimers();
    var list = _react.screen.getByRole('list');
    var item4 = list.children.item(4).children.item(0);
    focus(item4);
    expect(currentFocusIndex).toBe(4);
    (0, _react.act)(function () {
      return jest.advanceTimersByTime(1500);
    });
    pressDownKey(item4);
    expect(currentFocusIndex).toBe(0);
    jest.useRealTimers();
  });
  test('should not navigate focus using Arrow Up/Down key when `direction` is `horizontal`', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressDownKey(item0);
    expect(currentFocusIndex).toBe(0);
    pressUpKey(item0);
    expect(currentFocusIndex).toBe(0);
  });
  test('should navigate focus using Arrow Left/Right key when `direction` is `horizontal`', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var list = _react.screen.getByRole('list');
    var item0 = list.children.item(0).children.item(0);
    var item1 = list.children.item(1).children.item(0);
    var item2 = list.children.item(2).children.item(0);
    focus(item0);
    expect(currentFocusIndex).toBe(0);
    pressRightKey(item0);
    expect(currentFocusIndex).toBe(1);
    pressRightKey(item1);
    expect(currentFocusIndex).toBe(2);
    pressLeftKey(item2);
    expect(currentFocusIndex).toBe(1);
  });
  test('should scroll by focus navigation using Arrow Down key', function () {
    var spy = jest.fn(function () {});
    var scrollToFn = global.Element.prototype.scrollTo;
    global.Element.prototype.scrollTo = spy;
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var list = _react.screen.getByRole('list');
    var item13 = list.children.item(13).children.item(0);
    focus(item13);
    expect(currentFocusIndex).toBe(13);
    pressDownKey(item13);
    expect(currentFocusIndex).toBe(14);
    expect(spy).toHaveBeenCalled();
    global.Element.prototype.scrollTo = scrollToFn;
  });
  test('should scroll by Page Down key', function () {
    var spy = jest.fn(function () {});
    var scrollToFn = global.Element.prototype.scrollTo;
    global.Element.prototype.scrollTo = spy;
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList["default"], {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var list = _react.screen.getByRole('list');
    var item9 = list.children.item(9).children.item(0);
    var item10 = list.children.item(10).children.item(0);
    focus(item9);
    expect(currentFocusIndex).toBe(9);
    pressDownKey(item9);
    expect(currentFocusIndex).toBe(10);
    pressPageDownKey(item10);
    expect(spy).toHaveBeenCalled();
    global.Element.prototype.scrollTo = scrollToFn;
  });
});