"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
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 = _interopRequireWildcard(require("react"));
var _react2 = require("@testing-library/react");
var _testingLibrary = require("@wix/wix-ui-test-utils/testing-library");
var _react3 = require("../../utils/test-utils/react");
var _useDebouncedCallback = _interopRequireDefault(require("./useDebouncedCallback"));
var _Box = _interopRequireDefault(require("../../Box"));
var _Text = _interopRequireDefault(require("../../Text"));
var _Button = _interopRequireDefault(require("../../Button"));
var _ButtonUni = require("../../Button/Button.uni.driver");
var _TextUni = require("../../Text/Text.uni.driver");
var _unit = require("../../utils/test-utils/unit");
var _jsxFileName = "/home/builduser/work/57e038ea7326c1ec/packages/wix-design-system/dist/cjs/providers/useDebouncedCallback/useDebouncedCallback.spec.jsx",
  _this = void 0;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
var dataHooks = {
  count: 'test-component-count',
  button: 'test-component-button'
};
var ButtonTestkit = (0, _testingLibrary.uniTestkitFactoryCreator)(_ButtonUni.buttonDriverFactory);
var TextTestkit = (0, _testingLibrary.uniTestkitFactoryCreator)(_TextUni.textUniDriverFactory);
var TestComponent = function TestComponent(_ref) {
  var onClick = _ref.onClick,
    debounceMs = _ref.debounceMs;
  var _useState = (0, _react.useState)(0),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    counter = _useState2[0],
    setCounter = _useState2[1];
  var debouncedIncreaseCounter = (0, _useDebouncedCallback["default"])(function () {
    var newCount = counter + 1;
    setCounter(newCount);
    if (onClick) {
      onClick(newCount);
    }
  }, [onClick, counter, setCounter], debounceMs,
  // default lodash debounce doesn't work with jest fake timers
  _unit.debounce);
  return /*#__PURE__*/_react["default"].createElement(_Box["default"], {
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 38,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react["default"].createElement(_Text["default"], {
    dataHook: dataHooks.count,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 39,
      columnNumber: 7
    }
  }, counter), /*#__PURE__*/_react["default"].createElement(_Button["default"], {
    dataHook: dataHooks.button,
    onClick: debouncedIncreaseCounter,
    __self: _this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 40,
      columnNumber: 7
    }
  }, "Increase"));
};
vi.useFakeTimers();
describe('useDebouncedCallback', function () {
  it('debounces calls to provided callback', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
    var debounceDelay, onClick, _render, container, buttonTestkit, textTestkit;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          debounceDelay = 200;
          onClick = vi.fn();
          _render = (0, _react2.render)(/*#__PURE__*/_react["default"].createElement(TestComponent, {
            debounceMs: debounceDelay,
            onClick: onClick,
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 54,
              columnNumber: 7
            }
          })), container = _render.container;
          buttonTestkit = ButtonTestkit({
            wrapper: container,
            dataHook: dataHooks.button
          });
          textTestkit = TextTestkit({
            wrapper: container,
            dataHook: dataHooks.count
          });
          _context.t0 = expect;
          _context.next = 8;
          return textTestkit.getText();
        case 8:
          _context.t1 = _context.sent;
          (0, _context.t0)(_context.t1).toBe('0');
          _context.next = 12;
          return buttonTestkit.click();
        case 12:
          _context.next = 14;
          return buttonTestkit.click();
        case 14:
          (0, _react3.act)(function () {
            return vi.advanceTimersByTime(debounceDelay);
          });
          expect(onClick).toHaveBeenCalledTimes(1);
          expect(onClick).toHaveBeenNthCalledWith(1, 1);
          _context.t2 = expect;
          _context.next = 20;
          return textTestkit.getText();
        case 20:
          _context.t3 = _context.sent;
          (0, _context.t2)(_context.t3).toBe('1');
        case 22:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })));
  it('updates function when dependencies change', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2() {
    var debounceDelay, onClick, _render2, container, buttonTestkit, textTestkit;
    return _regenerator["default"].wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          debounceDelay = 200;
          onClick = vi.fn();
          _render2 = (0, _react2.render)(/*#__PURE__*/_react["default"].createElement(TestComponent, {
            debounceMs: debounceDelay,
            onClick: onClick,
            __self: _this,
            __source: {
              fileName: _jsxFileName,
              lineNumber: 76,
              columnNumber: 7
            }
          })), container = _render2.container;
          buttonTestkit = ButtonTestkit({
            wrapper: container,
            dataHook: dataHooks.button
          });
          textTestkit = TextTestkit({
            wrapper: container,
            dataHook: dataHooks.count
          });
          _context2.next = 7;
          return buttonTestkit.click();
        case 7:
          _context2.next = 9;
          return buttonTestkit.click();
        case 9:
          (0, _react3.act)(function () {
            return vi.advanceTimersByTime(debounceDelay);
          });
          _context2.next = 12;
          return buttonTestkit.click();
        case 12:
          _context2.next = 14;
          return buttonTestkit.click();
        case 14:
          (0, _react3.act)(function () {
            return vi.advanceTimersByTime(debounceDelay);
          });
          expect(onClick).toHaveBeenCalledTimes(2);
          expect(onClick).toHaveBeenNthCalledWith(1, 1);
          expect(onClick).toHaveBeenNthCalledWith(2, 2);
          _context2.t0 = expect;
          _context2.next = 21;
          return textTestkit.getText();
        case 21:
          _context2.t1 = _context2.sent;
          (0, _context2.t0)(_context2.t1).toBe('2');
        case 23:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  })));
  it('creates new callback when debounce delay changes', /*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee4() {
    var fn, _renderHook, result, rerender, debouncedCallback;
    return _regenerator["default"].wrap(function _callee4$(_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          fn = vi.fn();
          _renderHook = (0, _react2.renderHook)(function () {
            var _ref5 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
              _ref5$debounceMs = _ref5.debounceMs,
              debounceMs = _ref5$debounceMs === void 0 ? 200 : _ref5$debounceMs;
            return (0, _useDebouncedCallback["default"])(fn, [], debounceMs, _unit.debounce);
          }), result = _renderHook.result, rerender = _renderHook.rerender;
          debouncedCallback = result.current;
          rerender({
            debounceMs: 500
          });
          // Callback was changed when debounce delay changed
          expect(result.current).not.toBe(debouncedCallback);
          _context4.next = 7;
          return (0, _react3.act)(/*#__PURE__*/(0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee3() {
            return _regenerator["default"].wrap(function _callee3$(_context3) {
              while (1) switch (_context3.prev = _context3.next) {
                case 0:
                  return _context3.abrupt("return", result.current());
                case 1:
                case "end":
                  return _context3.stop();
              }
            }, _callee3);
          })));
        case 7:
          vi.advanceTimersByTime(200);
          // debounce duration is now longer
          expect(fn).not.toHaveBeenCalled();
          vi.advanceTimersByTime(300);
          expect(fn).toHaveBeenCalled();
        case 11:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  })));
});