"use strict";

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

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

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

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

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

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _immutable = require("immutable");

var _componentWithOutsideClick = _interopRequireDefault(require("../componentWithOutsideClick"));

var _mouseEventOn = _interopRequireDefault(require("../../../../../../tests/util/mouseEventOn"));

var _PlainComponent2 = _interopRequireDefault(require("../../../../../../tests/util/PlainComponent"));

var _reduxStore = require("../../../../../reduxStore");

describe('componentWithOutsideClick', function () {
  var outsideClickStub = sinon.stub();

  var ComposedComponent =
  /*#__PURE__*/
  function (_PlainComponent) {
    (0, _inherits2.default)(ComposedComponent, _PlainComponent);

    function ComposedComponent() {
      (0, _classCallCheck2.default)(this, ComposedComponent);
      return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(ComposedComponent).apply(this, arguments));
    }

    (0, _createClass2.default)(ComposedComponent, [{
      key: "clickedOutside",
      value: function clickedOutside() {
        outsideClickStub();
      }
    }]);
    return ComposedComponent;
  }(_PlainComponent2.default);

  var ExtendedComponent = (0, _componentWithOutsideClick.default)(ComposedComponent);
  var testbed = createTestbed(ExtendedComponent, {});
  var outerElement = document.createElement('div');
  beforeEach(function () {
    document.body.appendChild(outerElement);
  });
  afterEach(function () {
    outsideClickStub.resetHistory();
    outerElement.remove();
  });
  it('should render a child component', function () {
    testbed.render();
    var component = testbed.findChildByType(ExtendedComponent);
    expect(component).to.not.be.undefined();
  });
  it('should fire clickedOutside with a click outside the component', function () {
    testbed.render();
    (0, _mouseEventOn.default)(outerElement, 'click');
    expect(outsideClickStub.called).to.be.true();
  });
  it('should not fire clickedOutside when ignoreClickOutsideIf returns true', function () {
    ComposedComponent.prototype.ignoreClickOutsideIf = function () {
      return true;
    };

    testbed.render();
    (0, _mouseEventOn.default)(outerElement, 'click');
    expect(outsideClickStub.called).to.be.false();
    ComposedComponent.prototype.ignoreClickOutsideIf = null;
  });
  it('should not fire clickedOutside when modal is open', function () {
    (0, _reduxStore.setFakeStore)((0, _immutable.fromJS)({
      modal: {
        isOpen: true
      }
    }));

    ComposedComponent.prototype.ignoreClickOutsideIf = function () {
      return false;
    };

    testbed.render();
    (0, _mouseEventOn.default)(outerElement, 'click');
    expect(outsideClickStub.called).to.be.false();
    ComposedComponent.prototype.ignoreClickOutsideIf = null;
    (0, _reduxStore.unsetFakeStore)();
  });
  it('should not fire clickedOutside with a click inside the component', function () {
    testbed.render();
    var component = testbed.findChildByType(ExtendedComponent);

    _testUtils.default.Simulate.click(_reactDom.default.findDOMNode(component));

    expect(outsideClickStub.called).to.be.false();
  });
  it('click listener should go away once unmounted', function () {
    testbed.render();

    _reactDom.default.unmountComponentAtNode(testbed.rootNode);

    (0, _mouseEventOn.default)(outerElement, 'click');
    expect(outsideClickStub.called).to.be.false();
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});