"use strict";

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

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

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

var _Trigger = _interopRequireDefault(require("../Trigger"));

describe('TriggerComponent', function () {
  afterEach(function () {
    _Trigger.default.instanceList = [];
    jest.restoreAllMocks();
  });
  describe('Props initialize the TriggerComponent', function () {
    it('instance list of Trigger component should be empty if visible prop is false', function () {
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: false,
        disabled: false
      }));
      expect(_Trigger.default.count).toBe(1);
      expect(_Trigger.default.instanceList).toEqual([]);
    });
    it('instance list of Trigger component should be empty if onClose prop is null', function () {
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: true,
        disabled: true
      }));
      expect(_Trigger.default.instanceList).toEqual([]);
    });
    it('instance list of Trigger component should not be empty if disable prop is true', function () {
      var mOnClose = jest.fn();
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: true,
        disabled: true,
        onClose: mOnClose
      }));
      expect(_Trigger.default.instanceList).toEqual([mOnClose]);
      expect(mOnClose).not.toHaveBeenCalled();
    });
    it('should push onClose function to instance list if visible is true and the onClose function does not exist in instance list', function () {
      var mOnClose = jest.fn();
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: true,
        disabled: false,
        onClose: mOnClose
      }));
      expect(_Trigger.default.instanceList).toEqual([mOnClose]);
      expect(_Trigger.default.count).toBe(1);
      expect(mOnClose).not.toHaveBeenCalled();
    });
  });
  describe('Black-box: onKeydown function was triggered', function () {
    it('should handle escape keyboard event using the last handler of instance list', function () {
      var _Trigger$instanceList;

      var mOnClose = jest.fn();
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: true,
        disabled: false,
        onClose: mOnClose
      }));
      expect(_Trigger.default.count).toBe(1);
      expect(mOnClose).not.toHaveBeenCalled();
      expect(_Trigger.default.instanceList).toEqual([mOnClose]);
      expect((_Trigger$instanceList = _Trigger.default.instanceList[0]) === null || _Trigger$instanceList === void 0 ? void 0 : _Trigger$instanceList.disabled).toBeFalsy();

      _react2.fireEvent.keyDown(document.body, {
        key: 'Escape',
        code: 'Escape'
      });

      expect(_Trigger.default.count).toBe(1);
      expect(mOnClose).toHaveBeenCalled();
      expect(mOnClose).toBeCalledTimes(1);
    });
    it('should do nothing if last handler is disabled', function () {
      var mOnClose = jest.fn();
      (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
        visible: true,
        disabled: true,
        onClose: mOnClose
      }));
      expect(_Trigger.default.instanceList).toEqual([mOnClose]);

      _react2.fireEvent.keyDown(document.body, {
        key: 'Escape',
        code: 'Escape'
      });

      expect(mOnClose).not.toBeCalled();
    });
  });
  it('should add keydown event listener for document.body', function () {
    var mOnClose = jest.fn();
    var addEventListenerSpy = jest.spyOn(document.body, 'addEventListener');
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(_Trigger.default.count).toBe(1);
    expect(addEventListenerSpy).toBeCalledTimes(1);
  });
  it('should increase the counter in case of multiple trigger components', function () {
    var mOnClose = jest.fn();
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(_Trigger.default.count).toBe(1);
    (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(_Trigger.default.count).toBe(2);
  });
  it('should remove keydown event handler from document.body and decrease counter', function () {
    var mOnClose = jest.fn();
    var removeEventListenerSpy = jest.spyOn(document.body, 'removeEventListener');
    var wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_Trigger.default, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(_Trigger.default.instanceList).toEqual([mOnClose]);
    expect(_Trigger.default.count).toBe(1);
    wrapper.unmount();

    _react2.fireEvent.keyDown(document.body, {
      key: 'Escape',
      code: 'Escape'
    });

    expect(mOnClose).not.toBeCalled();
    expect(removeEventListenerSpy).toBeCalledTimes(1);
    expect(_Trigger.default.instanceList).toEqual([]);
    expect(_Trigger.default.count).toBe(0);
  });
});