"use strict";

require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _ImageItem = _interopRequireDefault(require("../../ImageItem"));
var _VirtualList = require("../VirtualList");
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["index"],
  _excluded2 = ["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; }
describe('VirtualGridList with native `scrollMode`', function () {
  var clientSize, dataSize, items, itemSize, renderItem, svgGenerator;
  beforeEach(function () {
    clientSize = {
      clientWidth: 1280,
      clientHeight: 720
    };
    dataSize = 200;
    items = [];
    itemSize = {
      minWidth: 300,
      minHeight: 240
    };
    renderItem = function renderItem(_ref) {
      var index = _ref.index,
        rest = _objectWithoutProperties(_ref, _excluded);
      // eslint-disable-line enact/display-name
      var _items$index = items[index],
        source = _items$index.source,
        subText = _items$index.subText,
        text = _items$index.text;
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_ImageItem["default"], _objectSpread(_objectSpread({}, rest), {}, {
        label: subText,
        src: source,
        children: text
      }));
    };
    svgGenerator = function svgGenerator(width, height, bgColor, textColor, customText) {
      return "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 ".concat(width, " ").concat(height, "' width='").concat(width, "' height='").concat(height, "'%3E") + "%3Crect width='".concat(width, "' height='").concat(height, "' fill='%23").concat(bgColor, "'%3E%3C/rect%3E") + "%3Ctext x='50%25' y='50%25' dominant-baseline='middle' text-anchor='middle' font-size='36px' fill='%23".concat(textColor, "'%3E").concat(customText, "%3C/text%3E%3C/svg%3E");
    };
    var itemNumberDigits = dataSize > 0 ? (dataSize - 1 + '').length : 0;
    var headingZeros = Array(itemNumberDigits).join('0');
    for (var i = 0; i < dataSize; i++) {
      var count = (headingZeros + i).slice(-itemNumberDigits),
        text = "Item ".concat(count),
        subText = "SubItem ".concat(count),
        color = Math.floor(Math.random() * (0x1000000 - 0x101010) + 0x101010).toString(16),
        source = svgGenerator(300, 300, color, 'ffffff', "Image ".concat(i));
      items.push({
        text: text,
        subText: subText,
        source: source
      });
    }
  });
  afterEach(function () {
    clientSize = null;
    dataSize = null;
    items = null;
    itemSize = null;
    renderItem = null;
  });
  test('should render a list of items', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var expected = 'Item 000';
    var actual = _react.screen.getByRole('list').children.item(0).textContent;
    expect(actual).toBe(expected);
  });
  test('should not render items when `clientSize` and outer DOM size are not specified', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var expected = 0;
    var actual = _react.screen.getByRole('list').children.length;
    expect(actual).toBe(expected);
  });
  test('should render (clientHeight / itemHeight + overhang) items', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var expected = 24; // (4 * (720 / 240)) + 4 * 3    // (4 columns * (clientHeight/itemHeight)) + 4 columns * overhangValue
    var actual = _react.screen.getByRole('list').children.length;
    expect(actual).toBe(expected);
  });
  test('should re-render (clientHeight / itemHeight + overhang) items after changing `clientSize`', function () {
    var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        itemRenderer: renderItem,
        itemSize: itemSize
      })),
      rerender = _render.rerender;
    var newClientSize = {
      clientWidth: 1280,
      clientHeight: 360
    };
    rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
      clientSize: newClientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var expected = 20; // (4 * (360 / 240)) + 4 * 3    // (4 columns * (clientHeight/itemHeight)) + 4 columns * overhangValue
    var actual = _react.screen.getByRole('list').children.length;
    expect(actual).toBe(expected);
  });
  test('should render only one scrollbar when `direction` is `horizontal`', function () {
    (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: itemSize
    }));
    var expected = 2; // One for the list and another for the horizontal scrollbar
    var actual = _react.screen.getByRole('list').parentElement.parentElement.parentElement.parentElement.children.length;
    expect(actual).toBe(expected);
  });
  describe('Scrollbar visibility', function () {
    test('should render both horizontal and vertical scrollbars when `horizontalScrollbar` and `verticalScrollbar` are `visible`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        horizontalScrollbar: "visible",
        itemRenderer: renderItem,
        itemSize: itemSize,
        verticalScrollbar: "visible"
      }));
      var virtualGridListRoot = _react.screen.getByRole('list').parentElement.parentElement.parentElement.parentElement;
      var verticalScrollbar = virtualGridListRoot.children.item(0).children.item(1);
      var horizontalScrollbar = virtualGridListRoot.children.item(1);
      expect(verticalScrollbar).toHaveClass('scrollbar vertical');
      expect(horizontalScrollbar).toHaveClass('scrollbar horizontal');
    });
    test('should render only vertical scrollbar when `verticalScrollbar` is `visible` and `horizontalScrollbar` is `hidden`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        horizontalScrollbar: "hidden",
        itemRenderer: renderItem,
        itemSize: itemSize,
        verticalScrollbar: "visible"
      }));
      var virtualGridListRoot = _react.screen.getByRole('list').parentElement.parentElement.parentElement.parentElement;
      var verticalScrollbar = virtualGridListRoot.children.item(0).children.item(1);
      var horizontalScrollbar = virtualGridListRoot.children.item(1);
      expect(verticalScrollbar).toBeInTheDocument();
      expect(verticalScrollbar).toHaveClass('scrollbar vertical');
      expect(horizontalScrollbar).toBeNull();
    });
    test('should not render any scrollbar when `horizontalScrollbar` and `verticalScrollbar` are `hidden`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        itemRenderer: renderItem,
        itemSize: itemSize,
        horizontalScrollbar: "hidden",
        verticalScrollbar: "hidden"
      }));
      var virtualGridListRoot = _react.screen.getByRole('list').parentElement.parentElement.parentElement.parentElement;
      var verticalScrollbar = virtualGridListRoot.children.item(0).children.item(1);
      var horizontalScrollbar = virtualGridListRoot.children.item(1);
      expect(verticalScrollbar).toBeNull();
      expect(horizontalScrollbar).toBeNull();
    });
  });
  describe('Adding an item', function () {
    test('should render an added item named `Password 0` as the first item', function (done) {
      var itemArray = [{
        name: 'A'
      }, {
        name: 'B'
      }, {
        name: 'C'
      }];
      var renderItemArray = function renderItemArray(_ref2) {
        var index = _ref2.index,
          rest = _objectWithoutProperties(_ref2, _excluded2);
        return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, {
          id: 'item' + index,
          children: itemArray[index].name
        }));
      };
      var _render2 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
          clientSize: clientSize,
          dataSize: itemArray.length,
          itemRenderer: renderItemArray,
          itemSize: itemSize
        })),
        rerender = _render2.rerender;
      itemArray.unshift({
        name: 'Password 0'
      });
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_VirtualList.VirtualGridList, {
        clientSize: clientSize,
        dataSize: itemArray.length,
        itemRenderer: renderItemArray,
        itemSize: itemSize
      }));
      jest.useFakeTimers();
      (0, _react.act)(function () {
        return jest.advanceTimersByTime(0);
      });
      var expected = itemArray[0].name;
      var actual = _react.screen.getByRole('list').children.item(0).textContent;
      expect(actual).toBe(expected);
      done();
      jest.useRealTimers();
    });
  });
});