"use strict";

var _resolution = _interopRequireDefault(require("@enact/ui/resolution"));
require("@testing-library/jest-dom");
var _react = require("@testing-library/react");
var _userEvent = _interopRequireDefault(require("@testing-library/user-event"));
var _Scroller = _interopRequireDefault(require("../Scroller"));
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 focus = function focus(elm) {
  return _react.fireEvent.focus(elm);
};
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 pressDownKey = keyDownUp(40);
var pressPageUpKey = keyDownUp(33);
describe('Scroller', function () {
  var contents;
  beforeEach(function () {
    contents = /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      style: {
        height: _resolution["default"].scaleToRem(2004),
        width: _resolution["default"].scaleToRem(4002)
      },
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        children: ["Lorem ipsum dolor sit amet, consectetur adipiscing elit.", /*#__PURE__*/(0, _jsxRuntime.jsx)("br", {}), "Aenean id blandit nunc. Donec lacinia nisi vitae mi dictum, eget pulvinar nunc tincidunt. Integer vehicula tempus rutrum. Sed efficitur neque in arcu dignissim cursus."]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        style: {
          marginTop: _resolution["default"].scaleToRem(1602)
        },
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          children: "Mauris blandit sollicitudin mattis. Fusce commodo arcu vitae risus consectetur sollicitudin. Aliquam eget posuere orci. Cras pellentesque lobortis sapien non lacinia."
        })
      })]
    });
  });
  afterEach(function () {
    contents = null;
  });
  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)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        verticalScrollbar: "visible",
        children: contents
      }));
      var scrollButtons = _react.screen.getAllByRole('button');
      var expected = 4;
      expect(scrollButtons).toHaveLength(expected);
    });
    test('should render only vertical scrollbar when `verticalScrollbar` is `visible` and `horizontalScrollbar` is `hidden`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "hidden",
        verticalScrollbar: "visible",
        children: contents
      }));
      var scrollButtons = _react.screen.getAllByRole('button');
      var expectedLength = 2;
      var expectedClass = 'vertical';
      var actual = scrollButtons[0].parentElement;

      // dispatching key event to increase code coverage
      focus(scrollButtons[1]);
      pressDownKey(scrollButtons[1]);
      pressEnterKey(scrollButtons[1]);
      expect(scrollButtons).toHaveLength(expectedLength);
      expect(actual).toHaveClass(expectedClass);
    });
    test('should render only horizontal scrollbar when `verticalScrollbar` is `hidden` and `horizontalScrollbar` is `visible`', /*#__PURE__*/_asyncToGenerator(function* () {
      var user = _userEvent["default"].setup();
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        scrollMode: "translate",
        horizontalScrollbar: "visible",
        verticalScrollbar: "hidden",
        children: contents
      }));
      var scrollButtons = _react.screen.getAllByRole('button');
      var expectedLength = 2;
      var expectedClass = 'horizontal';
      var actual = scrollButtons[0].parentElement;

      // dispatching click event to increase code coverage
      focus(scrollButtons[1]);
      yield user.click(scrollButtons[1]);
      expect(scrollButtons).toHaveLength(expectedLength);
      expect(actual).toHaveClass(expectedClass);
    }));
    test('should not render any scrollbar when when `horizontalScrollbar` and `verticalScrollbar` are `hidden`', function () {
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "hidden",
        verticalScrollbar: "hidden",
        children: contents
      }));
      var scrollButtons = _react.screen.queryAllByRole('button');
      var expected = 0;
      expect(scrollButtons).toHaveLength(expected);
    });
  });
  describe('Scrollbar accessibility', function () {
    test('should set a custom `aria-label` to the up scroll button in the vertical scrollbar', function () {
      var label = 'custom button aria label';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollUpAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var upButton = _react.screen.getByLabelText(label);
      var expected = 'vertical';
      var actual = upButton.parentElement;
      expect(upButton).toBeInTheDocument();
      expect(actual).toHaveClass(expected);
    });
    test('should set a custom `aria-label` to the down scroll button in the vertical scrollbar', function () {
      var label = 'custom button aria label';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollDownAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var downButton = _react.screen.getByLabelText(label);
      var expected = 'vertical';
      var actual = downButton.parentElement;
      expect(downButton).toBeInTheDocument();
      expect(actual).toHaveClass(expected);
    });
    test('should set a custom `aria-label` to the left scroll button in the horizontal scrollbar', function () {
      var label = 'custom button aria label';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollLeftAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var leftButton = _react.screen.getByLabelText(label);
      var expected = 'horizontal';
      var actual = leftButton.parentElement;
      expect(leftButton).toBeInTheDocument();
      expect(actual).toHaveClass(expected);
    });
    test('should set a custom `aria-label` to the right scroll button in the horizontal scrollbar', function () {
      var label = 'custom button aria label';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollRightAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var rightButton = _react.screen.getByLabelText(label);
      var expected = 'horizontal';
      var actual = rightButton.parentElement;
      expect(rightButton).toBeInTheDocument();
      expect(actual).toHaveClass(expected);
    });
    test('should set a null string `aria-label` to the up scroll button in the vertical scrollbar', function () {
      var label = '';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "hidden",
        scrollUpAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var upButton = _react.screen.getAllByRole('button')[0];
      var expectedAttribute = 'aria-label';
      expect(upButton).toHaveAttribute(expectedAttribute, label);
    });
    test('should set a null string `aria-label` to the down scroll button in the vertical scrollbar', function () {
      var label = '';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollDownAriaLabel: label,
        verticalScrollbar: "visible",
        children: contents
      }));
      var downButton = _react.screen.getAllByRole('button')[1];
      var expectedAttribute = 'aria-label';
      expect(downButton).toHaveAttribute(expectedAttribute, label);
    });
    test('should set a null string `aria-label` to the left scroll button in the horizontal scrollbar', function () {
      var label = '';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollLeftAriaLabel: label,
        verticalScrollbar: "hidden",
        children: contents
      }));
      var leftButton = _react.screen.getAllByRole('button')[0];
      var expectedAttribute = 'aria-label';
      expect(leftButton).toHaveAttribute(expectedAttribute, label);
    });
    test('should set a null string `aria-label` to the right scroll button in the horizontal scrollbar', function () {
      var label = '';
      (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        horizontalScrollbar: "visible",
        scrollRightAriaLabel: label,
        verticalScrollbar: "hidden",
        children: contents
      }));
      var rightButton = _react.screen.getAllByRole('button')[1];
      var expectedAttribute = 'aria-label';
      expect(rightButton).toHaveAttribute(expectedAttribute, label);
    });
    test('should change focus on arrow key if `focusableScrollbar`', function () {
      jest.useFakeTimers();
      var spy = jest.fn();
      var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
          focusableScrollbar: true,
          horizontalScrollbar: "visible",
          onScrollStart: spy,
          verticalScrollbar: "visible",
          children: contents
        })),
        rerender = _render.rerender;

      // we need to rerender in order to wait for the scrollButton refs to be populated.
      rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(_Scroller["default"], {
        focusableScrollbar: true,
        horizontalScrollbar: "visible",
        onScrollStart: spy,
        verticalScrollbar: "visible",
        children: contents
      }));
      var buttons = _react.screen.getAllByRole('button');
      focus(buttons[0]);
      pressDownKey(buttons[0]);
      expect(document.activeElement).toBe(buttons[1]);

      // dispatching key event to increase code coverage
      pressPageUpKey(buttons[1]);
      pressPageUpKey(buttons[1]);
      expect(document.activeElement).toBe(buttons[0]);
    });
  });
});