"use strict";

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

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

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

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

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

var TestDemo = function TestDemo(_ref) {
  var onClickAway = _ref.onClickAway,
      rest = (0, _objectWithoutProperties2.default)(_ref, ["onClickAway"]);

  var ref = /*#__PURE__*/_react2.default.createRef();

  (0, _.default)(ref, onClickAway);
  return /*#__PURE__*/_react2.default.createElement("div", rest, /*#__PURE__*/_react2.default.createElement("div", {
    title: "child1",
    ref: ref
  }), /*#__PURE__*/_react2.default.createElement("div", {
    title: "child2"
  }));
};

var TestDemo2 = function TestDemo2(_ref2) {
  var onClickAway = _ref2.onClickAway,
      rest = (0, _objectWithoutProperties2.default)(_ref2, ["onClickAway"]);

  var ref = /*#__PURE__*/_react2.default.createRef();

  var ref1 = /*#__PURE__*/_react2.default.createRef();

  (0, _.default)([ref, ref1], onClickAway);
  return /*#__PURE__*/_react2.default.createElement("div", rest, /*#__PURE__*/_react2.default.createElement("div", {
    title: "child1",
    ref: ref
  }), /*#__PURE__*/_react2.default.createElement("div", {
    title: "child2",
    ref: ref1
  }), /*#__PURE__*/_react2.default.createElement("div", {
    title: "child3"
  }));
};

var title = 'useClickAway';
describe('useClickAway', function () {
  test('useClickAway with one target', function () {
    var fn = jest.fn();
    (0, _react.render)( /*#__PURE__*/_react2.default.createElement(TestDemo, {
      title: title,
      onClickAway: fn
    }));
    expect(fn).toBeCalledTimes(0); // trigger click away once

    _react.fireEvent.click(_react.screen.getByTitle('child2'));

    expect(fn).toBeCalledTimes(1); // trigger click away twice

    _react.fireEvent.click(_react.screen.getByTitle('child2'));

    expect(fn).toBeCalledTimes(2); // trigger click away three times

    _react.fireEvent.click(_react.screen.getByTitle('child2'));

    expect(fn).toBeCalledTimes(3);
  });
  test('useClickAway with more targets', function () {
    var fn = jest.fn();
    (0, _react.render)( /*#__PURE__*/_react2.default.createElement(TestDemo2, {
      title: title,
      onClickAway: fn
    }));
    expect(fn).toBeCalledTimes(0); // trigger click in targets

    _react.fireEvent.click(_react.screen.getByTitle('child1'));

    expect(fn).toBeCalledTimes(0); // trigger click in targets

    _react.fireEvent.click(_react.screen.getByTitle('child2'));

    expect(fn).toBeCalledTimes(0); // trigger click away

    _react.fireEvent.click(_react.screen.getByTitle('child3'));

    expect(fn).toBeCalledTimes(1); // trigger click away

    _react.fireEvent.click(_react.screen.getByTitle('child3'));

    expect(fn).toBeCalledTimes(2);
  });
});