"use strict";

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

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

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

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

var _image = _interopRequireDefault(require("../../image"));

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

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var originalOffsetWidth = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetWidth');
var originalOffsetHeight = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'offsetHeight');
var IMGLIST = ['https://static.zhongan.com/website/health/zarm/images/banners/1.png', 'https://static.zhongan.com/website/health/zarm/images/banners/2.png', 'https://static.zhongan.com/website/health/zarm/images/banners/3.png'];

var contentRender = function contentRender() {
  return IMGLIST.map(function (item, i) {
    return /*#__PURE__*/_react2.default.createElement("div", {
      className: "carousel__item__pic",
      key: +i
    }, /*#__PURE__*/_react2.default.createElement(_image.default, {
      src: item,
      alt: "",
      draggable: false
    }));
  });
};

var createCarousel = function createCarousel(props) {
  var childrenLen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 3;
  var ITEMS = Array.from({
    length: childrenLen
  }).map(function (_v, i) {
    return i;
  });
  return /*#__PURE__*/_react2.default.createElement(_index.default, props, ITEMS.map(function (item, index) {
    return /*#__PURE__*/_react2.default.createElement("div", {
      key: +index
    }, item);
  }));
};

describe('Carousel', function () {
  it('base render correctly', function () {
    var wrapper = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("div", null, createCarousel(), createCarousel({}, 0), createCarousel({
      activeIndex: 1
    }, 0), createCarousel({}, 1)));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('style render correctly', function () {
    var style = {
      background: 'red'
    };
    var wrapper = (0, _react.render)(createCarousel({
      style: style
    }, 0));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('prefixCls render correctly', function () {
    var prefixCls = 'za-test';
    var wrapper = (0, _react.render)(createCarousel({
      prefixCls: prefixCls
    }, 1));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('className render correctly', function () {
    var className = 'za-wrapper-test';
    var wrapper = (0, _react.render)(createCarousel({
      className: className
    }, 0));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('height render correctly', function () {
    var wrapper = (0, _react.render)(createCarousel({
      height: 150,
      direction: 'vertical'
    }));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('pagination render correctly', function () {
    var wrapper = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("div", null, createCarousel({
      showPagination: true
    }), createCarousel({
      showPagination: false
    })));
    expect(wrapper.asFragment()).toMatchSnapshot();
  }); // it('activeIndex', () => {
  //   const wrapper = mount(createCarousel({ activeIndex: 1 }));
  //   expect(wrapper.state('activeIndex')).toEqual(1);
  // });

  it('swipeable', function () {
    var wrapper = (0, _react.render)(createCarousel({
      swipeable: false
    }));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('autoPlay', function () {
    jest.useFakeTimers();
    var onChange = jest.fn();
    var animationDuration = 200;
    var autoPlayIntervalTime = 1000;
    var props = {
      autoPlay: true,
      animationDuration: animationDuration,
      autoPlayIntervalTime: autoPlayIntervalTime
    };
    (0, _react.render)(createCarousel(_objectSpread(_objectSpread({}, props), {}, {
      onChange: onChange
    })));
    (0, _react.act)(function () {
      jest.advanceTimersByTime(autoPlayIntervalTime);
    });
    expect(onChange).toBeCalledWith(1);
  });
  beforeEach(function () {
    Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
      configurable: true,
      value: 375
    });
    Object.defineProperty(HTMLElement.prototype, 'offsetWidth', {
      configurable: true,
      value: 200
    });
  });
  afterAll(function () {
    Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetWidth);
    Object.defineProperty(HTMLElement.prototype, 'offsetHeight', originalOffsetHeight);
  });
  it('swipe change horizontal', function () {
    var onChange = jest.fn();

    var _render = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_index.default, {
      onChange: onChange
    }, contentRender())),
        container = _render.container;

    var element = container.querySelector('.za-carousel');

    _react.fireEvent.mouseDown(element, {
      pointerId: 1,
      clientX: 0,
      clientY: 0,
      buttons: 1
    });

    _react.fireEvent.mouseMove(element, {
      pointerId: 1,
      clientX: -200,
      clientY: 0,
      buttons: 1
    });

    _react.fireEvent.mouseUp(element, {
      pointerId: 1,
      clientX: -275
    });

    expect(onChange).toBeCalledTimes(1);
  });
  it('swipe change vertical', function () {
    var onChange = jest.fn();

    var _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_index.default, {
      onChange: onChange,
      direction: "vertical",
      loop: true
    }, contentRender())),
        container = _render2.container;

    var element = container.querySelector('.za-carousel');

    _react.fireEvent.mouseDown(element, {
      pointerId: 1,
      clientX: 50,
      clientY: 0,
      buttons: 1
    });

    _react.fireEvent.mouseMove(element, {
      pointerId: 1,
      clientX: 50,
      clientY: -100,
      buttons: 1
    });

    _react.fireEvent.mouseUp(element, {
      pointerId: 1,
      clientX: 50,
      clientY: -350
    });

    expect(onChange).toBeCalledTimes(1);
  });
  it('transitionend event', function () {
    var onChangeEnd = jest.fn();

    var _render3 = (0, _react.render)(createCarousel({
      onChangeEnd: onChangeEnd
    })),
        container = _render3.container;

    _react.fireEvent.click(container.getElementsByClassName('za-carousel__pagination__item')[1]);

    _react.fireEvent.transitionEnd(container.getElementsByClassName('za-carousel__items')[0]);

    expect(onChangeEnd).toBeCalledWith(1);
  });
});