"use strict";

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

var _enzyme = require("enzyme");

var StyledComponents = _interopRequireWildcard(require("../styled-components.js"));

var _index = require("../../button/index.js");

var _pagination = _interopRequireDefault(require("../pagination.js"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var originalAddEventListener = document.addEventListener;
var originalRemoveEventListener = document.removeEventListener;

function getSharedProps() {
  return {
    numPages: 3,
    currentPage: 2
  };
}

describe('Pagination Stateless', function () {
  beforeAll(function () {
    // $FlowFixMe
    document.addEventListener = jest.fn(); // $FlowFixMe

    document.removeEventListener = jest.fn();
  });
  beforeEach(function () {
    jest.clearAllMocks();
  });
  afterAll(function () {
    // $FlowFixMe
    document.addEventListener = originalAddEventListener; // $FlowFixMe

    document.removeEventListener = originalRemoveEventListener;
  });
  Object.keys(StyledComponents).forEach(function (componentName) {
    test("support component override - ".concat(componentName), function () {
      /* eslint-disable react/prop-types */
      var MockComponent = function MockComponent(_ref) {
        var children = _ref.children,
            $kind = _ref.$kind,
            $size = _ref.$size,
            $shape = _ref.$shape,
            $ref = _ref.$ref,
            $disabled = _ref.$disabled,
            $isLoading = _ref.$isLoading,
            $isSelected = _ref.$isSelected,
            initialState = _ref.initialState,
            onItemSelect = _ref.onItemSelect,
            restProps = _objectWithoutProperties(_ref, ["children", "$kind", "$size", "$shape", "$ref", "$disabled", "$isLoading", "$isSelected", "initialState", "onItemSelect"]);

        return _react.default.createElement("div", restProps, children);
      };

      var props = _objectSpread({}, getSharedProps(), {
        overrides: _defineProperty({}, componentName, {
          component: MockComponent,
          props: {
            id: 'prop'
          }
        })
      });

      var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, props));
      component.setState({
        isMenuOpen: true
      });
      expect(component.find(StyledComponents[componentName])).not.toExist();
      expect(component.find(MockComponent)).toExist();
      expect(component.find(MockComponent).prop('id')).toEqual('prop');
    });
  });
  test('dropdown button click', function () {
    var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, getSharedProps()));
    component.find(StyledComponents.DropdownButton).simulate('click');
    expect(component.state('isMenuOpen')).toEqual(true);
    expect(document.addEventListener.mock.calls[0][0]).toEqual('click');
    expect(document.removeEventListener.mock.calls.length).toBe(0);
    component.find(StyledComponents.DropdownButton).simulate('click');
    expect(component.state('isMenuOpen')).toEqual(false);
    expect(document.removeEventListener.mock.calls[0][0]).toEqual('click');
  });
  test('prev button click', function () {
    var props = _objectSpread({}, getSharedProps(), {
      onPageChange: jest.fn(),
      onPrevClick: jest.fn()
    });

    var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, props));
    component.find(_index.Button).first().simulate('click');
    expect(props.onPageChange.mock.calls[0]).toEqual([{
      nextPage: props.currentPage - 1,
      prevPage: props.currentPage
    }]);
    expect(props.onPrevClick.mock.calls.length).toBe(1);
  });
  test('next button click', function () {
    var props = _objectSpread({}, getSharedProps(), {
      onPageChange: jest.fn(),
      onNextClick: jest.fn()
    });

    var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, props));
    component.find(_index.Button).last().simulate('click');
    expect(props.onPageChange.mock.calls[0]).toEqual([{
      nextPage: props.currentPage + 1,
      prevPage: props.currentPage
    }]);
    expect(props.onNextClick.mock.calls.length).toBe(1);
  });
  test('getMenuOptions', function () {
    var props = getSharedProps();
    var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, props));
    expect(component.instance().getMenuOptions(props.numPages)).toEqual([{
      label: 1
    }, {
      label: 2
    }, {
      label: 3
    }]);
  });
  test('select item', function () {
    var props = _objectSpread({}, getSharedProps(), {
      onPageChange: jest.fn()
    });

    var component = (0, _enzyme.mount)(_react.default.createElement(_pagination.default, props));
    component.instance().onDropdownButtonClick = jest.fn();
    component.instance().onMenuItemSelect({
      item: {
        label: 3
      }
    });
    expect(props.onPageChange.mock.calls[0]).toEqual([{
      nextPage: 3,
      prevPage: 2
    }]);
  });
});