"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

var _immutable = require("immutable");

var _IconBank = _interopRequireDefault(require("@instructure/ui-icons/lib/Line/IconBank"));

var _dndTestUtils = require("../../../../../../../tests/util/dndTestUtils");

var _index = _interopRequireDefault(require("../index"));

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

var _ActionButtons = _interopRequireDefault(require("../../../ActionButtons"));

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

var _QuizEntry = _interopRequireDefault(require("../../../../../../common/records/QuizEntry"));

var _Bank = _interopRequireDefault(require("../../../../../../common/records/Bank"));

var _PositionBox = _interopRequireDefault(require("../../../../../../common/components/resources/positionBox/PositionBox"));

var styles = {
  componentId: 'bxAMG',
  template: function template(theme) {
    return "\n\n.bxAMG_eLmg{transform:translateZ(0)}\n\n.bxAMG_TMNr{background-color:".concat(theme.quizEntryShowBackgroundColor || 'inherit', ";padding:").concat(theme.quizEntryShowPadding || 'inherit', "}\n\n.bxAMG_fsuY{align-items:stretch;display:flex;flex-direction:row;flex-wrap:nowrap;height:").concat(theme.headerHeight || 'inherit', ";padding-right:").concat(theme.headerRightPadding || 'inherit', "}\n\n.bxAMG_bscQ{border:").concat(theme.dndBorder || 'inherit', "}\n\n.bxAMG_cKlw{align-items:center;display:inline-flex;flex:8 0 0;overflow:hidden}\n\n.bxAMG_IoDT{color:").concat(theme.rightHeaderColor || 'inherit', ";display:inline-flex;flex:0 0 auto;text-align:right;z-index:").concat(theme.rightHeaderZIndex || 'inherit', "}\n\n[dir=ltr] .bxAMG_IoDT,[dir=rtl] .bxAMG_IoDT{text-align:right}\n\n.bxAMG_IoDT button{background-color:transparent;background-color:initial}\n\n.bxAMG_eCZW,.bxAMG_cwte,.bxAMG_nvdO{color:").concat(theme.titleInteractionTypeNamePointsColor || 'inherit', ";font-size:").concat(theme.titleInteractionTypeNamePointsFontSize || 'inherit', ";margin-left:").concat(theme.titleInteractionTypeNamePointsLeftMargin || 'inherit', ";margin-right:").concat(theme.titleInteractionTypeNamePointsRightMargin || 'inherit', ";overflow:hidden;text-overflow:ellipsis;white-space:nowrap}\n\n.bxAMG_csjx{margin:").concat(theme.itemHolderMargin || 'inherit', "}\n\n.bxAMG_fPOQ,.bxAMG_gaTA{background-color:transparent;border:none;color:inherit;font-size:").concat(theme.optionsButtonDragDropHandleFontSize || 'inherit', ";margin:").concat(theme.optionsButtonDragDropHandleMargin || 'inherit', "}\n\n.bxAMG_gaTA{cursor:pointer}");
  },
  'quizEntryWrapper': 'bxAMG_eLmg',
  'quizEntryShow': 'bxAMG_TMNr',
  'header': 'bxAMG_fsuY',
  'dndBorder': 'bxAMG_bscQ',
  'leftHeader': 'bxAMG_cKlw',
  'rightHeader': 'bxAMG_IoDT',
  'interactionTypeName': 'bxAMG_eCZW',
  'points': 'bxAMG_cwte',
  'title': 'bxAMG_nvdO',
  'itemHolder': 'bxAMG_csjx',
  'dragAndDropHandle': 'bxAMG_fPOQ',
  'optionsButton': 'bxAMG_gaTA'
};
var overlayStyles = {
  componentId: 'bxAMG',
  template: function template(theme) {
    return "\n\n.dwHlC_bGBk{align-self:center;position:relative}\n\n.dwHlC_cYkL{cursor:pointer;height:100%;left:0;position:absolute;top:0;width:100%;z-index:".concat(theme.overlayZIndex || 'inherit', "}\n\n.dwHlC_cYkL:hover{border-left:").concat(theme.overlayBarSize || 'inherit', " solid ").concat(theme.overlayBarColor || 'inherit', ";box-sizing:border-box}");
  },
  'root': 'dwHlC_bGBk',
  'overlay': 'dwHlC_cYkL'
};
describe('QuizEntryShow', function () {
  var QuizEntryShowWithContext = withContext(_presenter.default);
  var moveQuizEntryStub = sinon.stub();
  var moveTemporaryQuizEntryStub = sinon.stub();
  var removeQuizEntryStub = sinon.stub();
  var screenreaderNotificationStub = sinon.stub();
  var switchOnEditingStub = sinon.stub();
  var quizEntry = new _QuizEntry.default({
    id: 55,
    entryType: 'Item',
    entryId: 66,
    pointsPossible: 12,
    position: 3
  });
  var item = new _Item.default({
    id: 66,
    title: 'The Title'
  });
  var bank = new _Bank.default({
    id: '1',
    entryCount: 120
  });
  var quizEntry1 = new _QuizEntry.default({
    position: 1
  });
  var quizEntry2 = new _QuizEntry.default({
    position: 2
  });
  var quizEntry4 = new _QuizEntry.default({
    position: 4
  });
  var quizEntry5 = new _QuizEntry.default({
    position: 5
  });
  var quizEntries = (0, _immutable.List)([quizEntry1, quizEntry2, quizEntry, quizEntry4, quizEntry5]);
  var defaultProps = {
    moveQuizEntry: moveQuizEntryStub,
    moveTemporaryQuizEntry: moveTemporaryQuizEntryStub,
    removeQuizEntry: removeQuizEntryStub,
    screenreaderNotification: screenreaderNotificationStub,
    switchOnEditing: switchOnEditingStub,
    errorsAreShowing: (0, _immutable.Map)(),
    quizEntry: quizEntry,
    entry: item,
    displayPosition: 3,
    quizEntries: quizEntries,
    scrollKey: 'scrollKey',
    editQuizEntryToggle: false
  };
  var testbed = createTestbed((0, _dndTestUtils.wrapInDragAndDropTestContext)(QuizEntryShowWithContext), defaultProps);
  beforeEach(function () {
    moveQuizEntryStub.resetHistory();
    moveTemporaryQuizEntryStub.resetHistory();
    removeQuizEntryStub.resetHistory();
    screenreaderNotificationStub.resetHistory();
    switchOnEditingStub.resetHistory();
  });
  shouldRenderComponent(testbed, {}, QuizEntryShowWithContext);
  it('renders the title', function () {
    testbed.render();
    expect(testbed.rootNode.innerText.match(/The Title/)).to.exist();
  });
  it('renders a bank entry', function () {
    var qe = quizEntry.set('entryType', 'Bank');
    sinon.stub(qe, 'getEntry').returns(bank);
    testbed.render({
      quizEntry: qe
    });
    var elem = testbed.findChildrenByType(_IconBank.default)[0];
    expect(elem).to.not.be.undefined();
  });
  context('item-type entry', function () {
    it('renders points possible', function () {
      testbed.render();
      var pointsPossible = testbed.findChildrenByClass(styles.points)[0].innerText;
      expect(pointsPossible).to.eq('12 points');
    });
    it('defaults points possible to 0', function () {
      testbed.render({
        quizEntry: quizEntry.set('pointsPossible', void 0)
      });
      var pointsPossible = testbed.findChildrenByClass(styles.points)[0].innerText;
      expect(pointsPossible).to.eq('0 points');
    });
  });
  it('switches to editing mode on click', function () {
    testbed.render();
    var header = testbed.findChildrenByClass(overlayStyles.overlay)[0];

    _testUtils.default.Simulate.click(header);

    expect(switchOnEditingStub.called).to.be.true();
    expect(screenreaderNotificationStub.called).to.be.true();
  });
  it('does not switch to edit mode if there are errors on another quiz entry', function () {
    testbed.render({
      quizEntryWithErrorsShowing: {
        position: 1
      }
    });
    var header = testbed.findChildrenByClass(overlayStyles.overlay)[0];

    _testUtils.default.Simulate.click(header);

    expect(switchOnEditingStub.called).to.be.false();
    expect(screenreaderNotificationStub.called).to.be.false();
  });
  it('passes scrollKey to ActionButtons', function () {
    testbed.render();
    var actionButtons = testbed.findChildByType(_ActionButtons.default);
    expect(actionButtons.props.scrollKey).to.equal(defaultProps.scrollKey);
  });
  describe('display position', function () {
    it('renders the displayPosition rather than the quizEntry position', function () {
      testbed.render();
      var box = testbed.findChildrenByType(_PositionBox.default)[0];
      expect(box.props.position).to.eql("".concat(defaultProps.displayPosition));
    });
  });
  describe('#shouldDisableDragHandle', function () {
    var runTest = function runTest(position, quizEntries, expected) {
      testbed.render({
        quizEntry: quizEntry.set('position', position),
        quizEntries: quizEntries
      });
      var subject = testbed.findChildByType(_presenter.QuizEntryShowRaw);
      expect(subject.shouldDisableDragHandle()).to.eq(expected);
    };

    it('is true if there is only one item', function () {
      runTest(1, (0, _immutable.List)([quizEntry]), true);
    });
    it('is false if there are successor items', function () {
      runTest(1, (0, _immutable.List)([quizEntry, quizEntry2]), false);
    });
    it('is false if there are predecessor items', function () {
      runTest(2, (0, _immutable.List)([quizEntry1, quizEntry]), false);
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['label', 'label-title-only']
      });
    });
  });
  describe('test with connect', function () {
    var connectedTestbed = createTestbed((0, _dndTestUtils.wrapInDragAndDropTestContext)(withContext(_index.default)), defaultProps);
    shouldRenderComponent(connectedTestbed, {}, _presenter.default);
  });
});