"use strict";

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

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

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

var _button = _interopRequireDefault(require("../../button"));

var _utils = require("../../cascader-view/utils");

jest.useFakeTimers();
var District = [{
  value: '340000',
  label: '安徽省',
  children: [{
    value: '340800',
    label: '安庆市',
    children: [{
      value: '340803',
      label: '大观区',
      children: []
    }, {
      value: '340822',
      label: '怀宁县',
      children: []
    }, {
      value: '340882',
      label: '其它区',
      children: []
    }]
  }]
}, {
  value: '310000',
  label: '上海',
  children: [{
    value: '310100',
    label: '上海市',
    children: [{
      value: '310113',
      label: '宝山区',
      children: []
    }, {
      value: '310105',
      label: '长宁区',
      children: []
    }, {
      value: '310230',
      label: '崇明县',
      children: []
    }, {
      value: '310152',
      label: '川沙区',
      children: []
    }]
  }]
}];
describe('Cascader', function () {
  it('renders correctly if visible is true', function () {
    var onChange = jest.fn();

    var _render = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
      defaultValue: [],
      value: [],
      cancelText: "\u53D6\u6D88",
      confirmText: "\u786E\u5B9A",
      visible: true,
      itemRender: function itemRender(data) {
        return data.label;
      },
      dataSource: District,
      onChange: onChange,
      mountContainer: false
    })),
        container = _render.container;

    expect(container).toMatchSnapshot();
  });
  it('handle props click', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var onCancel, onConfirm, value, _render2, container, getByText, items, mask;

    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            onCancel = jest.fn();
            onConfirm = jest.fn();
            value = ['340000', '340800', '340803'];
            _render2 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
              defaultValue: [],
              value: value,
              cancelText: "\u53D6\u6D88",
              confirmText: "\u786E\u5B9A",
              visible: true,
              maskClosable: true,
              dataSource: District,
              onCancel: onCancel,
              onConfirm: onConfirm,
              mountContainer: false
            })), container = _render2.container, getByText = _render2.getByText;

            _react2.fireEvent.click(getByText('确定'));

            items = (0, _utils.parseItems)(District, value);
            expect(onConfirm).toHaveBeenCalledWith(value, items);

            _react2.fireEvent.click(getByText('取消'));

            expect(onCancel).toBeCalled();
            mask = container.querySelector('.za-mask');

            _react2.fireEvent.click(mask);

            expect(onCancel).toBeCalled();

          case 12:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('handle onChange', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
    var onChange, _render3, container, currentValue, items;

    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            onChange = jest.fn();
            _render3 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, {
              "data-testid": "root",
              defaultValue: [],
              value: [],
              cancelText: "\u53D6\u6D88",
              confirmText: "\u786E\u5B9A",
              visible: true,
              dataSource: District,
              onChange: onChange,
              mountContainer: false
            })), container = _render3.container;

            _react2.fireEvent.click(_react2.screen.getAllByDisplayValue('340000')[0]);

            _context2.next = 5;
            return _react2.screen.findByText('安庆市');

          case 5:
            expect(container).toMatchSnapshot();

            _react2.fireEvent.click(_react2.screen.getAllByDisplayValue('340800')[0]);

            _context2.next = 9;
            return _react2.screen.findByText('大观区');

          case 9:
            expect(container).toMatchSnapshot();

            _react2.fireEvent.click(_react2.screen.getAllByDisplayValue('340803')[0]);

            expect(container).toMatchSnapshot();
            currentValue = ['340000', '340800', '340803'];
            items = (0, _utils.parseItems)(District, currentValue);
            expect(onChange).toBeCalledTimes(3);
            expect(onChange).toHaveBeenCalledWith(currentValue, items);

          case 16:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('static method prompt', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
    var cancel, confirm, CASCADE_DATA, _render4, getByText, currentValue, items;

    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            cancel = jest.fn();
            confirm = jest.fn();
            CASCADE_DATA = [{
              value: '1',
              label: '北京市',
              children: [{
                value: '11',
                label: '海淀区',
                children: []
              }, {
                value: '12',
                label: '西城区',
                children: []
              }]
            }, {
              value: '2',
              label: '上海市',
              children: [{
                value: '21',
                label: '杨浦区',
                children: []
              }, {
                value: '22',
                label: '静安区',
                children: []
              }]
            }];
            _render4 = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(_button.default, {
              onClick: function onClick() {
                _index.default.prompt({
                  defaultValue: ['1', '12'],
                  cancelText: "cancel",
                  confirmText: "confirm",
                  dataSource: CASCADE_DATA,
                  onCancel: cancel,
                  onConfirm: confirm
                });
              }
            }, "cascader prompt")), getByText = _render4.getByText;

            _react2.fireEvent.click(getByText('cascader prompt'));

            _react2.fireEvent.click(_react2.screen.getAllByDisplayValue('2')[0]);

            _context3.next = 8;
            return _react2.screen.findByText('杨浦区');

          case 8:
            _react2.fireEvent.click(getByText('confirm'));

            currentValue = ['2'];
            items = (0, _utils.parseItems)(CASCADE_DATA, currentValue);
            expect(confirm).toHaveBeenCalledWith(currentValue, items);

            _react2.fireEvent.click(getByText('cancel'));

            expect(cancel).toHaveBeenCalled();

          case 14:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
});