"use strict";

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

var _reactDom = _interopRequireDefault(require("react-dom"));

var _immutable = require("immutable");

var _TextInput = _interopRequireDefault(require("@instructure/ui-forms/lib/components/TextInput"));

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

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

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

var styles = {
  componentId: 'cYnzk',
  template: function template(theme) {
    return "\n\n.Header__styles__header {\n  background-color: ".concat(theme.headerBackgroundColor || 'inherit', ";\n  color: ").concat(theme.headerColor || 'inherit', ";\n\n  padding: ").concat(theme.headerPadding || 'inherit', ";\n\n  display: flex;\n  justify-content: space-between;\n  vertical-align: middle;\n  align-items: center;\n}\n\n.Header__styles__leftHeader {\n  flex: 8 0 0;\n  display: inline-flex;\n  flex-wrap: nowrap;\n  align-items: center;\n\n  \n\n  width: 100%;\n}\n\n.Header__styles__interactionType {\n  font-size: ").concat(theme.interactionTypeFontSize || 'inherit', ";\n  margin-left: ").concat(theme.interactionTypeLeftMargin || 'inherit', ";\n  margin-right: ").concat(theme.interactionTypeRightMargin || 'inherit', ";\n}\n\n.Header__styles__dividingLine {\n  width: 0;\n  border-left: ").concat(theme.dividingLineLeftBorder || 'inherit', ";\n}\n\n.Header__styles__titleHolder {\n  display: flex;\n  flex: 1 1 auto;\n}\n\n.Header__styles__titleInput {\n  flex: 1 1 auto;\n  font-size: ").concat(theme.titleInputFontSize || 'inherit', ";\n  white-space: nowrap;\n  text-overflow: ellipsis;\n}\n");
  },
  'header': 'Header__styles__header',
  'leftHeader': 'Header__styles__leftHeader',
  'interactionType': 'Header__styles__interactionType',
  'dividingLine': 'Header__styles__dividingLine',
  'titleHolder': 'Header__styles__titleHolder',
  'titleInput': 'Header__styles__titleInput'
};
describe('QuizEntryEditHeader', function () {
  var sandbox = sinon.createSandbox();
  var changeItemTitleStub = sandbox.stub();
  var changeQuizEntryEntryStub = sandbox.stub();
  var changeQuizEntryPointsStub = sandbox.stub();
  var clearModificationsStub = sandbox.stub();
  var clearTemporaryItemStub = sandbox.stub();
  var cloneQuizEntryStub = sandbox.stub();
  var connectDragSourceStub = sandbox.stub();
  var createItemStub = sandbox.stub().returns(Promise.resolve({
    id: '2'
  }));
  var createQuizEntryStub = sandbox.stub().returns(Promise.resolve({
    id: '56'
  }));
  var hideErrorStub = sandbox.stub();
  var moveQuizEntryStub = sandbox.stub();
  var moveTemporaryQuizEntryStub = sandbox.stub();
  var openModalStub = sandbox.stub();
  var removeQuizEntryStub = sandbox.stub();
  var screenreaderNotificationStub = sandbox.stub();
  var setUiStub = sandbox.stub();
  var showErrorStub = sandbox.stub();
  var switchOffEditingStub = sandbox.stub();
  var updateItemStub = sandbox.stub().resolves();
  var updateQuizEntryStub = sandbox.stub().resolves();
  var workingQuizEntry = new _QuizEntry.default({
    id: '55',
    pointsPossible: 3999
  });

  workingQuizEntry.isValid = function () {
    return true;
  };

  var quizEntry = new _QuizEntry.default({
    id: '55',
    entryType: 'Item',
    entryId: 66,
    pointsPossible: 12,
    position: 3
  });
  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 workingItem = new _Item.default({
    id: '1',
    properties: {
      varyPointsByAnswer: false
    }
  });

  workingItem.isValid = function () {
    return true;
  };

  workingItem.getErrors = function () {
    return (0, _immutable.fromJS)({});
  };

  workingItem.errorsFor = function () {
    return (0, _immutable.fromJS)({});
  };

  var item = new _Item.default({
    id: '66',
    title: 'some title'
  });
  var defaultProps = {
    changeItemTitle: changeItemTitleStub,
    changeQuizEntryEntry: changeQuizEntryEntryStub,
    changeQuizEntryPoints: changeQuizEntryPointsStub,
    clearModifications: clearModificationsStub,
    clearTemporaryItem: clearTemporaryItemStub,
    cloneQuizEntry: cloneQuizEntryStub,
    connectDragSource: connectDragSourceStub,
    createItem: createItemStub,
    createQuizEntry: createQuizEntryStub,
    hideError: hideErrorStub,
    moveQuizEntry: moveQuizEntryStub,
    moveTemporaryQuizEntry: moveTemporaryQuizEntryStub,
    displayPosition: 3,
    entry: item,
    guid: 'guid123',
    isOccluded: false,
    moveToPosition: function moveToPosition() {},
    name: 'This Will Not Always Be An Interaction Type Name So Please Do Not Assume It Is',
    openModal: openModalStub,
    quizEntries: quizEntries,
    quizEntry: quizEntry,
    quizId: '99',
    removeQuizEntry: removeQuizEntryStub,
    screenreaderNotification: screenreaderNotificationStub,
    setUi: setUiStub,
    showError: showErrorStub,
    switchOffEditing: switchOffEditingStub,
    updateItem: updateItemStub,
    updateQuizEntry: updateQuizEntryStub,
    withConfirm: function withConfirm() {},
    workingEntry: workingItem
  };
  var testbed = createTestbed(withContext(_index.default), defaultProps);
  beforeEach(function () {
    quizEntry.getWorkingInstance = function () {
      return workingQuizEntry;
    };

    item.getWorkingInstance = function () {
      return workingItem;
    };

    sandbox.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _index.default);
  it('renders the position from props (not from item record)', function () {
    var positionProp = 8;
    testbed.render({
      displayPosition: positionProp
    });
    var positionNode = testbed.findChildrenByClass(styles.leftHeader)[0];
    expect(positionNode.innerText).to.include("".concat(positionProp));
  });
  it('renders the name prop in header', function () {
    testbed.render();
    var header = testbed.findChildrenByClass(styles.header)[0];
    expect(testbed.getText(header)).to.include('This Will Not Always Be An Interaction Type Name So Please Do Not Assume It Is');
  });
  describe('title change', function () {
    it('calls changeItemTitle when the title is changed', function () {
      testbed.render();

      var titleInput = _reactDom.default.findDOMNode(testbed.findChildByType(_TextInput.default));

      testbed.change(titleInput.querySelector('input'), 'new title');
      expect(changeItemTitleStub).to.have.been.called();
    });
    it('shows the workingItem title in the title input', function () {
      testbed.render({
        workingEntry: item
      });

      var titleInput = _reactDom.default.findDOMNode(testbed.findChildByType(_TextInput.default));

      expect(titleInput.querySelector('input').value).to.equal('some title');
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['label', 'label-title-only']
      });
    });
  });
  describe('initialFocusId is not set', function () {
    it('titleRef should be focused', function (done) {
      testbed.render();
      var subject = testbed.findChildByType(_index.default);
      setTimeout(function () {
        expect(subject.titleRef.focused).to.be.true();
        done();
      }, 300);
    });
  });
  describe('initialFocusId is set', function () {
    it('titleRef should not be focused', function (done) {
      testbed.render(Object.assign({}, defaultProps, {
        initialFocusId: 'abcdefg'
      }));
      var subject = testbed.findChildByType(_index.default);
      setTimeout(function () {
        expect(subject.titleRef.focused).to.be.false();
        done();
      }, 300);
    });
  });
  describe('initialFocusId is not set', function () {
    it('titleRef should be focused', function (done) {
      testbed.render();
      var subject = testbed.findChildByType(_index.default);
      setTimeout(function () {
        expect(subject.titleRef.focused).to.be.true();
        done();
      }, 300);
    });
  });
  describe('initialFocusId is set', function () {
    it('titleRef should not be focused', function (done) {
      testbed.render(Object.assign({}, defaultProps, {
        initialFocusId: 'abcdefg'
      }));
      var subject = testbed.findChildByType(_index.default);
      setTimeout(function () {
        expect(subject.titleRef.focused).to.be.false();
        done();
      }, 300);
    });
  });
});