"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 _immutable = require("immutable");

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

var _Modal = _interopRequireDefault(require("../../../../../common/components/Modal"));

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

describe('ItemFeedbackModal', function () {
  var closeModalStub = sinon.stub();
  var openModalStub = sinon.stub();
  var changeItemFeedbackStub = sinon.stub();
  var setUiStub = sinon.stub();
  var modalProps = {
    changeItemFeedback: changeItemFeedbackStub,
    closeModal: closeModalStub,
    feedback: (0, _immutable.Map)(),
    item: {
      id: 1,
      itemBody: 'itemStem'
    },
    modalOpen: true,
    openModal: openModalStub,
    setUi: setUiStub
  };
  var ConnectedItemFeedbackModal = withContext(_presenter.default);
  var testbed = createTestbed(ConnectedItemFeedbackModal, modalProps);
  var shallowtb = createTestbed(_presenter.default, modalProps);
  var shallowtestbed;
  beforeEach(function () {
    shallowtestbed = shallowtb.renderShallowly();
  });
  afterEach(function () {
    closeModalStub.resetHistory();
    changeItemFeedbackStub.resetHistory();
    openModalStub.resetHistory();
    setUiStub.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _presenter.default);
  describe('CreateItemFeedbackModal', function () {
    it('renders the modal', function () {
      var modal = shallowtestbed.find(_Modal.default);
      expect(modal).to.have.length(1);
    });
  });
  describe('Modal Title', function () {
    it('renders the correct title for the modal', function () {
      var modalheader = shallowtestbed.find('ModalHeader');
      expect(modalheader).to.have.length(1);
      expect(modalheader.render().text()).to.be.equal('Student Feedback');
    });
  });
  describe('Item Stem', function () {
    it('renders the item stem in the modal', function () {
      testbed.render();
      expect(testbed.getText()).to.contain('itemStem');
    });
  });
  describe('Item Feedback Inputs', function () {
    it('sets feedback on change', function () {
      var subject = testbed.render();
      var event = {
        target: {
          id: 'correct'
        }
      };
      var component = subject.refs.composedComponent;
      component.onFeedbackChange(event, {
        editorContent: 'some new content'
      });
      var newFeedback = setUiStub.firstCall.args[1];
      expect(newFeedback.get('correct')).to.equal('some new content');
    });
  });
  describe('Cancel action', function () {
    it('renders cancel button', function () {
      var cancelButton = shallowtestbed.find('Button[variant="default"]');
      expect(cancelButton.render().text()).to.include('Cancel');
    });
    it('calls the cancel action', function () {
      var cancelButton = shallowtestbed.find('Button[variant="default"]');
      cancelButton.simulate('click');
      expect(closeModalStub.called).to.be.true();
    });
    it('does not update feedback when the modal is cancelled',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var cancelButton, inputs, incorrectInput;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (0, _uiTestUtils.mount)(_react.default.createElement(ConnectedItemFeedbackModal, modalProps));

            case 2:
              _context.next = 4;
              return (0, _uiTestUtils.find)(':label(Cancel)');

            case 4:
              cancelButton = _context.sent;
              _context.next = 7;
              return (0, _uiTestUtils.findAll)('textarea');

            case 7:
              inputs = _context.sent;
              incorrectInput = inputs[1];
              _context.next = 11;
              return incorrectInput.change({
                target: {
                  id: 'incorrect',
                  value: 'incorrect'
                }
              });

            case 11:
              _context.next = 13;
              return cancelButton.click();

            case 13:
              expect(changeItemFeedbackStub.called).to.be.false();
              expect(closeModalStub.called).to.be.true();

            case 15:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
  describe('Done action', function () {
    it('renders the Done button', function () {
      var modalfooter = shallowtestbed.find('ModalFooter');
      var createButton = modalfooter.shallow({
        disableLifecycleMethods: true
      }).find('Button[variant="primary"]');
      expect(createButton.render().text()).to.include('Done');
    });
    it('updates item modifications when the modal is submitted',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var doneButton, inputs, incorrectInput;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              _context2.next = 2;
              return (0, _uiTestUtils.mount)(_react.default.createElement(ConnectedItemFeedbackModal, modalProps));

            case 2:
              _context2.next = 4;
              return (0, _uiTestUtils.find)(':label(Done)');

            case 4:
              doneButton = _context2.sent;
              _context2.next = 7;
              return (0, _uiTestUtils.findAll)('textarea');

            case 7:
              inputs = _context2.sent;
              incorrectInput = inputs[1];
              _context2.next = 11;
              return incorrectInput.change({
                target: {
                  id: 'incorrect',
                  value: 'incorrect'
                }
              });

            case 11:
              _context2.next = 13;
              return doneButton.click();

            case 13:
              expect(changeItemFeedbackStub.called).to.be.true();
              expect(closeModalStub.called).to.be.true();

            case 15:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
});