"use strict";

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

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

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

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

var _immutable = require("immutable");

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

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

var _Transition = _interopRequireDefault(require("@instructure/ui-motion/lib/components/Transition"));

var _PositionSummary = _interopRequireDefault(require("../../../PositionSummary"));

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

var _SessionItem = _interopRequireDefault(require("../../../../../../records/SessionItem"));

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

var styles = {
  componentId: 'cyDpo',
  template: function template(theme) {
    return "\n\n.Collection__styles__column {\n  display: flex;\n  flex-flow: column;\n  padding: ".concat(theme.columnPadding || 'inherit', ";\n  padding-bottom: 0;\n  align-items: center;\n}\n\n.Collection__styles__itemsContainer {\n  display: none;\n}\n\n.Collection__styles__sidebarIsClosed {\n  display: flex;\n}\n\n.Collection__styles__hideAll {\n  display: none;\n}\n\n.Collection__styles__sidebarIsOpen .Collection__styles__topBorder {\n    border-top: dashed ").concat(theme.sidebarIsOpenBottomBorderWidth || 'inherit', ";\n    border-color: ").concat(theme.sidebarIsOpenBottomBorderColor || 'inherit', ";\n    padding-left: 0;\n  }\n\n.Collection__styles__sidebarIsOpen .Collection__styles__itemsContainer {\n    display: block;\n  }\n");
  },
  'column': 'Collection__styles__column',
  'itemsContainer': 'Collection__styles__itemsContainer',
  'sidebarIsClosed': 'Collection__styles__sidebarIsClosed',
  'hideAll': 'Collection__styles__hideAll',
  'sidebarIsOpen': 'Collection__styles__sidebarIsOpen',
  'topBorder': 'Collection__styles__topBorder'
};
describe('Common Stimulus Sidebar Collection Presenter', function () {
  var itemCollection = (0, _immutable.fromJS)([{}, {}, {}]);
  var renderSidebarItemStub = sinon.stub();
  var setSidebarStimulusIsShowingStub = sinon.stub();
  var props = {
    firstPosition: 1,
    isShowingItems: false,
    sidebarOpen: true,
    renderSidebarItem: renderSidebarItemStub,
    stimulusId: '1',
    itemCollection: itemCollection,
    setSidebarStimulusIsShowing: setSidebarStimulusIsShowingStub
  };
  afterEach(function () {
    renderSidebarItemStub.resetHistory();
    setSidebarStimulusIsShowingStub.resetHistory();
  });
  var testbed = createTestbed(_presenter.default, props);

  var sharedItemsRenderExample = function sharedItemsRenderExample(itemCollection) {
    var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    it('renders items', function () {
      testbed.render((0, _objectSpread2.default)({
        itemCollection: itemCollection
      }, props));
      expect(renderSidebarItemStub.callCount).to.eq(itemCollection.size);
    });
  };

  it('calls the render function with the right display positions on Build', function () {
    testbed.render({
      itemCollection: (0, _immutable.List)([new _QuizEntry.default({
        entryType: 'Bank',
        properties: (0, _immutable.Map)({
          sampleNum: 3
        })
      }), new _QuizEntry.default()])
    });
    expect(renderSidebarItemStub.firstCall.args[1]).to.equal(1);
    expect(renderSidebarItemStub.firstCall.args[2]).to.equal(true);
    expect(renderSidebarItemStub.secondCall.args[1]).to.equal(4);
  });
  it('calls the render function with the right sidebarOpen argument', function () {
    testbed.render({
      sidebarOpen: false,
      itemCollection: (0, _immutable.List)([new _QuizEntry.default({
        entryType: 'Bank',
        properties: (0, _immutable.Map)({
          sampleNum: 3
        })
      }), new _QuizEntry.default()])
    });
    expect(renderSidebarItemStub.firstCall.args[2]).to.equal(false);
  });
  it('calls the render function with the right display positions on Take', function () {
    testbed.render({
      itemCollection: (0, _immutable.List)([new _SessionItem.default(), new _SessionItem.default()])
    });
    expect(renderSidebarItemStub.firstCall.args[1]).to.equal(1);
    expect(renderSidebarItemStub.secondCall.args[1]).to.equal(2);
  });
  context('with more than 5 items', function () {
    var items = (0, _immutable.fromJS)([{}, {}, {}, {}, {}, {}]);
    context('with isShowingItems true', function () {
      sharedItemsRenderExample(items, {
        isShowingItems: true
      });
      it('renders an iconUpArrow', function () {
        testbed.render({
          itemCollection: items,
          isShowingItems: true
        });
        expect(testbed.findChildrenByType(_IconArrowOpenUp.default)).to.not.be.empty();
      });
    });
    context('with sidebarOpen true and not showing items', function () {
      it('renders a div with hideAll applied', function () {
        testbed.render({
          itemCollection: items,
          sidebarOpen: true
        });
        expect(testbed.findChildrenByClass(styles.hideAll)).to.not.be.empty();
      });
    });
    context('with sidebarOpen false', function () {
      it('renders a div with hideAll applied', function () {
        testbed.render({
          itemCollection: items,
          sidebarOpen: false
        });
        expect(testbed.findChildrenByClass(styles.hideAll)).to.not.be.empty();
      });
      context('with isShowingItems true', function () {
        sharedItemsRenderExample(items, {
          isShowingItems: true,
          sidebarOpen: false
        });
        it('renders an iconUpArrow', function () {
          testbed.render({
            itemCollection: items,
            isShowingItems: true,
            sidebarOpen: false
          });
          expect(testbed.findChildrenByType(_IconArrowOpenUp.default)).to.not.be.empty();
        });
      });

      var sharedRender = function sharedRender() {
        testbed.render({
          itemCollection: items,
          sidebarOpen: false
        });
      };

      it('renders an IconDownArrow', function () {
        sharedRender();
        expect(testbed.findChildrenByType(_IconArrowOpenDown.default)).to.not.be.empty();
      });
      it('renders a summary of the items', function () {
        sharedRender();
        expect(testbed.findChildrenByType(_PositionSummary.default)).to.not.be.empty();
      });
      it('renders the first and last position of the items', function () {
        sharedRender();
        var text = testbed.getText();
        expect(text).to.include(props.firstPosition);
        expect(text).to.include(props.firstPosition + items.size - 1);
      });
      it('does not render intermediate position numbers', function () {
        sharedRender();
        var text = testbed.getText();
        expect(text).to.not.include(props.firstPosition + 1);
      });
    });
    sharedItemsRenderExample(items);
  });
  context('with fewer than 6 items', function () {
    var items = (0, _immutable.fromJS)([{}, {}, {}]);

    var sharedRenderExample = function sharedRenderExample(props, transitionIn) {
      it('hides the arrows', function () {
        testbed.render((0, _objectSpread2.default)({
          itemCollection: items
        }, props));
        expect(testbed.findChildrenByClass(styles.hideAll).length).to.eq(2);
        expect(testbed.findChildrenByType(_Transition.default)[0].props.in).to.eq(transitionIn);
      });
    };

    context('with isShowingItems true', function () {
      sharedItemsRenderExample(items, {
        isShowingItems: true
      });
      sharedRenderExample({
        isShowingItems: true
      }, false);
    });
    context('with sidebarOpen false', function () {
      sharedItemsRenderExample(items, {
        sidebarOpen: false
      });
      sharedRenderExample({
        sidebarOpen: false
      }, true);
    });
    context('with sidebarOpen true and not showing items', function () {
      sharedItemsRenderExample(items, {
        sidebarOpen: true
      });
      sharedRenderExample({
        sidebarOpen: true
      }, true);
    });
    sharedItemsRenderExample(items);
    sharedRenderExample({}, true);
  });
  it('sets the sidebar showing items to false when clicking ArrowOpenDown arrow', function () {
    testbed.render();

    var arrow = _reactDom.default.findDOMNode(testbed.findChildByType(_IconArrowOpenDown.default));

    _testUtils.default.Simulate.click(arrow);

    expect(setSidebarStimulusIsShowingStub.calledWith('1', true)).to.be.true();
  });
  it('sets the sidebar showing items to false when clicking ArrowOpenUp arrow', function () {
    testbed.render();

    var arrow = _reactDom.default.findDOMNode(testbed.findChildByType(_IconArrowOpenUp.default));

    _testUtils.default.Simulate.click(arrow);

    expect(setSidebarStimulusIsShowingStub.calledWith('1', false)).to.be.true();
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});