"use strict";

var _enzyme = require("enzyme");

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

var _Item = _interopRequireDefault(require("../../Item"));

var _VirtualList = _interopRequireDefault(require("../VirtualList"));

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

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _objectWithoutProperties(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]; } 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; }

describe('VirtualList', function () {
  var clientSize, dataSize, getScrollTo, handlerOnScroll, handlerOnScrollStart, handlerOnScrollStop, items, myScrollTo, onScrollCount, onScrollStartCount, onScrollStopCount, renderItem, resultScrollLeft, resultScrollTop;
  beforeEach(function () {
    clientSize = {
      clientWidth: 1280,
      clientHeight: 720
    };
    dataSize = 100;
    items = [];
    onScrollCount = 0;
    onScrollStartCount = 0;
    onScrollStopCount = 0;
    resultScrollLeft = 0;
    resultScrollTop = 0;

    getScrollTo = function getScrollTo(scrollTo) {
      myScrollTo = scrollTo;
    };

    handlerOnScroll = function handlerOnScroll() {
      onScrollCount++;
    };

    handlerOnScrollStart = function handlerOnScrollStart() {
      onScrollStartCount++;
    };

    handlerOnScrollStop = function handlerOnScrollStop(e) {
      onScrollStopCount++;
      resultScrollLeft = e.scrollLeft;
      resultScrollTop = e.scrollTop;
    };

    renderItem = function renderItem(_ref) {
      var index = _ref.index,
          rest = _objectWithoutProperties(_ref, ["index"]);

      // eslint-disable-line enact/display-name, enact/prop-types
      return _react.default.createElement(_Item.default, rest, items[index].name);
    };

    for (var i = 0; i < dataSize; i++) {
      items.push({
        name: 'Account ' + i
      });
    }
  });
  afterEach(function () {
    clientSize = null;
    dataSize = null;
    getScrollTo = null;
    handlerOnScroll = null;
    handlerOnScrollStart = null;
    handlerOnScrollStop = null;
    items = null;
    myScrollTo = null;
    onScrollCount = null;
    onScrollStartCount = null;
    onScrollStopCount = null;
    renderItem = null;
    resultScrollLeft = null;
    resultScrollTop = null;
  });
  it('should render a list of \'items\'', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: 30
    }));
    var expected = 'Account 0';
    var actual = subject.find('[data-index]').at(0).text();
    expect(actual).to.equal(expected);
  });
  it('should render (clientHeight / itemHeight + overhang) items', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
      clientSize: clientSize,
      dataSize: dataSize,
      itemRenderer: renderItem,
      itemSize: 30
    }));
    var expected = 27; // 720 / 30 + 3

    var actual = subject.find('Item[data-index]').length;
    expect(actual).to.equal(expected);
  });
  it('should render only one scrollbar', function () {
    var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
      clientSize: clientSize,
      dataSize: dataSize,
      direction: "horizontal",
      itemRenderer: renderItem,
      itemSize: 30
    }));
    var expected = 1;
    var actual = subject.find('Scrollbar').length;
    expect(actual).to.equal(expected);
  });
  describe('ScrollTo', function () {
    it('should scroll to the specific item of a given index with scrollTo', function () {
      (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        cbScrollTo: getScrollTo,
        clientSize: clientSize,
        dataSize: dataSize,
        itemRenderer: renderItem,
        itemSize: 30,
        onScrollStop: handlerOnScrollStop
      }));
      myScrollTo({
        index: 10,
        animate: false
      });
      var expected = 300;
      var actual = resultScrollTop;
      expect(actual).to.equal(expected);
    });
    it('should scroll to the given \'x\' position with scrollTo', function () {
      (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        cbScrollTo: getScrollTo,
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        itemRenderer: renderItem,
        itemSize: 30,
        onScrollStop: handlerOnScrollStop
      }));
      myScrollTo({
        position: {
          x: 100
        },
        animate: false
      });
      var expected = 100;
      var actual = resultScrollLeft;
      expect(actual).to.equal(expected);
    });
    it('should scroll to the given \'y\' position with scrollTo', function () {
      (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        cbScrollTo: getScrollTo,
        clientSize: clientSize,
        dataSize: dataSize,
        itemRenderer: renderItem,
        itemSize: 30,
        onScrollStop: handlerOnScrollStop
      }));
      myScrollTo({
        position: {
          y: 100
        },
        animate: false
      });
      var expected = 100;
      var actual = resultScrollTop;
      expect(actual).to.equal(expected);
    });
    describe('scroll events', function () {
      it('should call onScrollStart once', function () {
        (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
          cbScrollTo: getScrollTo,
          clientSize: clientSize,
          dataSize: dataSize,
          itemRenderer: renderItem,
          itemSize: 30,
          onScrollStart: handlerOnScrollStart
        }));
        myScrollTo({
          position: {
            y: 100
          },
          animate: false
        });
        var expected = 1;
        var actual = onScrollStartCount;
        expect(actual).to.equal(expected);
      });
      it('should call onScroll once', function () {
        (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
          cbScrollTo: getScrollTo,
          clientSize: clientSize,
          dataSize: dataSize,
          itemRenderer: renderItem,
          itemSize: 30,
          onScroll: handlerOnScroll
        }));
        myScrollTo({
          position: {
            y: 100
          },
          animate: false
        });
        var expected = 1;
        var actual = onScrollCount;
        expect(actual).to.equal(expected);
      });
      it('should call onScrollStop once', function () {
        (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
          cbScrollTo: getScrollTo,
          clientSize: clientSize,
          dataSize: dataSize,
          itemRenderer: renderItem,
          itemSize: 30,
          onScrollStop: handlerOnScrollStop
        }));
        myScrollTo({
          position: {
            y: 100
          },
          animate: false
        });
        var expected = 1;
        var actual = onScrollStopCount;
        expect(actual).to.equal(expected);
      });
    });
  });
  describe('Adding an item', function () {
    it('should render an added item named \'Password 0\' as the first item', function (done) {
      var itemArray = [{
        name: 'A'
      }, {
        name: 'B'
      }, {
        name: 'C'
      }];

      var renderItemArray = function renderItemArray(_ref2) {
        var index = _ref2.index,
            rest = _objectWithoutProperties(_ref2, ["index"]);

        // eslint-disable-line enact/display-name, enact/prop-types, react/jsx-no-bind
        return _react.default.createElement("div", _extends({}, rest, {
          id: 'item' + index
        }), itemArray[index].name);
      };

      var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        clientSize: clientSize,
        dataSize: itemArray.length,
        itemRenderer: renderItemArray // eslint-disable-line react/jsx-no-bind
        ,
        itemSize: 30
      }));
      itemArray.unshift({
        name: 'Password 0'
      });
      subject.setProps({
        dataSize: itemArray.length
      });
      setTimeout(function () {
        var expected = itemArray[0].name;
        var actual = subject.find('[data-index]').at(0).text();
        expect(actual).to.equal(expected);
        done();
      }, 0);
    });
  });
  describe('Scrollbar accessibility', function () {
    it('should set "aria-label" to previous scroll button in the horizontal scrollbar', function () {
      var label = 'custom button aria label';
      var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        scrollLeftAriaLabel: label,
        itemRenderer: renderItem,
        itemSize: 30
      }));
      var expected = label;
      var actual = subject.find('ScrollButton').at(0).prop('aria-label');
      expect(actual).to.equal(expected);
    });
    it('should set "aria-label" to next scroll button in the horizontal scrollbar', function () {
      var label = 'custom button aria label';
      var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "horizontal",
        scrollRightAriaLabel: label,
        itemRenderer: renderItem,
        itemSize: 30
      }));
      var expected = label;
      var actual = subject.find('ScrollButton').at(1).prop('aria-label');
      expect(actual).to.equal(expected);
    });
    it('should set "aria-label" to previous scroll button in the vertical scrollbar', function () {
      var label = 'custom button aria label';
      var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "vertical",
        itemRenderer: renderItem,
        itemSize: 30,
        scrollUpAriaLabel: label
      }));
      var expected = label;
      var actual = subject.find('ScrollButton').at(0).prop('aria-label');
      expect(actual).to.equal(expected);
    });
    it('should set "aria-label" to next scroll button in the vertical scrollbar', function () {
      var label = 'custom button aria label';
      var subject = (0, _enzyme.mount)(_react.default.createElement(_VirtualList.default, {
        clientSize: clientSize,
        dataSize: dataSize,
        direction: "vertical",
        itemRenderer: renderItem,
        itemSize: 30,
        scrollDownAriaLabel: label
      }));
      var expected = label;
      var actual = subject.find('ScrollButton').at(1).prop('aria-label');
      expect(actual).to.equal(expected);
    });
  });
});