"use strict";

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

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

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

var _react = _interopRequireDefault(require("react"));

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

var _uiTestUtils = require("@instructure/ui-test-utils");

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

describe('LearnosityCalculator', function () {
  var render = function render(_ref) {
    var toggleCalculator = _ref.toggleCalculator,
        enabled = _ref.enabled;
    return _react.default.createElement(_Button.default, {
      onClick: toggleCalculator,
      disabled: !enabled
    }, "Hello");
  };

  var props = {
    activeCalculatorId: null,
    appendFeature: Function.prototype,
    calculatorType: 'none',
    destroyFeature: Function.prototype,
    featureId: "calc_".concat(new Date().getTime()),
    quizSessionId: '42',
    render: render,
    setActiveCalculatorId: Function.prototype,
    toggleFeature: Function.prototype
  };
  it('does not add content from render prop when type is none',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, props));

          case 2:
            _context.t0 = expect;
            _context.next = 5;
            return (0, _uiTestUtils.findAll)('button', {
              expectEmpty: true
            });

          case 5:
            _context.t1 = _context.sent;
            (0, _context.t0)(_context.t1).to.have.length(0);

          case 7:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('adds content from render prop when type is basic',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee2() {
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic"
            })));

          case 2:
            _context2.t0 = expect;
            _context2.next = 5;
            return (0, _uiTestUtils.findAll)('button');

          case 5:
            _context2.t1 = _context2.sent;
            (0, _context2.t0)(_context2.t1).to.have.length(1);

          case 7:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('appends the calculator',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee3() {
    var appendFeature, feature;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            appendFeature = (0, _uiTestUtils.stub)();
            _context3.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              appendFeature: appendFeature,
              calculatorType: "basic"
            })));

          case 3:
            feature = {
              type: 'calculator',
              mode: 'basic',
              feature_id: props.featureId
            };
            expect(appendFeature).to.have.been.calledOnce();
            expect(appendFeature).to.have.been.calledWithMatch(props.quizSessionId, feature);

          case 6:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it('sets its own feature id if none is provided',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee4() {
    var appendFeature;
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            appendFeature = (0, _uiTestUtils.stub)();
            _context4.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              appendFeature: appendFeature,
              calculatorType: "basic",
              featureId: null
            })));

          case 3:
            expect(appendFeature).to.have.been.calledOnce();
            expect(appendFeature.firstCall.args[1].feature_id).to.be.a('string');

          case 5:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  })));
  it('destroys the calculator when component is unmounted',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee5() {
    var destroyFeature;
    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            destroyFeature = (0, _uiTestUtils.stub)();
            _context5.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic",
              destroyFeature: destroyFeature
            })));

          case 3:
            _context5.next = 5;
            return (0, _uiTestUtils.mount)(_react.default.createElement("div", null));

          case 5:
            _context5.t0 = expect;
            _context5.next = 8;
            return (0, _uiTestUtils.findAll)('.learnosity-feature', {
              expectEmpty: true
            });

          case 8:
            _context5.t1 = _context5.sent;
            (0, _context5.t0)(_context5.t1).to.have.length(0);
            expect(destroyFeature).to.have.been.calledOnceWith(props.featureId);

          case 11:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  })));
  it('updates component when the type is changed',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee6() {
    return _regenerator.default.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            _context6.next = 2;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, props));

          case 2:
            _context6.t0 = expect;
            _context6.next = 5;
            return (0, _uiTestUtils.findAll)('button', {
              expectEmpty: true
            });

          case 5:
            _context6.t1 = _context6.sent;
            (0, _context6.t0)(_context6.t1).to.have.length(0);
            _context6.next = 9;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "scientific"
            })));

          case 9:
            _context6.t2 = expect;
            _context6.next = 12;
            return (0, _uiTestUtils.findAll)('button');

          case 12:
            _context6.t3 = _context6.sent;
            (0, _context6.t2)(_context6.t3).to.have.length(1);
            _context6.next = 16;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, props));

          case 16:
            _context6.t4 = expect;
            _context6.next = 19;
            return (0, _uiTestUtils.findAll)('button', {
              expectEmpty: true
            });

          case 19:
            _context6.t5 = _context6.sent;
            (0, _context6.t4)(_context6.t5).to.have.length(0);

          case 21:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  })));
  it('passes disabled before learnosity authenticated',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee7() {
    var appendFeature, button;
    return _regenerator.default.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            appendFeature = function appendFeature() {
              return new Promise();
            };

            _context7.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              appendFeature: appendFeature,
              calculatorType: "basic"
            })));

          case 3:
            _context7.next = 5;
            return (0, _uiTestUtils.find)('button');

          case 5:
            button = _context7.sent;
            expect(button.getDOMNode().disabled).to.be.true();

          case 7:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  })));
  it('passes enabled when learnosity authenticated',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee8() {
    var button;
    return _regenerator.default.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            _context8.next = 2;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic"
            })));

          case 2:
            _context8.next = 4;
            return (0, _uiTestUtils.find)('button');

          case 4:
            button = _context8.sent;
            expect(button.getDOMNode().disabled).to.be.false();

          case 6:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  })));
  it('passes function to toggle calculator',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee9() {
    var toggleFeature, button;
    return _regenerator.default.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            toggleFeature = (0, _uiTestUtils.stub)();
            _context9.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic",
              toggleFeature: toggleFeature
            })));

          case 3:
            _context9.next = 5;
            return (0, _uiTestUtils.find)('button');

          case 5:
            button = _context9.sent;
            _context9.next = 8;
            return button.click();

          case 8:
            expect(toggleFeature).to.have.been.calledOnceWith(props.featureId);

          case 9:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  })));
  it('sets active calculator id when activated',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee10() {
    var setActiveCalculatorId, button;
    return _regenerator.default.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            setActiveCalculatorId = (0, _uiTestUtils.stub)();
            _context10.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic",
              setActiveCalculatorId: setActiveCalculatorId
            })));

          case 3:
            _context10.next = 5;
            return (0, _uiTestUtils.find)('button');

          case 5:
            button = _context10.sent;
            _context10.next = 8;
            return button.click();

          case 8:
            expect(setActiveCalculatorId).to.have.been.calledOnceWith(props.featureId);

          case 9:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  })));
  it('toggles calculator off when active calculator id changes',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee11() {
    var toggleFeature, subject;
    return _regenerator.default.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            toggleFeature = (0, _uiTestUtils.stub)();
            _context11.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic",
              toggleFeature: toggleFeature
            })));

          case 3:
            subject = _context11.sent;
            _context11.next = 6;
            return subject.setProps({
              activeCalculatorId: 'abc'
            });

          case 6:
            expect(toggleFeature).to.have.been.calledOnceWith(props.featureId, false);

          case 7:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  })));
  it('does not toggle calculator off when active calculator id changes to self',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee12() {
    var toggleFeature, subject;
    return _regenerator.default.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            toggleFeature = (0, _uiTestUtils.stub)();
            _context12.next = 3;
            return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
              calculatorType: "basic",
              toggleFeature: toggleFeature
            })));

          case 3:
            subject = _context12.sent;
            _context12.next = 6;
            return subject.setProps({
              activeCalculatorId: props.featureId
            });

          case 6:
            expect(toggleFeature).not.to.have.been.called();

          case 7:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  })));
  context('when no quiz session id is given', function () {
    it('does not render anything',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee13() {
      var subject;
      return _regenerator.default.wrap(function _callee13$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              _context13.next = 2;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
                calculatorType: "basic",
                quizSessionId: null
              })));

            case 2:
              subject = _context13.sent;
              expect(subject.getDOMNode()).not.to.exist();

            case 4:
            case "end":
              return _context13.stop();
          }
        }
      }, _callee13);
    })));
    it('appends the calculator if a quiz session id is subsequently provided',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee14() {
      var appendFeature, subject;
      return _regenerator.default.wrap(function _callee14$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              appendFeature = (0, _uiTestUtils.stub)();
              _context14.next = 3;
              return (0, _uiTestUtils.mount)(_react.default.createElement(_index.default, Object.assign({}, props, {
                appendFeature: appendFeature,
                calculatorType: "basic",
                quizSessionId: null
              })));

            case 3:
              subject = _context14.sent;
              _context14.next = 6;
              return subject.setProps({
                quizSessionId: props.quizSessionId
              });

            case 6:
              expect(appendFeature).to.have.been.calledOnce();

            case 7:
            case "end":
              return _context14.stop();
          }
        }
      }, _callee14);
    })));
  });
});