"use strict";

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

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

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

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

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

var _uiTestUtils = require("@instructure/ui-test-utils");

var _locator = _interopRequireDefault(require("@instructure/ui-overlays/lib/components/Modal/locator"));

var _presenter = _interopRequireDefault(require("../presenter"));

describe('Confirmation Modal', function () {
  var render =
  /*#__PURE__*/
  function () {
    var _ref2 = (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var _ref,
          _ref$closeModal,
          closeModal,
          _ref$modalOpen,
          modalOpen,
          options,
          _args = arguments;

      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _ref = _args.length > 0 && _args[0] !== undefined ? _args[0] : {};
              _ref$closeModal = _ref.closeModal, closeModal = _ref$closeModal === void 0 ? Function.prototype : _ref$closeModal, _ref$modalOpen = _ref.modalOpen, modalOpen = _ref$modalOpen === void 0 ? true : _ref$modalOpen, options = (0, _objectWithoutProperties2.default)(_ref, ["closeModal", "modalOpen"]);
              _context.next = 4;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_presenter.default, Object.assign({
                closeModal: closeModal,
                modalOpen: modalOpen
              }, options)));

            case 4:
              return _context.abrupt("return", _context.sent);

            case 5:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function render() {
      return _ref2.apply(this, arguments);
    };
  }();

  describe('modal header', function () {
    it('renders the given title as a label',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var customTitle;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              customTitle = 'Remove Item?';
              _context2.next = 3;
              return render({
                title: customTitle
              });

            case 3:
              _context2.t0 = _uiTestUtils.expect;
              _context2.next = 6;
              return _locator.default.find(":label('".concat(customTitle, "')"));

            case 6:
              _context2.t1 = _context2.sent;
              (0, _context2.t0)(_context2.t1).to.exist();

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
    it('renders the given title',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var customTitle, modal, header;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              customTitle = 'What Item?';
              _context3.next = 3;
              return render({
                title: customTitle
              });

            case 3:
              _context3.next = 5;
              return _locator.default.find();

            case 5:
              modal = _context3.sent;
              _context3.next = 8;
              return modal.findHeader();

            case 8:
              header = _context3.sent;
              (0, _uiTestUtils.expect)(header.text()).to.include(customTitle);

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
    it('renders a default title if none given',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee4() {
      var standardTitle, modal, header;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              standardTitle = 'Confirm';
              _context4.next = 3;
              return render();

            case 3:
              _context4.next = 5;
              return _locator.default.find();

            case 5:
              modal = _context4.sent;
              _context4.next = 8;
              return modal.findHeader();

            case 8:
              header = _context4.sent;
              (0, _uiTestUtils.expect)(header.text()).to.include(standardTitle);

            case 10:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
    it('renders a close button',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee5() {
      var modal, header, closeButton;
      return _regenerator.default.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              _context5.next = 2;
              return render();

            case 2:
              _context5.next = 4;
              return _locator.default.find();

            case 4:
              modal = _context5.sent;
              _context5.next = 7;
              return modal.findHeader();

            case 7:
              header = _context5.sent;
              _context5.next = 10;
              return header.find('button');

            case 10:
              closeButton = _context5.sent;
              (0, _uiTestUtils.expect)(closeButton).to.have.text('Close');

            case 12:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    })));
  });
  describe('modal footer', function () {
    it('renders the cancelText if given',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee6() {
      var customText, modal, footer, cancelButton;
      return _regenerator.default.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              customText = 'Dont do it';
              _context6.next = 3;
              return render({
                cancelText: customText
              });

            case 3:
              _context6.next = 5;
              return _locator.default.find();

            case 5:
              modal = _context6.sent;
              _context6.next = 8;
              return modal.findFooter();

            case 8:
              footer = _context6.sent;
              _context6.next = 11;
              return footer.findAll('button');

            case 11:
              cancelButton = _context6.sent[0];
              (0, _uiTestUtils.expect)(cancelButton).to.have.text('Dont do it');

            case 13:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
    it('renders the continueText if given',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee7() {
      var customText, modal, footer, continueButton;
      return _regenerator.default.wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              customText = 'Do it';
              _context7.next = 3;
              return render({
                continueText: customText
              });

            case 3:
              _context7.next = 5;
              return _locator.default.find();

            case 5:
              modal = _context7.sent;
              _context7.next = 8;
              return modal.findFooter();

            case 8:
              footer = _context7.sent;
              _context7.next = 11;
              return footer.findAll('button');

            case 11:
              continueButton = _context7.sent[1];
              (0, _uiTestUtils.expect)(continueButton).to.have.text('Do it');

            case 13:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    })));
  });
  describe('main content', function () {
    it('renders the text given',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee8() {
      var customText, modal, body;
      return _regenerator.default.wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              customText = 'Are you sure you wan\'t to do this?';
              _context8.next = 3;
              return render({
                text: customText
              });

            case 3:
              _context8.next = 5;
              return _locator.default.find();

            case 5:
              modal = _context8.sent;
              _context8.next = 8;
              return modal.findBody();

            case 8:
              body = _context8.sent;
              (0, _uiTestUtils.expect)(body).to.have.text(customText);

            case 10:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  describe('actions', function () {
    var findButtonWithText =
    /*#__PURE__*/
    function () {
      var _ref10 = (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee9(text) {
        var modal, footer, button;
        return _regenerator.default.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                _context9.next = 2;
                return _locator.default.find();

              case 2:
                modal = _context9.sent;
                _context9.next = 5;
                return modal.findFooter();

              case 5:
                footer = _context9.sent;
                _context9.next = 8;
                return footer.find("button:contains(".concat(text, ")"));

              case 8:
                button = _context9.sent;
                return _context9.abrupt("return", button);

              case 10:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      }));

      return function findButtonWithText(_x) {
        return _ref10.apply(this, arguments);
      };
    }();

    it('calls cancelCb when you hit cancel',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee10() {
      var cancelCbStub, subject, cancelButton;
      return _regenerator.default.wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              cancelCbStub = (0, _uiTestUtils.stub)();
              _context10.next = 3;
              return render({
                onCancel: cancelCbStub
              });

            case 3:
              subject = _context10.sent;
              _context10.next = 6;
              return findButtonWithText('Cancel');

            case 6:
              cancelButton = _context10.sent;
              _context10.next = 9;
              return cancelButton.click();

            case 9:
              _context10.next = 11;
              return subject.setProps({
                modalOpen: false
              });

            case 11:
              _context10.next = 13;
              return (0, _uiTestUtils.waitForExpect)(function () {
                return (0, _uiTestUtils.expect)(cancelCbStub).to.have.been.called();
              });

            case 13:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
    it('calls continueCb when you hit continue',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee11() {
      var continueCbStub, subject, confirmButton;
      return _regenerator.default.wrap(function _callee11$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              continueCbStub = (0, _uiTestUtils.stub)();
              _context11.next = 3;
              return render({
                onContinue: continueCbStub
              });

            case 3:
              subject = _context11.sent;
              _context11.next = 6;
              return findButtonWithText('Confirm');

            case 6:
              confirmButton = _context11.sent;
              _context11.next = 9;
              return confirmButton.click();

            case 9:
              _context11.next = 11;
              return subject.setProps({
                modalOpen: false
              });

            case 11:
              _context11.next = 13;
              return (0, _uiTestUtils.waitForExpect)(function () {
                return (0, _uiTestUtils.expect)(continueCbStub).to.have.been.called();
              });

            case 13:
            case "end":
              return _context11.stop();
          }
        }
      }, _callee11);
    })));
    it('calls closeModal when you hit cancel',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee12() {
      var closeModalStub, cancelButton;
      return _regenerator.default.wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              closeModalStub = (0, _uiTestUtils.stub)();
              _context12.next = 3;
              return render({
                closeModal: closeModalStub
              });

            case 3:
              _context12.next = 5;
              return findButtonWithText('Cancel');

            case 5:
              cancelButton = _context12.sent;
              _context12.next = 8;
              return cancelButton.click();

            case 8:
              _context12.next = 10;
              return (0, _uiTestUtils.waitForExpect)(function () {
                return (0, _uiTestUtils.expect)(closeModalStub).to.have.been.called();
              });

            case 10:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    })));
    it('calls closeModal when you hit continue',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee13() {
      var closeModalStub, confirmButton;
      return _regenerator.default.wrap(function _callee13$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              closeModalStub = (0, _uiTestUtils.stub)();
              _context13.next = 3;
              return render({
                closeModal: closeModalStub
              });

            case 3:
              _context13.next = 5;
              return findButtonWithText('Confirm');

            case 5:
              confirmButton = _context13.sent;
              _context13.next = 8;
              return confirmButton.click();

            case 8:
              _context13.next = 10;
              return (0, _uiTestUtils.waitForExpect)(function () {
                return (0, _uiTestUtils.expect)(closeModalStub).to.have.been.called();
              });

            case 10:
            case "end":
              return _context13.stop();
          }
        }
      }, _callee13);
    })));
  });
  describe('a11y tests', function () {
    it('is accessible',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee14() {
      return _regenerator.default.wrap(function _callee14$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              _context14.next = 2;
              return render();

            case 2:
              _context14.t0 = _uiTestUtils.expect;
              _context14.next = 5;
              return (0, _uiTestUtils.accessible)();

            case 5:
              _context14.t1 = _context14.sent;
              (0, _context14.t0)(_context14.t1).to.be.true();

            case 7:
            case "end":
              return _context14.stop();
          }
        }
      }, _callee14);
    })));
  });
});