import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import useClickAway from '..';

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

  var ref = /*#__PURE__*/React.createRef();
  useClickAway(ref, onClickAway);
  return /*#__PURE__*/React.createElement("div", rest, /*#__PURE__*/React.createElement("div", {
    title: "child1",
    ref: ref
  }), /*#__PURE__*/React.createElement("div", {
    title: "child2"
  }));
};

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

  var ref = /*#__PURE__*/React.createRef();
  var ref1 = /*#__PURE__*/React.createRef();
  useClickAway([ref, ref1], onClickAway);
  return /*#__PURE__*/React.createElement("div", rest, /*#__PURE__*/React.createElement("div", {
    title: "child1",
    ref: ref
  }), /*#__PURE__*/React.createElement("div", {
    title: "child2",
    ref: ref1
  }), /*#__PURE__*/React.createElement("div", {
    title: "child3"
  }));
};

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

    fireEvent.click(screen.getByTitle('child2'));
    expect(fn).toBeCalledTimes(1); // trigger click away twice

    fireEvent.click(screen.getByTitle('child2'));
    expect(fn).toBeCalledTimes(2); // trigger click away three times

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

    fireEvent.click(screen.getByTitle('child1'));
    expect(fn).toBeCalledTimes(0); // trigger click in targets

    fireEvent.click(screen.getByTitle('child2'));
    expect(fn).toBeCalledTimes(0); // trigger click away

    fireEvent.click(screen.getByTitle('child3'));
    expect(fn).toBeCalledTimes(1); // trigger click away

    fireEvent.click(screen.getByTitle('child3'));
    expect(fn).toBeCalledTimes(2);
  });
});