"use strict";

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

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

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

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

var _index = _interopRequireDefault(require("../index"));

describe('AddToBankOptions', function () {
  var sandbox = sinon.createSandbox();
  var openModalStub = sandbox.stub();
  var screenreaderNotificationStub = sandbox.stub();
  var showErrorStub = sandbox.stub();
  afterEach(function () {
    sandbox.resetHistory();
  });
  var guid = '12345';
  var defaultProps = {
    displayPosition: 1,
    entryType: 'Item',
    guid: guid,
    isValid: true,
    openModal: openModalStub,
    screenreaderNotification: screenreaderNotificationStub,
    showError: showErrorStub
  };
  var testbed = createTestbed(_index.default, defaultProps);

  var clickButton = function clickButton() {
    var button = testbed.findChildByType(_Button.default);
    var buttonNode = testbed.findChildrenByTagName('button', button)[0];

    _testUtils.default.Simulate.click(buttonNode);
  };

  it('renders a button with the correct text', function () {
    testbed.render();
    var button = testbed.findChildByType(_Button.default);
    expect(button).to.not.be.undefined();
    var text = testbed.getText();
    expect(text).to.include('Add to Bank');
  });
  describe('opening bank', function () {
    context('with valid entry', function () {
      it('calls openModal with the correct props', function () {
        testbed.render();
        clickButton();
        expect(openModalStub.calledWith([_constants.ADD_TO_BANK_MODAL, 'Item', guid].join(':'))).to.be.true();
      });
    });
    context('with invalid entry', function () {
      it('calls screenreaderNotification with the expected message', function () {
        testbed.render({
          isValid: false
        });
        clickButton();
        var expectedMsg = 'Item 1 cannot be saved due to invalid input data.';
        expect(openModalStub).not.to.have.been.called();
        expect(screenreaderNotificationStub.calledWith(expectedMsg)).to.be.true();
      });
      it('calls showError with the guid', function () {
        testbed.render({
          isValid: false
        });
        clickButton();
        expect(openModalStub).not.to.have.been.called();
        expect(showErrorStub.calledWith(guid)).to.be.true();
      });
    });
  });
});