"use strict";

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

var _immutable = require("immutable");

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 testbed = createTestbed(withContext(_presenter.default), modalProps);
  var shallowtb = createTestbed(_presenter.default, modalProps);
  var 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', function () {
      var cancelButton = shallowtestbed.find('Button[variant="default"]');
      var incorrectInput = shallowtestbed.find('TextInput').at(1);
      incorrectInput.simulate('change', {
        target: {
          id: 'incorrect',
          value: 'incorrect'
        }
      });
      cancelButton.simulate('click');
      expect(changeItemFeedbackStub.called).to.be.false();
    });
  });
  describe('Done action', function () {
    it('renders the Done button', function () {
      var modalfooter = shallowtestbed.find('ModalFooter');
      var createButton = modalfooter.shallow().find('Button[variant="primary"]');
      expect(createButton.render().text()).to.include('Done');
    });
    it('updates item modifications when the modal is submitted', function () {
      var doneButton = shallowtestbed.find('Button[variant="primary"]');
      var incorrectInput = shallowtestbed.find('TextInput').at(1);
      incorrectInput.simulate('change', {
        target: {
          id: 'incorrect',
          value: 'incorrect'
        }
      });
      doneButton.simulate('click');
      expect(changeItemFeedbackStub.called).to.be.true();
      expect(closeModalStub.called).to.be.true();
    });
  });
});