"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

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

var _BreadcrumbLink = _interopRequireDefault(require("@instructure/ui-breadcrumb/lib/components/Breadcrumb/BreadcrumbLink"));

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

var immutable = _interopRequireWildcard(require("immutable"));

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

var _PaginatedCollection = _interopRequireDefault(require("../../../../common/components/shared/PaginatedCollection"));

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

var _HeaderMenu = _interopRequireDefault(require("../../../../banks/components/HeaderMenu"));

var _Page = _interopRequireDefault(require("../../../../common/components/layout/Page"));

var _NavWrapper = _interopRequireDefault(require("../../NavWrapper"));

var _BankSearch = _interopRequireDefault(require("../../../../banks/components/BankSearch"));

describe('Bank', function () {
  var bank = new _Bank.default({
    id: 1,
    title: 'Units of Measurement'
  });
  var stubs = {
    addEntryToQuiz: sinon.stub().resolves(),
    getBankEntries: sinon.stub(),
    openModal: sinon.stub(),
    screenreaderNotification: sinon.stub()
  };
  var props = (0, _objectSpread2.default)({}, stubs, {
    bank: bank,
    bankId: bank.id.toString(),
    getBank: Function.prototype,
    navigateToBankEntry: Function.prototype,
    navigateToBanks: Function.prototype,
    resetSearch: Function.prototype,
    search: immutable.fromJS({
      interactionTypes: immutable.Set()
    })
  });
  var testbed = createTestbed(_presenter.default, props);
  var connectedTestbed = createTestbed(withContext(_presenter.default), props);
  afterEach(function () {
    Object.keys(stubs).forEach(function (key) {
      return stubs[key].resetHistory();
    });
  });
  it('renders a Page component', function () {
    var output = testbed.renderShallowly();
    expect(output.find(_Page.default)).to.have.length(1);
  });
  it('renders Bank app navigation', function () {
    var output = testbed.renderShallowly();
    var crumbs = output.find(_BreadcrumbLink.default);
    expect(crumbs.length).to.eq(1);
  });
  it('fetches bank entries', function (done) {
    connectedTestbed.mount();
    setTimeout(function () {
      expect(stubs.getBankEntries).to.have.been.calledWithMatch(1, false, {
        page: 1
      });
      done();
    }, 100);
  });
  it('does not render a return-to-build button when not passed a navigateToBuild prop', function () {
    var output = testbed.renderShallowly({
      navigateToBuild: null
    });
    var buttons = output.find(_Button.default);
    var texts = buttons.map(function (button) {
      return buttons.first().prop('children');
    });
    expect(texts.join()).to.not.include('Return');
  });
  it('passes the rendereHeaderMenu function to the NavWrapper', function () {
    var output = testbed.renderShallowly();
    var navWrapper = output.find(_NavWrapper.default);
    expect(navWrapper.prop('renderHeaderMenu')).to.not.be.undefined();
  });
  it('does not render Bank header menu in compact mode', function () {
    var output = testbed.renderShallowly({
      isCompact: true
    });
    expect(output.find(_HeaderMenu.default).length).to.eq(0);
  });
  it('does render addBankToQuiz button if not empty', function () {
    var output = testbed.renderShallowly({
      isCompact: true,
      bank: {
        itemEntryCount: 1
      }
    });
    var buttons = output.find(_Button.default);
    expect(buttons.length).to.equal(1);
    var buttonText = buttons.render().text();
    expect(buttonText).to.include('All / Random');
  });
  it('does not render addBankToQuiz button if empty', function () {
    var output = testbed.renderShallowly({
      isCompact: true,
      bank: {
        itemEntryCount: 0
      }
    });
    var buttons = output.find(_Button.default);
    expect(buttons.length).to.equal(0);
  });
  it('cascades addEntryToQuiz iff compact mode', function () {
    var output = testbed.renderShallowly();
    var collection = output.find(_PaginatedCollection.default).first();
    var bankEntriesList = collection.prop('renderRecords')();
    expect(bankEntriesList.props.addEntryToQuiz).to.be.null();
    var compact = testbed.renderShallowly({
      isCompact: true
    });
    var compactCollection = compact.find(_PaginatedCollection.default).first();
    var compactList = compactCollection.prop('renderRecords')();
    expect(compactList.props.addEntryToQuiz).to.equal(stubs.addEntryToQuiz);
  });
  it('opens addBankEntryModal', function () {
    var output = testbed.renderShallowly();
    var newEntryButton = output.find(_Button.default).first();
    newEntryButton.simulate('click');
    expect(stubs.openModal).to.have.been.called();
  });
  describe('addBankToQuiz', function () {
    it('calls addEntryToQuiz and alerts the screenreader', function (done) {
      var output = testbed.renderShallowly({
        isCompact: true,
        bank: {
          itemEntryCount: 1
        }
      });
      var addBankToQuizButton = output.find(_Button.default).first();
      addBankToQuizButton.simulate('click');
      expect(stubs.addEntryToQuiz).to.have.been.called();
      setTimeout(function () {
        expect(stubs.screenreaderNotification).to.have.been.called();
        done();
      });
    });
  });
  it('renders search bar if filters are present', function () {
    var output = testbed.renderShallowly({
      bank: {
        filters: {
          some: 'filters'
        }
      },
      useBankSearch: true
    });
    var search = output.find(_BankSearch.default);
    expect(search.length).to.equal(1);
  });
  it('does not render search bar when filters are not present', function () {
    var output = testbed.renderShallowly({
      bank: {
        filters: null
      }
    });
    var search = output.find(_BankSearch.default);
    expect(search.length).to.equal(0);
  });
  it('notifies the user via screen reader when search results change',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var output, bank;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            output = testbed.renderShallowly((0, _objectSpread2.default)({}, props, {
              getBankEntries: function getBankEntries() {
                return Promise.resolve({
                  totalPages: 1
                });
              }
            }));
            bank = output.instance();
            bank.debouncedAnnounceResults = bank.announceResults;
            bank.getPage(1, immutable.fromJS({}));
            _context.next = 6;
            return new Promise(function (r) {
              return setTimeout(r, 0);
            });

          case 6:
            expect(stubs.screenreaderNotification).to.have.been.calledWith('Displaying one page of results');

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('should meet a11y standards', function (done) {
    testbed.renderShallowly();
    testbed.checkA11yStandards(done);
  });
});