"use strict";

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

var _immutable = require("immutable");

var _multiple_choice = _interopRequireDefault(require("quiz-interactions/lib/records/interactions/multiple_choice"));

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

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

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

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

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

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

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

var _SidebarItem = _interopRequireDefault(require("../SidebarItem"));

var _Stimulus2 = _interopRequireDefault(require("../Stimulus"));

var _Sidebar = _interopRequireDefault(require("../../../../../common/components/layout/sidebar/Sidebar"));

var styles = {
  componentId: 'bAyez',
  template: function template(theme) {
    return "\n\n\n.Sidebar__styles__bottomPadding {\n  padding: ".concat(theme.bottomPadding || 'inherit', ";\n}\n\n.Sidebar__styles__hide {\n  display: none;\n}\n\n.Sidebar__styles__sidebarIsOpen .Sidebar__styles__closedItems {\n    display: none;\n  }\n\n.Sidebar__styles__sidebarIsClosed .Sidebar__styles__openItems {\n    display: none;\n  }\n");
  },
  'bottomPadding': 'Sidebar__styles__bottomPadding',
  'hide': 'Sidebar__styles__hide',
  'sidebarIsOpen': 'Sidebar__styles__sidebarIsOpen',
  'closedItems': 'Sidebar__styles__closedItems',
  'sidebarIsClosed': 'Sidebar__styles__sidebarIsClosed',
  'openItems': 'Sidebar__styles__openItems'
};
describe('Building Sidebar Presenter', function () {
  var intType = new _multiple_choice.default({});
  var itemA = new _Item.default({
    id: '5',
    itemBody: '<div>first</div>&amp;<div>second</div>'
  });
  var itemB = new _Item.default({
    id: '6'
  });
  var bank = new _Bank.default({
    id: '8',
    title: '<div>nohtml</div>'
  });
  var stimulus = new _Stimulus.default({
    id: '10'
  });
  var bankEntry = new _BankEntry.default({
    id: '11'
  });
  sinon.stub(itemA, 'getInteractionType').returns(intType);
  sinon.stub(itemB, 'getInteractionType').returns(intType);
  sinon.stub(bankEntry, 'getEntry').returns(itemA);
  var quizEntryA = new _QuizEntry.default({
    id: '3',
    entryType: 'Item',
    position: 4
  });
  var quizEntryB = new _QuizEntry.default({
    id: '8',
    entryType: 'Item',
    position: 8
  });
  var quizEntryC = new _QuizEntry.default({
    id: '9',
    entryType: 'Bank',
    position: 9
  });
  var quizEntryD = new _QuizEntry.default({
    id: '11',
    entryType: 'BankEntry',
    position: 10
  });
  var quizEntryE = new _QuizEntry.default({
    id: '10',
    entryType: 'Stimulus',
    position: 11
  });
  sinon.stub(quizEntryA, 'getEntry').returns(itemA);
  sinon.stub(quizEntryB, 'getEntry').returns(itemB);
  sinon.stub(quizEntryC, 'getEntry').returns(bank);
  sinon.stub(quizEntryD, 'getEntry').returns(bankEntry);
  sinon.stub(quizEntryE, 'getEntry').returns(stimulus);
  var quizEntries = (0, _immutable.List)([quizEntryA, quizEntryB, quizEntryC, quizEntryD]);

  var noop = function noop() {};

  var props = {
    connectDropTarget: function connectDropTarget(Component) {
      return Component;
    },
    // inserted by React DnD
    isOver: false,
    // inserted by React DnD
    moveQuizEntry: noop,
    // Used for DND
    moveTemporaryQuizEntry: noop,
    // Used for DND
    newHoverPosition: noop,
    // Used for DND
    quizId: '1',
    // Used for DND
    scrollToItem: noop,
    sidebarOpen: true,
    toggleSidebar: function toggleSidebar() {},
    quizEntries: quizEntries
  };
  var testbed = createTestbed(withContext((0, _dndTestUtils.wrapInDragAndDropTestContext)(_presenter.default)), props);
  describe('#renderSidebarItems', function () {
    it('renders the correct number', function () {
      testbed.render();
      var sidebarItems = testbed.findChildrenByType(_SidebarItem.default);
      expect(sidebarItems.length).to.eq(8);
      var openItems = document.querySelector(".".concat(styles.openItems));
      expect(openItems.childNodes.length).to.eq(4);
      var closedItems = document.querySelector(".".concat(styles.closedItems));
      expect(closedItems.childNodes.length).to.eq(4);
    });
    it('renders with the sidebarIsOpen class when sidebar is open', function () {
      testbed.render({
        sidebarOpen: true
      });
      expect(!!document.querySelector(".".concat(styles.sidebarIsOpen))).to.be.true();
      expect(!!document.querySelector(".".concat(styles.sidebarIsClosed))).to.be.false();
    });
    it('renders with the sidebarIsClosed class when sidebar is closed', function () {
      testbed.render({
        sidebarOpen: false
      });
      expect(!!document.querySelector(".".concat(styles.sidebarIsOpen))).to.be.false();
      expect(!!document.querySelector(".".concat(styles.sidebarIsClosed))).to.be.true();
    });
    it('should extract text content of item body HTML', function () {
      testbed.render();
      var sidebarItems = testbed.findChildrenByType(_SidebarItem.default);
      expect(sidebarItems[0].props.itemBody).to.eq('first&second');
      expect(sidebarItems[3].props.itemBody).to.eq('first&second');
    });
    it('should not extract text content of bank title', function () {
      testbed.render();
      var sidebarItems = testbed.findChildrenByType(_SidebarItem.default);
      expect(sidebarItems[2].props.itemBody).to.eq('<div>nohtml</div>');
    });
  });
  describe('#render', function () {
    it('renders the common sidebar', function () {
      testbed.render();
      expect(testbed.findChildrenByType(_Sidebar.default).length).to.eq(1);
    });
    it('passes the role prop to the CommonSidebar component', function () {
      var role = 'complementary';
      testbed.render({
        role: role
      });
      var commonSidebar = testbed.findChildByType(_Sidebar.default);
      expect(commonSidebar.props.role).to.equal(role);
    });
    context('with a stimulus quizEntry', function () {
      var quizEntriesWithStimulus = quizEntries.set(-1, quizEntryE);
      it('renders a stimulus sidebar component', function () {
        testbed.render({
          quizEntries: quizEntriesWithStimulus
        });
        expect(testbed.findChildrenByType(_Stimulus2.default).length).to.eq(2);
      });
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});