"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));

var _react = require("@testing-library/react");

var _react2 = _interopRequireWildcard(require("react"));

var _2 = _interopRequireDefault(require(".."));

function TestComponent() {
  var container = (0, _react2.useRef)(null);

  var _useState = (0, _react2.useState)(0),
      _useState2 = (0, _slicedToArray2.default)(_useState, 2),
      scrollTop = _useState2[0],
      setScrollTop = _useState2[1];

  var list = new Array(null).fill(null).map(function (_, idx) {
    return /*#__PURE__*/_react2.default.createElement("div", {
      key: "".concat(+idx)
    }, "\u7B2C", idx + 1, "\u884C");
  });
  (0, _2.default)({
    container: container,
    onScroll: function onScroll(e) {
      setScrollTop(e.target.scrollTop);
    }
  });
  return /*#__PURE__*/_react2.default.createElement(_react2.default.Fragment, null, /*#__PURE__*/_react2.default.createElement("div", {
    ref: container,
    style: {
      height: '200px',
      overflowY: 'auto'
    },
    "data-testid": "scroll-container"
  }, list), /*#__PURE__*/_react2.default.createElement("div", {
    "data-testid": "scroll-top-value"
  }, scrollTop));
}

describe('useScroll', function () {
  test('should update scroll top of the container twice(invoking on leading and trailing edges)', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var _render, getByTestId;

    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _render = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(TestComponent, null)), getByTestId = _render.getByTestId;
            expect(getByTestId('scroll-top-value').textContent).toBe('0');

            _react.fireEvent.scroll(getByTestId('scroll-container'), {
              target: {
                scrollTop: 200
              }
            });

            _react.fireEvent.scroll(getByTestId('scroll-container'), {
              target: {
                scrollTop: 201
              }
            });

            _react.fireEvent.scroll(getByTestId('scroll-container'), {
              target: {
                scrollTop: 202
              }
            }); // invoking on the leading edge


            expect(getByTestId('scroll-top-value').textContent).toBe('200');
            _context.next = 8;
            return (0, _react.waitFor)(function () {
              // invoking on the trailing edge
              // waiting 200 milliseconds then invoke the throttled scroll event handler
              expect(getByTestId('scroll-top-value').textContent).toBe('202');
            });

          case 8:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  test('should update scroll top of the container twice(invoking on leading edge twice)', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
    var _render2, getByTestId;

    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(TestComponent, null)), getByTestId = _render2.getByTestId;
            expect(getByTestId('scroll-top-value').textContent).toBe('0'); // invoking on the leading edge

            _react.fireEvent.scroll(getByTestId('scroll-container'), {
              target: {
                scrollTop: 200
              }
            });

            setTimeout(function () {
              // wait for 201 milliseconds, then scroll again. Also invoking on the leading edge
              _react.fireEvent.scroll(getByTestId('scroll-container'), {
                target: {
                  scrollTop: 201
                }
              });
            }, 201);
            expect(getByTestId('scroll-top-value').textContent).toBe('200');
            _context2.next = 7;
            return (0, _react.waitFor)(function () {
              expect(getByTestId('scroll-top-value').textContent).toBe('201');
            });

          case 7:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
});