"use strict";

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

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

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

var _immutable = require("immutable");

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

var _Link = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Link"));

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

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

var _SDKApp = require("../../../../common/components/SDKApp");

var _featureCheck = require("../../../../common/util/featureCheck");

var _formatMessage = _interopRequireDefault(require("@instructure/quiz-common/lib/format-message"));

var styles = {
  componentId: 'eShTv',
  template: function template(theme) {
    return "\n\n.BanksList__styles__bankRow {\n  display: flex;\n  flex-direction: row;\n  border-bottom: ".concat(theme.bankRowBorderWidth || 'inherit', " solid ").concat(theme.bankRowBorderColor || 'inherit', ";\n}\n\n.BanksList__styles__bankInfo {\n  margin: ").concat(theme.bankInfoMargin || 'inherit', " 0;\n  flex: 1;\n}\n\n.BanksList__styles__bankTitle {\n  font-size: ").concat(theme.bankTitleFontSize || 'inherit', ";\n  padding-bottom: ").concat(theme.bankTitlePadding || 'inherit', ";\n}\n\n.BanksList__styles__bankDetails {\n  color: ").concat(theme.bankDetailsColor || 'inherit', ";\n  font-size: ").concat(theme.bankDetailsFontSize || 'inherit', ";\n}\n\n.BanksList__styles__bankOptions {\n  margin: ").concat(theme.bankOptionsMargin || 'inherit', " 0;\n}\n\n.BanksList__styles__bankOptionIcon {\n  vertical-align: middle;\n  margin-right: ").concat(theme.BankOptionIconMargin || 'inherit', ";\n  font-size: ").concat(theme.BankOptionIconFontSize || 'inherit', ";\n}\n\n.BanksList__styles__deleteAlert {\n  display: block;\n}\n\n.BanksList__styles__deletePreface {\n  margin-bottom: ").concat(theme.deletePrefaceMargin || 'inherit', ";\n}\n");
  },
  'bankRow': 'BanksList__styles__bankRow',
  'bankInfo': 'BanksList__styles__bankInfo',
  'bankTitle': 'BanksList__styles__bankTitle',
  'bankDetails': 'BanksList__styles__bankDetails',
  'bankOptions': 'BanksList__styles__bankOptions',
  'bankOptionIcon': 'BanksList__styles__bankOptionIcon',
  'deleteAlert': 'BanksList__styles__deleteAlert',
  'deletePreface': 'BanksList__styles__deletePreface'
};
describe('BanksList', function () {
  _formatMessage.default.setup({
    formats: (0, _SDKApp.generateFormats)('America/Denver')
  });

  var navigateToBankStub = sinon.stub();
  var openModalStub = sinon.stub();
  var withConfirmStub = sinon.stub();
  var deleteBankStub = sinon.stub();
  var focusCreateBankButtonStub = sinon.stub();
  var bank1 = new _Bank.default({
    id: '1',
    updatedAt: new Date('2017-03-14 14:01 UTC'),
    title: 'Algebraic',
    sharedCount: 2
  });
  var bank2 = new _Bank.default({
    id: '999',
    updatedAt: Date.now(),
    title: 'Mathematical',
    sharedCount: 0
  });
  var banks = (0, _immutable.List)([bank1, bank2]);
  var testbed = createTestbed(withContext(_presenter.default), {
    banks: banks,
    navigateToBank: navigateToBankStub
  });
  var shallowBanksListTestbed = createTestbed(_presenter.default, {
    banks: banks,
    focusCreateBankButton: focusCreateBankButtonStub,
    deleteBank: deleteBankStub,
    navigateToBank: navigateToBankStub,
    openModal: openModalStub,
    withConfirm: withConfirmStub
  });
  afterEach(function () {
    deleteBankStub.resetHistory();
    focusCreateBankButtonStub.resetHistory();
    navigateToBankStub.resetHistory();
    openModalStub.resetHistory();
    withConfirmStub.resetHistory();
  });
  it('renders each bank', function () {
    testbed.render();
    var rows = testbed.findChildrenByClass(styles.bankInfo);
    expect(rows.length).to.eq(2);
    expect(testbed.getText(rows[0])).to.include('Algebraic');
    expect(testbed.getText(rows[0])).to.include('Updated Mar 14');
  });
  it('navigates to bank', function () {
    testbed.render();
    var rows = testbed.findChildrenBySelector(".".concat(styles.bankRow, " button"));

    _testUtils.default.Simulate.click(rows[2]);

    expect(navigateToBankStub.calledWith('999')).to.be.true();
  });
  it('opens edit bank modal', function () {
    var shallowTestbed = shallowBanksListTestbed.renderShallowly();
    shallowTestbed.instance().openModal({
      id: '2'
    })();
    expect(openModalStub.calledWith('EDIT_BANK_MODAL:2')).to.be.true();
  });
  it('renders edit and delete options', function () {
    testbed.render();
    var menu = testbed.findChildrenByType(_Menu.default)[0];
    expect(menu.props.children.length).to.equal(2);
  });
  describe('deleteBank', function () {
    it('opens a confirmation modal when deleting a bank', function () {
      var shallowTestbed = shallowBanksListTestbed.renderShallowly();
      shallowTestbed.instance().openDeleteBankModal('1')();
      expect(withConfirmStub).to.have.been.called();
    });
    it('calls delete bank', function () {
      var shallowTestbed = shallowBanksListTestbed.renderShallowly();
      shallowTestbed.instance().deleteBank('1');
      expect(deleteBankStub).to.have.been.calledWith('1');
    });
    it('focuses the create bank button', function (done) {
      var shallowTestbed = shallowBanksListTestbed.renderShallowly();
      shallowTestbed.instance().deleteBank('1');
      setTimeout(function () {
        expect(focusCreateBankButtonStub).to.have.been.called();
        done();
      }, 150);
    });
  });
  describe('sharing', function () {
    (0, _featureCheck.withTestFeatures)(['item_bank_sharing']);
    it('should render the link to share', function () {
      testbed.render();
      expect(testbed.getText()).to.include('Share');
    });
    it('should open the modal when the link is clicked', function () {
      var subject = testbed.render();
      var shareLink = testbed.findChildrenByType(_Link.default)[3];
      shareLink.props.onClick();
      expect(subject.refs.composedComponent.state.sharingModalOpen).to.be.true();
    });
    it('should close the modal when dismissed', function () {
      var subject = testbed.render();
      var comp = subject.refs.composedComponent;
      comp.handleSharingModalDismiss();
      expect(subject.refs.composedComponent.state.sharingModalOpen).to.be.false();
    });
    it('should display +share when shareCount is 0', function () {
      testbed.render();
      var shareLink = testbed.findChildrenByType(_Link.default)[3];

      var SharedLinkNode = _reactDom.default.findDOMNode(shareLink);

      expect(SharedLinkNode.innerText).to.include('+ Share');
    });
    it('should display Sharing(shareCount) when shareCount > 0', function () {
      testbed.render();
      var shareLink = testbed.findChildrenByType(_Link.default)[1];

      var SharedLinkNode = _reactDom.default.findDOMNode(shareLink);

      expect(SharedLinkNode.innerText).to.include('Sharing (2)');
    });
  });
  it('should meet a11y standards', function (done) {
    testbed.render();
    testbed.checkA11yStandards(done, {
      ignores: ['aria-allowed-role' // TODO: remove this when instui fixes Select
      ]
    });
  });
});