"use strict";

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

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

var _immutable = require("immutable");

var _Menu = _interopRequireDefault(require("@instructure/ui-menu/lib/components/Menu"));

var _MenuItem = _interopRequireDefault(require("@instructure/ui-menu/lib/components/Menu/MenuItem"));

var _ScreenReaderContent = _interopRequireDefault(require("@instructure/ui-a11y/lib/components/ScreenReaderContent"));

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

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

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

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

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

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

describe('DragHandle', function () {
  var stubGetEntry = function stubGetEntry(quizEntry, status) {
    sinon.stub(quizEntry, 'getEntry').returns(new _Item.default({
      status: status || 'mutable'
    }));
    return quizEntry;
  };

  var tempQuizEntry = new _QuizEntry.default({
    id: _constants.TEMPORARY_QUIZ_ENTRY_ID
  });
  var quizEntry1 = stubGetEntry(new _QuizEntry.default({
    position: 1
  }));
  var quizEntry2 = new _QuizEntry.default({
    position: 2
  });
  sinon.stub(quizEntry2, 'getEntry').returns(new _Item.default({
    id: '22',
    itemBody: '<div>Hello</div>'
  }));
  var quizEntry3 = stubGetEntry(new _QuizEntry.default({
    position: 3
  }));
  var stimulusQuizEntry1 = stubGetEntry(new _QuizEntry.default({
    id: 5,
    entryType: 'Stimulus',
    position: 1
  }));
  var stimulusQuizEntry2 = stubGetEntry(new _QuizEntry.default({
    id: 6,
    entryType: 'Stimulus',
    position: 2
  }));
  var stimulusQuizEntry3 = stubGetEntry(new _QuizEntry.default({
    id: 7,
    entryType: 'Stimulus',
    position: 3
  }));
  var quizEntryInStimulus1 = stubGetEntry(new _QuizEntry.default({
    stimulusQuizEntryId: 5,
    position: 2
  }));
  var quizEntryInStimulus2 = stubGetEntry(new _QuizEntry.default({
    stimulusQuizEntryId: 6,
    position: 2
  }));
  sinon.stub(stimulusQuizEntry1, 'getNestedEntries').returns((0, _immutable.List)([1]));
  sinon.stub(stimulusQuizEntry2, 'getNestedEntries').returns((0, _immutable.List)([1]));
  var immutableQuizEntryInStimulus1 = stubGetEntry(new _QuizEntry.default({
    entryType: 'Item',
    stimulusQuizEntry: 5,
    position: 2
  }), 'immutable');
  var SRNotificationStub = sinon.stub();
  var moveQuizEntryStub = sinon.stub();
  var setUIStub = sinon.stub();
  var clearUIStub = sinon.stub();
  var openMoveImmutableModalStub = sinon.stub();
  var defaultProps = {
    clearUI: clearUIStub,
    disabled: false,
    displayPosition: 1,
    onSidebar: false,
    quizEntries: (0, _immutable.List)([quizEntry1, quizEntry2, quizEntry3]),
    quizEntry: quizEntry2,
    quizId: '1',
    screenreaderNotification: SRNotificationStub,
    setUI: setUIStub,
    openMoveImmutableModal: openMoveImmutableModalStub,
    moveQuizEntry: moveQuizEntryStub,
    moveTemporaryQuizEntry: moveQuizEntryStub
  };
  var testbed = createTestbed(_presenter.default, defaultProps);
  afterEach(function () {
    moveQuizEntryStub.resetHistory();
    SRNotificationStub.resetHistory();
    setUIStub.resetHistory();
    clearUIStub.resetHistory();
    openMoveImmutableModalStub.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _presenter.default);
  describe('#conditionalFocus', function () {
    it('focuses the trigger when movedQuizEntry matches quizEntry id', function () {
      testbed.render({
        movedQuizEntry: defaultProps.quizEntry.id,
        movedInSidebar: defaultProps.onSidebar
      });

      var trigger = _reactDom.default.findDOMNode(testbed.findChildByType(_Button.default));

      expect(document.activeElement).to.equal(trigger);
    });
    it('calls clearUI after focusing', function () {
      testbed.render({
        movedQuizEntry: defaultProps.quizEntry.id,
        movedInSidebar: defaultProps.onSidebar
      });
      expect(clearUIStub).to.have.been.called();
    });
  });
  describe('#renderIcon', function () {
    it('returns an IconDragHandleLine component', function () {
      testbed.render();
      var iconComponents = testbed.findChildByType(_IconDragHandle.default);
      expect(iconComponents).to.not.be.empty();
    });
    it('returns a ScreenReaderContent component', function () {
      testbed.render();
      var screenReaderComponents = testbed.findChildByType(_ScreenReaderContent.default);
      expect(screenReaderComponents).to.not.be.empty();
    });
  });
  describe('#renderMenuItems', function () {
    context('when isFirstItem and isLastItem are both true', function () {
      it('does not render Menu', function () {
        testbed.render({
          quizEntry: new _QuizEntry.default({
            position: 1
          }),
          quizEntries: (0, _immutable.List)([1])
        });
        var popoverMenu = testbed.findChildrenByType(_Menu.default);
        expect(popoverMenu).to.be.empty();
      });
    });
    context('when disabled', function () {
      it('does not render Menu', function () {
        testbed.render({
          disabled: true
        });
        var popoverMenu = testbed.findChildrenByType(_Menu.default);
        expect(popoverMenu).to.be.empty();
      });
    });
    context('when isFirstItem and isLastItem are not both true', function () {
      it('renders popovermenu', function () {
        testbed.render({
          quizEntry: new _QuizEntry.default({
            position: 1
          }),
          quizEntries: (0, _immutable.List)([1, 2])
        });
        var popoverMenu = testbed.findChildrenByType(_Menu.default);
        expect(popoverMenu).to.not.be.empty();
      });
      it('renders all move menu items', function () {
        testbed.render({
          quizEntry: new _QuizEntry.default({
            position: 2
          }),
          quizEntries: (0, _immutable.List)([1, 2, 3])
        });
        expect(testbed.findChildByType(_presenter.default).renderMenuItems().length).to.eq(4);
      });
      context('is the first item', function () {
        it('renders only two menu items', function () {
          testbed.render({
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1, 2])
          });
          var popoverMenuItems = testbed.findChildByType(_presenter.default).renderMenuItems();
          expect(popoverMenuItems.length).to.eq(2);
        });
        it('menu items only contain down as option', function () {
          testbed.render({
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1, 2])
          });
          var popoverMenuItems = testbed.findChildByType(_presenter.default).renderMenuItems();
          expect(popoverMenuItems[0].props.children).to.include('Move Down');
          expect(popoverMenuItems[1].props.children).to.include('Move to Bottom');
        });
      });
      context('is the last item', function () {
        it('renders only two menu items', function () {
          testbed.render({
            quizEntry: new _QuizEntry.default({
              position: 2
            }),
            quizEntries: (0, _immutable.List)([1, 2])
          });
          var popoverMenuItems = testbed.findChildByType(_presenter.default).renderMenuItems();
          expect(popoverMenuItems.length).to.eq(2);
        });
        it('menu items only contain up as option', function () {
          testbed.render({
            quizEntry: new _QuizEntry.default({
              position: 2
            }),
            quizEntries: (0, _immutable.List)([1, 2])
          });
          var popoverMenuItems = testbed.findChildByType(_presenter.default).renderMenuItems();
          expect(popoverMenuItems[0].props.children).to.include('Move to Top');
          expect(popoverMenuItems[1].props.children).to.include('Move Up');
        });
      });
    });
    it('returns an array of MenuItems', function () {
      var output = testbed.renderShallowly();
      expect(output.find(_MenuItem.default).length).to.eq(4);
    });
  });
  describe('#renderConditionally', function () {
    context('when isFirstItem and isLastItem are not both true', function () {
      context('disabled set to false', function () {
        it('renders Menu', function () {
          testbed.render({
            disabled: false,
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1, 2])
          });
          var popoverMenu = testbed.findChildByType(_Menu.default);
          expect(popoverMenu).to.not.be.undefined();
        });
      });
      context('disabled set to true', function () {
        it('does not render Menu', function () {
          testbed.render({
            disabled: true,
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1])
          });
          var popoverMenu = testbed.findChildrenByType(_Menu.default);
          expect(popoverMenu).to.be.empty();
        });
      });
    });
    context('when isFirstItem and isLastItem are both true', function () {
      context('disabled set to false', function () {
        it('does not render Menu', function () {
          testbed.render({
            disabled: false,
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1])
          });
          var popoverMenu = testbed.findChildrenByType(_Menu.default);
          expect(popoverMenu).to.be.empty();
        });
      });
      context('disabled set to true', function () {
        it('does not render Menu', function () {
          testbed.render({
            disabled: true,
            quizEntry: new _QuizEntry.default({
              position: 1
            }),
            quizEntries: (0, _immutable.List)([1])
          });
          var popoverMenu = testbed.findChildrenByType(_Menu.default);
          expect(popoverMenu).to.be.empty();
        });
      });
    });
  });
  describe('#moveUp', function () {
    it('changes the item position by -1', function () {
      testbed.render();
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveUp();
      expect(moveQuizEntryStub.getCall(0).args[1]).to.eq(1);
    });
    it('moves out of stimulus if at the top', function () {
      testbed.render({
        quizEntry: quizEntryInStimulus1,
        quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntryInStimulus1])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveUp();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(1);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq('');
    });
    it('moves into stimulus if next item is in stimulus', function () {
      testbed.render({
        quizEntry: quizEntry3,
        quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntryInStimulus1, quizEntry3])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveUp();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(3);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq(5);
    });
    it('moves into stimulus if next item is empty stimulus', function () {
      testbed.render({
        quizEntry: quizEntry2,
        quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntry2])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveUp();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(2);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq(5);
    });
    context('moving stimulus', function () {
      it('moves around other empty stimulus', function () {
        testbed.render({
          quizEntry: stimulusQuizEntry2,
          quizEntries: (0, _immutable.List)([stimulusQuizEntry1, stimulusQuizEntry2])
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveUp();
        expect(moveQuizEntryStub.firstCall.args[1]).to.eq(1);
      });
      it('moves around other stimulus with item', function () {
        testbed.render({
          quizEntry: stimulusQuizEntry3,
          quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntryInStimulus1, stimulusQuizEntry3])
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveUp();
        expect(moveQuizEntryStub.firstCall.args[1]).to.eq(1);
      });
    });
  });
  describe('#moveDown', function () {
    it('changes the item position by +1', function () {
      testbed.render();
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveDown();
      expect(moveQuizEntryStub.getCall(0).args[1]).to.eq(3);
    });
    it('moves out of stimulus if at the bottom with no question after', function () {
      testbed.render({
        quizEntry: quizEntryInStimulus1,
        quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntryInStimulus1])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveDown();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(2);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq('');
    });
    it('moves out of stimulus if at the bottom with question after', function () {
      testbed.render({
        quizEntry: quizEntryInStimulus1,
        quizEntries: (0, _immutable.List)([stimulusQuizEntry1, quizEntryInStimulus1, quizEntry3])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveDown();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(2);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq('');
    });
    it('moves into stimulus if next item is stimulus', function () {
      testbed.render({
        quizEntry: quizEntry1,
        quizEntries: (0, _immutable.List)([quizEntry1, stimulusQuizEntry2])
      });
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveDown();
      expect(moveQuizEntryStub.firstCall.args[1]).to.eq(2);
      expect(moveQuizEntryStub.firstCall.args[2]).to.eq(6);
    });
    context('moving stimulus', function () {
      it('moves around other empty stimulus', function () {
        testbed.render({
          quizEntry: stimulusQuizEntry1,
          quizEntries: (0, _immutable.List)([stimulusQuizEntry1, stimulusQuizEntry2])
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveDown();
        expect(moveQuizEntryStub.firstCall.args[1]).to.eq(2);
      });
      it('moves around other stimulus with item', function () {
        testbed.render({
          quizEntry: stimulusQuizEntry1,
          quizEntries: (0, _immutable.List)([stimulusQuizEntry1, stimulusQuizEntry2, quizEntryInStimulus2])
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveDown();
        expect(moveQuizEntryStub.firstCall.args[1]).to.eq(3);
      });
    });
  });
  describe('#moveToTop', function () {
    it('changes the item position to 1', function () {
      testbed.render();
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveToTop();
      expect(moveQuizEntryStub.getCall(0).args[1]).to.eq(1);
    });
    context('with temporary item', function () {
      it('changes the item position to 1', function () {
        testbed.render({
          quizEntry: tempQuizEntry
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveToTop();
        expect(moveQuizEntryStub.getCall(0).args[0]).to.eq(1);
      });
    });
  });
  describe('#moveToBottom', function () {
    it('changes the item position to last position', function () {
      testbed.render();
      var subject = testbed.findChildByType(_presenter.default);
      subject.moveToBottom();
      expect(moveQuizEntryStub.getCall(0).args[1]).to.eq(3);
    });
    context('with temporary item', function () {
      it('changes the item position to last position', function () {
        testbed.render({
          quizEntry: tempQuizEntry
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveToBottom();
        expect(moveQuizEntryStub.getCall(0).args[0]).to.eq(3);
      });
    });
  });
  describe('#moveQuizEntry', function () {
    context('with an immutable item', function () {
      var moveImmutable = function moveImmutable() {
        testbed.render({
          quizEntry: immutableQuizEntryInStimulus1,
          quizEntries: (0, _immutable.List)([stimulusQuizEntry1, immutableQuizEntryInStimulus1])
        });
        var subject = testbed.findChildByType(_presenter.default);
        subject.moveToTop();
      };

      it('sets move immutable data correctly', function () {
        moveImmutable();
        expect(setUIStub.firstCall.args[1].toJS()).to.deep.eq({
          movedInSidebar: false,
          quizId: '1',
          position: 1,
          stimulusQuizEntryId: '',
          quizEntryId: '0'
        });
      });
      it('opens the moveImmutable modal', function () {
        moveImmutable();
        expect(openMoveImmutableModalStub).to.be.called();
      });
      it('does not call moveQuizEntry', function () {
        moveImmutable();
        expect(moveQuizEntryStub).not.to.have.been.called();
      });
    });
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done, {
        ignores: ['label', 'label-title-only']
      });
    });
  });
});