"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 _InteractionType = _interopRequireDefault(require("quiz-interactions/lib/records/InteractionType"));

var _shallowTestbed = _interopRequireDefault(require("../../../../../tests/util/shallowTestbed"));

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

var _Stimulus = _interopRequireDefault(require("../../../../common/records/Stimulus"));

var _BankEntry = _interopRequireDefault(require("../../../../common/records/BankEntry"));

var _CopyMoveBankEntryModal = _interopRequireDefault(require("../../CopyMoveBankEntryModal"));

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

var _constants = require("@instructure/quiz-common/lib/constants");

var styles = {
  componentId: 'eQXFj',
  template: function template(theme) {
    return "\n\n.BankEntryRow__styles__entryDetail {\n  padding: ".concat(theme.entryDetailPadding || 'inherit', ";\n}\n\n.BankEntryRow__styles__headerRow {\n  display: flex;\n  flex-direction: row;\n  justify-content: space-between;\n  align-items: flex-start;\n  color: ").concat(theme.headerRowColor || 'inherit', ";\n}\n\n.BankEntryRow__styles__buttons {\n  position: relative;\n  display: inline-flex;\n  z-index: ").concat(theme.buttonsZIndex || 'inherit', ";\n}\n\n.BankEntryRow__styles__expandButtonContainer {\n  border-left: ").concat(theme.expandButtonContainerBorderWidth || 'inherit', " solid ").concat(theme.expandButtonContainerBorderColor || 'inherit', ";\n}\n\n.BankEntryRow__styles__deleteAlert {\n  display: block;\n}\n\n.BankEntryRow__styles__deletePreface {\n  margin-bottom: ").concat(theme.deletePrefaceMargin || 'inherit', ";\n}\n\n.BankEntryRow__styles__addButton {\n  margin-bottom: ").concat(theme.addButtonMargin || 'inherit', ";\n}\n\n.BankEntryRow__styles__headerText {\n  color: ").concat(theme.headerTextColor || 'inherit', ";\n  font-size: ").concat(theme.headerTextFontSize || 'inherit', ";\n}\n\n.BankEntryRow__styles__stem {\n  overflow-x: auto;\n  font-size: ").concat(theme.stemFontSize || 'inherit', ";\n  line-height: ").concat(theme.stemLineHeight || 'inherit', ";\n}\n\n.BankEntryRow__styles__stem p {\n    margin: 0;\n  }\n\n.BankEntryRow__styles__footer {\n  padding: ").concat(theme.footerPadding || 'inherit', ";\n  border-top: ").concat(theme.footerBorderWidth || 'inherit', " solid ").concat(theme.footerBorderColor || 'inherit', ";\n  font-size: ").concat(theme.footerFontSize || 'inherit', ";\n}\n");
  },
  'entryDetail': 'BankEntryRow__styles__entryDetail',
  'headerRow': 'BankEntryRow__styles__headerRow',
  'buttons': 'BankEntryRow__styles__buttons',
  'expandButtonContainer': 'BankEntryRow__styles__expandButtonContainer',
  'deleteAlert': 'BankEntryRow__styles__deleteAlert',
  'deletePreface': 'BankEntryRow__styles__deletePreface',
  'addButton': 'BankEntryRow__styles__addButton',
  'headerText': 'BankEntryRow__styles__headerText',
  'stem': 'BankEntryRow__styles__stem',
  'footer': 'BankEntryRow__styles__footer'
};
describe('BankEntryRow', function () {
  var toggleExpandedStub = sinon.stub();
  var setUiStub = sinon.stub();
  var type = new _InteractionType.default({
    name: 'Multiple Choice',
    slug: 'choice'
  });
  var item = new _Item.default({
    id: '666',
    title: 'Tough Color Question',
    itemBody: 'Which color is happiest?',
    scoringData: {
      value: 'id'
    }
  });
  sinon.stub(item, 'getInteractionType').returns(type);
  var bankEntry = new _BankEntry.default({
    id: '1',
    entry: item,
    entryType: 'Item',
    updatedAt: new Date()
  });
  sinon.stub(bankEntry, 'getEntry').returns(item);
  var addEntryToQuizStub = sinon.stub().resolves();
  var createItemStub = sinon.stub().returnsArg(0);
  var openModal = sinon.stub();
  var navigateBankEntryStub = sinon.stub();
  var withConfirmStub = sinon.stub();
  var removeBankEntryStub = sinon.stub();
  var screenreaderNotificationStub = sinon.stub();
  var testbed = (0, _shallowTestbed.default)(_presenter.default, {
    addEntryToQuiz: addEntryToQuizStub,
    bankEntry: bankEntry,
    createItem: createItemStub,
    isExpanded: false,
    toggleExpanded: toggleExpandedStub,
    openModal: openModal,
    navigateToBankEntry: navigateBankEntryStub,
    screenreaderNotification: screenreaderNotificationStub,
    removeBankEntry: removeBankEntryStub,
    withConfirm: withConfirmStub,
    setUi: setUiStub,
    useBankTags: false
  });
  afterEach(function () {
    addEntryToQuizStub.resetHistory();
    toggleExpandedStub.resetHistory();
    setUiStub.resetHistory();
    createItemStub.resetHistory();
    openModal.resetHistory();
    navigateBankEntryStub.resetHistory();
    withConfirmStub.resetHistory();
    removeBankEntryStub.resetHistory();
    screenreaderNotificationStub.resetHistory();
  });
  it('should render entry', function () {
    testbed.render({}, null, {
      disableLifecycleMethods: true
    });
    expect(testbed.getText('Heading')).to.include('Multiple Choice');
    expect(testbed.getText('Heading')).to.include('Item Tough Color Question');
    expect(testbed.getText('RichContentRenderer')).to.include('Which color is happiest?');
  });
  describe('addToQuiz', function () {
    it('should render "add to quiz" button only when action creator is provided', function () {
      testbed.render({
        addEntryToQuiz: void 0
      }, null, {
        disableLifecycleMethods: true
      });
      expect(testbed.find(".".concat(styles.addButton)).length).to.eq(0);
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      expect(testbed.find(".".concat(styles.addButton)).length).to.eq(1);
    });
    it('should render disabled "add to quiz" button if is stimulus and adding to stimulus', function () {
      var bankEntry = new _BankEntry.default({
        entryType: 'Stimulus'
      });
      sinon.stub(bankEntry, 'getEntry').returns(new _Stimulus.default());
      testbed.render({
        stimulusId: 5,
        bankEntry: bankEntry
      }, null, {
        disableLifecycleMethods: true
      });
      var button = testbed.find(".".concat(styles.addButton)).first();
      expect(button.props().children.props.disabled).to.be.true();
    });
    it('should render enabled "add to quiz" button if is item and adding to stimulus', function () {
      testbed.render({
        stimulusId: 5
      }, null, {
        disableLifecycleMethods: true
      });
      var button = testbed.find(".".concat(styles.addButton)).first();
      expect(button.props().children.props.disabled).to.be.false();
    });
    it('should render enabled "add to quiz" button if is stimulus, but not adding to stimulus', function () {
      var bankEntry = new _BankEntry.default({
        entryType: 'Stimulus'
      });
      sinon.stub(bankEntry, 'getEntry').returns(new _Stimulus.default());
      testbed.render({
        bankEntry: bankEntry
      }, null, {
        disableLifecycleMethods: true
      });
      var button = testbed.find(".".concat(styles.addButton)).first();
      expect(button.props().children.props.disabled).to.be.false();
    });
    it('should call "add to quiz" when quiz id is provided', function () {
      testbed.render({
        activeQuizId: '876'
      }, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find(".".concat(styles.addButton, " Button")).first());
      expect(addEntryToQuizStub).to.have.been.calledWith('876', '1', 'BankEntry', sinon.match({
        points_possible: 1
      }));
    });
    it('calls addEntryToQuiz and alerts the screenreader', function (done) {
      testbed.render({
        activeQuizId: '876'
      }, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find(".".concat(styles.addButton, " Button")).first());
      expect(addEntryToQuizStub).to.have.been.called();
      setTimeout(function () {
        expect(screenreaderNotificationStub).to.have.been.called();
        done();
      });
    });
  });
  describe('expandable', function () {
    it('should read correctly', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      expect(testbed.find('ExpandButton').prop('title')).to.eq('Expand Item details');
      testbed.render({
        isExpanded: true
      }, null, {
        disableLifecycleMethods: true
      });
      expect(testbed.find('ExpandButton').prop('title')).to.eq('Collapse Item details');
    });
    it('clicking down arrow should trigger expand toggle', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find('ExpandButton'));
      expect(toggleExpandedStub).to.be.calledOnce();
    });
    it('should render entry details', function () {
      testbed.render({
        isExpanded: true
      }, null, {
        disableLifecycleMethods: true
      });
      var output = testbed.getText(".".concat(styles.footer));
      expect(output).to.include('Last Update');
    });
  });
  describe('CopyMoveBankEntryModal', function () {
    it('clicking move/copy button should open modal', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find('Button').at(2));
      expect(openModal).to.have.been.calledWith('COPY_MOVE_BANK_ENTRY_MODAL:Item:666');
    });
    it('should create a new entry when requested',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var entry;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              testbed.render({}, null, {
                disableLifecycleMethods: true
              });
              _context.next = 3;
              return testbed.find(_CopyMoveBankEntryModal.default).prop('createEntry')();

            case 3:
              entry = _context.sent;
              expect(createItemStub).to.have.been.calledWith(item);
              expect(entry).to.eq(item);

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
  describe('deleteBankEntry', function () {
    it('clicking trash button should ask to remove bank entry', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find('Button').at(3));
      expect(withConfirmStub).to.have.been.called();
      withConfirmStub.getCalls()[0].args[0]();
      expect(removeBankEntryStub).to.have.been.called();
    });
  });
  describe('edit bank entry', function () {
    it('clicking edit button should navigate to bank entry', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find('Button').at(1));
      expect(navigateBankEntryStub.called).to.eq(true);
    });
    it('clicking edit button should store focus as edit button', function () {
      testbed.render({}, null, {
        disableLifecycleMethods: true
      });
      testbed.click(testbed.find('Button').at(1));
      expect(setUiStub).to.be.calledWith(_constants.SET_EDIT_BUTTON_FOCUS, '1');
    });
  });
});