"use strict";

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

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

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

require("@testing-library/jest-dom");

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

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

var _ = _interopRequireDefault(require(".."));

describe('Modal', function () {
  it('renders correctly', function () {
    var props = {
      onMaskClick: jest.fn(),
      onClose: jest.fn(),
      closable: true,
      title: '标题',
      visible: true
    };
    var wrapper = (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_.default, props, "foo"));
    expect(wrapper.asFragment()).toMatchSnapshot();
  });
  it('onClose', function () {
    var onClose = jest.fn();
    (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_.default, {
      visible: true,
      title: "\u6807\u9898",
      closable: true,
      onClose: onClose
    }, "\u6A21\u6001\u6846\u5185\u5BB9"));
    var close = document.body.querySelector('.za-modal__close');

    _react.fireEvent.click(close);

    expect(onClose).toBeCalled();
  });
  it('click dialog', function () {
    var onClose = jest.fn();
    (0, _react.render)( /*#__PURE__*/_react2.default.createElement(_.default, {
      visible: true,
      maskClosable: true,
      onClose: onClose
    }, "foo"));
    var mask = document.body.querySelector('.za-mask');

    _react.fireEvent.click(mask);

    expect(onClose).toBeCalled();
  });
  it('alert', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() {
    var confirm, _render, getByText;

    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            confirm = jest.fn();
            _render = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("button", {
              onClick: function onClick() {
                _.default.alert({
                  title: '警告框标题',
                  content: '这里是警告框的内容部分',
                  onConfirm: confirm
                });
              }
            }, "alert")), getByText = _render.getByText;

            _react.fireEvent.click(getByText('alert'));

            _context.next = 5;
            return (0, _react.waitFor)(function () {
              _react.screen.getByText('警告框标题');
            });

          case 5:
            expect(getByText('警告框标题')).toBeInTheDocument();

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

            _context.next = 9;
            return (0, _react.waitFor)(function () {
              expect(confirm).toBeCalled();
            });

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('alert confirm mock return false', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
    var confirm, _render2, getByText;

    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            confirm = jest.fn().mockReturnValue(false);
            _render2 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("button", {
              onClick: function onClick() {
                _.default.alert({
                  title: '警告框标题1',
                  content: '这里是警告框的内容部分',
                  onConfirm: confirm,
                  confirmText: '确定1'
                });
              }
            }, "alert confirm false")), getByText = _render2.getByText;

            _react.fireEvent.click(getByText('alert confirm false'));

            _context2.next = 5;
            return (0, _react.waitFor)(function () {
              _react.screen.getByText('警告框标题1');
            });

          case 5:
            _react.fireEvent.click(getByText('确定1'));

            _context2.next = 8;
            return (0, _react.waitFor)(function () {
              expect(getByText('警告框标题1')).toBeInTheDocument();
            });

          case 8:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('confirm', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() {
    var confirm, cancel, _render3, getByText;

    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            confirm = jest.fn();
            cancel = jest.fn();
            _render3 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("button", {
              onClick: function onClick() {
                _.default.confirm({
                  title: '确认信息',
                  content: '这里是确认框的内容部分',
                  onConfirm: confirm,
                  onCancel: cancel,
                  confirmText: 'confirm',
                  cancelText: 'cancel'
                });
              }
            }, "modal confirm")), getByText = _render3.getByText;

            _react.fireEvent.click(getByText('modal confirm'));

            _context3.next = 6;
            return (0, _react.waitFor)(function () {
              _react.screen.getByText('确认信息');
            });

          case 6:
            _react.fireEvent.click(getByText('confirm'));

            _context3.next = 9;
            return (0, _react.waitFor)(function () {
              expect(confirm).toBeCalled();
            });

          case 9:
            _react.fireEvent.click(getByText('cancel'));

            _context3.next = 12;
            return (0, _react.waitFor)(function () {
              expect(cancel).toBeCalled();
            });

          case 12:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it('confirm onConfirm onCancel return false', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee5() {
    var confirm, cancel, fn, _render4, getByText;

    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            confirm = jest.fn().mockReturnValue(false);
            cancel = jest.fn().mockReturnValue(false);
            fn = jest.fn();
            _render4 = (0, _react.render)( /*#__PURE__*/_react2.default.createElement("button", {
              onClick: /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4() {
                return _regenerator.default.wrap(function _callee4$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        _context4.next = 2;
                        return _.default.confirm({
                          title: '确认信息1',
                          content: '这里是确认框的内容部分1',
                          onConfirm: confirm,
                          onCancel: cancel,
                          confirmText: 'confirm false',
                          cancelText: 'cancel false'
                        });

                      case 2:
                        fn();

                      case 3:
                      case "end":
                        return _context4.stop();
                    }
                  }
                }, _callee4);
              }))
            }, "modal confirm false")), getByText = _render4.getByText;

            _react.fireEvent.click(getByText('modal confirm false'));

            _context5.next = 7;
            return (0, _react.waitFor)(function () {
              _react.screen.getByText('确认信息1');
            });

          case 7:
            _react.fireEvent.click(getByText('confirm false'));

            _react.fireEvent.click(getByText('cancel false'));

            _context5.next = 11;
            return (0, _react.waitFor)(function () {
              expect(fn).toBeCalledTimes(0);
            });

          case 11:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  })));
});