import React from 'react';
import { render, fireEvent } from '@testing-library/react';
import Trigger from '../Trigger';
describe('TriggerComponent', function () {
  afterEach(function () {
    Trigger.instanceList = [];
    jest.restoreAllMocks();
  });
  describe('Props initialize the TriggerComponent', function () {
    it('instance list of Trigger component should be empty if visible prop is false', function () {
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: false,
        disabled: false
      }));
      expect(Trigger.count).toBe(1);
      expect(Trigger.instanceList).toEqual([]);
    });
    it('instance list of Trigger component should be empty if onClose prop is null', function () {
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: true,
        disabled: true
      }));
      expect(Trigger.instanceList).toEqual([]);
    });
    it('instance list of Trigger component should not be empty if disable prop is true', function () {
      var mOnClose = jest.fn();
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: true,
        disabled: true,
        onClose: mOnClose
      }));
      expect(Trigger.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();
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: true,
        disabled: false,
        onClose: mOnClose
      }));
      expect(Trigger.instanceList).toEqual([mOnClose]);
      expect(Trigger.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();
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: true,
        disabled: false,
        onClose: mOnClose
      }));
      expect(Trigger.count).toBe(1);
      expect(mOnClose).not.toHaveBeenCalled();
      expect(Trigger.instanceList).toEqual([mOnClose]);
      expect((_Trigger$instanceList = Trigger.instanceList[0]) === null || _Trigger$instanceList === void 0 ? void 0 : _Trigger$instanceList.disabled).toBeFalsy();
      fireEvent.keyDown(document.body, {
        key: 'Escape',
        code: 'Escape'
      });
      expect(Trigger.count).toBe(1);
      expect(mOnClose).toHaveBeenCalled();
      expect(mOnClose).toBeCalledTimes(1);
    });
    it('should do nothing if last handler is disabled', function () {
      var mOnClose = jest.fn();
      render( /*#__PURE__*/React.createElement(Trigger, {
        visible: true,
        disabled: true,
        onClose: mOnClose
      }));
      expect(Trigger.instanceList).toEqual([mOnClose]);
      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');
    render( /*#__PURE__*/React.createElement(Trigger, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(Trigger.count).toBe(1);
    expect(addEventListenerSpy).toBeCalledTimes(1);
  });
  it('should increase the counter in case of multiple trigger components', function () {
    var mOnClose = jest.fn();
    render( /*#__PURE__*/React.createElement(Trigger, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(Trigger.count).toBe(1);
    render( /*#__PURE__*/React.createElement(Trigger, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(Trigger.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 = render( /*#__PURE__*/React.createElement(Trigger, {
      visible: true,
      disabled: false,
      onClose: mOnClose
    }));
    expect(Trigger.instanceList).toEqual([mOnClose]);
    expect(Trigger.count).toBe(1);
    wrapper.unmount();
    fireEvent.keyDown(document.body, {
      key: 'Escape',
      code: 'Escape'
    });
    expect(mOnClose).not.toBeCalled();
    expect(removeEventListenerSpy).toBeCalledTimes(1);
    expect(Trigger.instanceList).toEqual([]);
    expect(Trigger.count).toBe(0);
  });
});