"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 _reactDom = _interopRequireDefault(require("react-dom"));

var _testUtils = _interopRequireDefault(require("react-dom/test-utils"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

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

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

describe('Entry Save', function () {
  var EntryWithContext = withContext(_presenter.default);
  var item = new _Item.default({
    id: '666',
    title: 'Tough Color Question',
    itemBody: 'Which color is happiest?',
    scoringData: {
      value: 'id'
    }
  });
  var sandbox = sinon.createSandbox();
  var notificationStub = sandbox.stub();
  var submitHandlerStub = sandbox.stub();
  var cancelHandlerStub = sandbox.stub();
  var clearChangesStub = sandbox.stub();
  var clearNextQuizEntryStub = sandbox.stub();
  var switchOnEditingStub = sandbox.stub();
  var switchOffEditingStub = sandbox.stub();
  var showErrorStub = sandbox.stub();
  var hideErrorStub = sandbox.stub();
  var testbed = createTestbed(EntryWithContext, {
    clearNextQuizEntry: clearNextQuizEntryStub,
    errorsAreShowing: true,
    isValid: true,
    showError: showErrorStub,
    hideError: hideErrorStub,
    entry: item,
    submitHandler: submitHandlerStub,
    switchOnEditing: switchOnEditingStub,
    switchOffEditing: switchOffEditingStub,
    cancelHandler: cancelHandlerStub,
    clearChanges: clearChangesStub,
    screenreaderNotification: notificationStub
  });
  var CANCEL_BUTTON_NUMBER = 0;
  var SUBMIT_BUTTON_NUMBER = 1;
  afterEach(function () {
    sandbox.reset();
  });
  shouldRenderComponent(testbed, {}, EntryWithContext);

  var confirmNotifiesScreenreader = function confirmNotifiesScreenreader(expectedMessage) {
    expect(notificationStub).to.have.been.calledWith(expectedMessage);
  };

  it('switches off editing and calls clearChanges when it unmounts',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            testbed.render({
              clearChanges: clearChangesStub,
              switchOffEditing: switchOffEditingStub
            });

            _reactDom.default.unmountComponentAtNode(testbed.rootNode);

            expect(clearChangesStub).to.have.been.called();
            expect(switchOffEditingStub).to.have.been.called();
            confirmNotifiesScreenreader('Item editing mode turned off.');

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

  var clickButton = function clickButton(buttonComponent) {
    var buttonNode = _reactDom.default.findDOMNode(buttonComponent);

    _testUtils.default.Simulate.click(buttonNode);
  };

  var clickSubmit = function clickSubmit() {
    var submitButton = testbed.findChildrenByType(_Button.default)[SUBMIT_BUTTON_NUMBER];
    clickButton(submitButton);
  };

  var clickCancel = function clickCancel() {
    var cancelButton = testbed.findChildrenByType(_Button.default)[CANCEL_BUTTON_NUMBER];
    clickButton(cancelButton);
  };

  describe('#onSubmit', function () {
    context('is valid', function () {
      it('calls submitHandler',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2() {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                testbed.render();
                clickSubmit();
                expect(submitHandlerStub).to.have.been.called();

              case 3:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      })));
      it('calls switchOffEditing', function (done) {
        testbed.render();
        clickSubmit();
        setTimeout(function () {
          expect(switchOffEditingStub).to.have.been.called();
          done();
        });
      });
      it('notifies the screenreader', function (done) {
        testbed.render();
        clickSubmit();
        setTimeout(function () {
          confirmNotifiesScreenreader('Changes to item saved.');
          done();
        });
      });
      it('cannot be called twice on double-click',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee3() {
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                testbed.render();
                clickSubmit();
                clickSubmit();
                expect(submitHandlerStub).to.have.been.calledOnce();

              case 4:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      })));
      it('disables the button',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee4() {
        var submitButton;
        return _regenerator.default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                testbed.render();
                clickSubmit();
                submitButton = testbed.findChildrenByType(_Button.default)[SUBMIT_BUTTON_NUMBER];
                expect(submitButton.props.disabled).to.be.true();

              case 4:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4);
      })));
      context('when submitHandler fails', function () {
        beforeEach(function () {
          submitHandlerStub.rejects();
        });
        it('does not call switchOffEditing', function (done) {
          testbed.render();
          clickSubmit();
          setTimeout(function () {
            expect(switchOffEditingStub).not.to.have.been.called();
            done();
          });
        });
        it('does not notify the screenreader', function (done) {
          testbed.render();
          clickSubmit();
          setTimeout(function () {
            expect(notificationStub).not.to.have.been.called();
            done();
          });
        });
        it('re-enables the button', function (done) {
          testbed.render();
          clickSubmit();
          setTimeout(function () {
            var submitButton = testbed.findChildrenByType(_Button.default)[SUBMIT_BUTTON_NUMBER];
            expect(submitButton.props.disabled).to.be.false();
            done();
          });
        });
      });
    });
    context('is invalid', function () {
      it('does not call submitHandler',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee5() {
        return _regenerator.default.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                testbed.render({
                  isValid: false
                });
                clickSubmit();
                expect(submitHandlerStub).to.have.not.been.called();

              case 3:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      })));
      it('calls showError',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee6() {
        return _regenerator.default.wrap(function _callee6$(_context6) {
          while (1) {
            switch (_context6.prev = _context6.next) {
              case 0:
                testbed.render({
                  isValid: false
                });
                clickSubmit();
                expect(showErrorStub).to.have.been.called();

              case 3:
              case "end":
                return _context6.stop();
            }
          }
        }, _callee6);
      })));
      it('calls clearNextQuizEntry',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee7() {
        return _regenerator.default.wrap(function _callee7$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                testbed.render({
                  isValid: false
                });
                clickSubmit();
                expect(clearNextQuizEntryStub).to.have.been.called();

              case 3:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee7);
      })));
      it('does not call switchOffEditing',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee8() {
        return _regenerator.default.wrap(function _callee8$(_context8) {
          while (1) {
            switch (_context8.prev = _context8.next) {
              case 0:
                testbed.render({
                  isValid: false
                });
                clickSubmit();
                expect(switchOffEditingStub).to.have.not.been.called();

              case 3:
              case "end":
                return _context8.stop();
            }
          }
        }, _callee8);
      })));
      it('notifies the screenreader',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee9() {
        return _regenerator.default.wrap(function _callee9$(_context9) {
          while (1) {
            switch (_context9.prev = _context9.next) {
              case 0:
                testbed.render({
                  isValid: false
                });
                clickSubmit();
                confirmNotifiesScreenreader('This item cannot be saved due to invalid input data.');

              case 3:
              case "end":
                return _context9.stop();
            }
          }
        }, _callee9);
      })));
    });
  });
  describe('#onCancel', function () {
    it('calls switchOffEditing',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee10() {
      return _regenerator.default.wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              testbed.render();
              clickCancel();
              expect(switchOffEditingStub).to.have.been.called();

            case 3:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
    it('calls hideError',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee11() {
      return _regenerator.default.wrap(function _callee11$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              testbed.render();
              clickCancel();
              expect(hideErrorStub).to.have.been.called();

            case 3:
            case "end":
              return _context11.stop();
          }
        }
      }, _callee11);
    })));
    it('calls cancelHandler',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee12() {
      return _regenerator.default.wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              testbed.render();
              clickCancel();
              expect(cancelHandlerStub).to.have.been.called();

            case 3:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    })));
    it('calls clearChanges',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee13() {
      return _regenerator.default.wrap(function _callee13$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              testbed.render();
              clickCancel();
              expect(clearChangesStub).to.have.been.called();

            case 3:
            case "end":
              return _context13.stop();
          }
        }
      }, _callee13);
    })));
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});