"use strict";

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

var _index = _interopRequireDefault(require("../index"));

var _enzyme = _interopRequireWildcard(require("enzyme"));

var _Select = _interopRequireDefault(require("../../Select"));

var _Button = require("../../Button");

var _enzymeAdapterReact = _interopRequireDefault(require("enzyme-adapter-react-16"));

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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

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; }

_enzyme.default.configure({
  adapter: new _enzymeAdapterReact.default()
});

var numberOfRows = 200;

var makeProps = function makeProps() {
  var overrides = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return _objectSpread({
    numberOfRows: numberOfRows,
    rowsPerPage: 10,
    page: 1
  }, overrides);
};

var Tester = function Tester(props) {
  var _this = this;

  _classCallCheck(this, Tester);

  _defineProperty(this, "paginationValue", null);

  _defineProperty(this, "page", null);

  _defineProperty(this, "wrapper", null);

  _defineProperty(this, "rowsPerPageSelectWrapper", null);

  _defineProperty(this, "pagingInformationWrapper", null);

  _defineProperty(this, "getRowsPerPageOptions", function () {
    // eslint-disable-next-line no-invalid-this
    return _this.rowsPerPageSelectWrapper.props().options;
  });

  _defineProperty(this, "changeRowsPerPage", function (value) {
    // eslint-disable-next-line no-invalid-this
    var rowsPerPageSelectWrapper = _this.rowsPerPageSelectWrapper;

    var _rowsPerPageSelectWra = rowsPerPageSelectWrapper.props(),
        onChange = _rowsPerPageSelectWra.onChange;

    onChange({
      value: value
    });
  });

  _defineProperty(this, "getTotalRecords", function () {
    // eslint-disable-next-line no-invalid-this
    return _this.paginationTotalRecordsWrapper.text();
  });

  _defineProperty(this, "getRowsPerPageDropdownValue", function () {
    // eslint-disable-next-line no-invalid-this
    return _this.rowsPerPageSelectWrapper.props().value;
  });

  _defineProperty(this, "getPagingInformation", function () {
    // eslint-disable-next-line no-invalid-this
    return _this.pagingInformationWrapper.text();
  });

  _defineProperty(this, "clickOnNextPage", function () {
    // eslint-disable-next-line no-invalid-this
    _this.wrapper.find(_Button.Button).filterWhere(function (node) {
      return node.props().className === 'vs-next-page';
    }).props().onClick();
  });

  _defineProperty(this, "clickOnPreviousPage", function () {
    // eslint-disable-next-line no-invalid-this
    _this.wrapper.find(_Button.Button).filterWhere(function (node) {
      return node.props().className === 'vs-previous-page';
    }).props().onClick();
  });

  _defineProperty(this, "getNextPage", function () {
    // eslint-disable-next-line no-invalid-this
    return _this.page;
  });

  var _onChange = function _onChange(value) {
    // eslint-disable-next-line no-invalid-this
    _this.paginationValue = value;
  };

  var onPageChange = function onPageChange(value) {
    _this.page = value;
  };

  this.wrapper = (0, _enzyme.mount)(_react.default.createElement(_index.default, makeProps(_objectSpread({
    onChange: _onChange,
    onPageChange: onPageChange
  }, props))));
  this.rowsPerPageSelectWrapper = this.wrapper.find(_Select.default).filterWhere(function (node) {
    return node.props().name === 'rows-per-page';
  });
  this.pagingInformationWrapper = this.wrapper.find('div.vs-pagination-paging');
  this.paginationTotalRecordsWrapper = this.wrapper.find('div.vs-pagination-total-records');
};

var defaultOptions = {
  numberOfRows: 20
};

var createTesterWithOptions = function createTesterWithOptions(options) {
  return new Tester(_objectSpread({}, defaultOptions, options));
};

var createTester = function createTester() {
  return createTesterWithOptions({});
};

var createTesterWithNumberOfRows = function createTesterWithNumberOfRows(numberOfRows) {
  return createTesterWithOptions({
    numberOfRows: numberOfRows
  });
};

describe('Pagination', function () {
  it('should display total records', function () {
    var tester = createTesterWithNumberOfRows(27);
    expect(tester.getTotalRecords()).toEqual("27 total records");
  });
  it('should have default value for rows per page', function () {
    var tester = createTester();
    expect(tester.getRowsPerPageDropdownValue()).toEqual({
      value: 10,
      label: '10 per page'
    });
  });
  it('should set rows per page', function () {
    var rowsPerPage = 25;
    var tester = createTesterWithOptions({
      rowsPerPage: rowsPerPage
    });
    expect(tester.getRowsPerPageDropdownValue()).toEqual({
      value: 25,
      label: '25 per page'
    });
  });
  it('should display the rows per page options', function () {
    // given
    var tester = createTester(); // then

    expect(tester.getRowsPerPageOptions()).toEqual([{
      value: 10,
      label: '10 per page'
    }, {
      value: 25,
      label: '25 per page'
    }, {
      value: 50,
      label: '50 per page'
    }]);
  });
  it('should display paging information', function () {
    // given
    var numberOfRows = 50; // when

    var tester = createTesterWithOptions({
      numberOfRows: numberOfRows
    }); // then

    expect(tester.getPagingInformation()).toEqual('1/5');
  });
  it('should display paging information that cant be divided evenly', function () {
    // given
    var numberOfRows = 52; // when

    var tester = createTesterWithOptions({
      numberOfRows: numberOfRows
    }); // then

    expect(tester.getPagingInformation()).toEqual('1/6');
  });
  it('should display paging information for different rowsPerPage', function () {
    // given
    var rowsPerPage = 25;
    var numberOfRows = 100; // when

    var tester = createTesterWithOptions({
      rowsPerPage: rowsPerPage,
      numberOfRows: numberOfRows
    }); // then

    expect(tester.getPagingInformation()).toEqual('1/4');
  });
  it('should display the current page', function () {
    // given
    var numberOfRows = 100;
    var page = 2; // when

    var tester = createTesterWithOptions({
      numberOfRows: numberOfRows,
      page: page
    }); // then

    expect(tester.getPagingInformation()).toEqual('2/10');
  });
  it('should callback with the selecting rowsPerPage value', function () {
    // given
    var tester = createTester(); // when

    tester.changeRowsPerPage(50); // then

    expect(tester.paginationValue).toEqual({
      rowsPerPage: 50,
      page: 1
    });
  });
  it('should onPageChange with next page', function () {
    // given
    var page = 2;
    var rowsPerPage = 25;
    var numberOfRows = 1000; // when

    var tester = createTesterWithOptions({
      page: page,
      rowsPerPage: rowsPerPage,
      numberOfRows: numberOfRows
    });
    tester.clickOnNextPage(); // then

    expect(tester.paginationValue).toEqual({
      page: 3,
      rowsPerPage: 25
    });
  });
  it('should onChange with previous page', function () {
    // given
    var page = 2; // when

    var tester = createTesterWithOptions({
      page: page
    });
    tester.clickOnPreviousPage(); // then

    expect(tester.paginationValue).toEqual({
      page: 1,
      rowsPerPage: 10
    });
  });
  it('should not call onChange when page number is below 1', function () {
    // given
    var page = 1; // when

    var tester = createTesterWithOptions({
      page: page
    });
    tester.clickOnPreviousPage(); // then

    expect(tester.paginationValue).toEqual(null);
  });
  it('should not call onChange when next page number is above the max allowed page number', function () {
    // given
    var page = 5;
    var numberOfRows = 125;
    var rowsPerPage = 25; // when

    var tester = createTesterWithOptions({
      page: page,
      numberOfRows: numberOfRows,
      rowsPerPage: rowsPerPage
    });
    tester.clickOnNextPage(); // then

    expect(tester.paginationValue).toEqual(null);
  });
  it('should change page back to the first page e when I change rowsPerPage', function () {
    // given
    var page = 9;
    var numberOfRows = 100;
    var rowsPerPage = 10; // when

    var tester = createTesterWithOptions({
      page: page,
      numberOfRows: numberOfRows,
      rowsPerPage: rowsPerPage
    });
    tester.changeRowsPerPage(50); // then

    expect(tester.paginationValue).toEqual({
      page: 1,
      rowsPerPage: 50
    });
  });
  it('should not change the page number when I change rowsPerPage and the current page is not greater than the max allowed page', function () {
    // given
    var page = 1;
    var numberOfRows = 100;
    var rowsPerPage = 10; // when

    var tester = createTesterWithOptions({
      page: page,
      numberOfRows: numberOfRows,
      rowsPerPage: rowsPerPage
    });
    tester.changeRowsPerPage(50); // then

    expect(tester.paginationValue).toEqual({
      page: 1,
      rowsPerPage: 50
    });
  });
});