"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 _sinon = _interopRequireDefault(require("sinon"));

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

describe('learnosity', function () {
  var sandbox = _sinon.default.createSandbox();

  var questionsApp = {
    append: sandbox.stub(),
    feature: sandbox.stub()
  };
  var learnosityApp = {
    init: sandbox.stub()
  };
  var loader = sandbox.stub();
  var fetcher = {
    post: sandbox.stub()
  };
  var calculator = {
    destroy: sandbox.stub(),
    toggle: sandbox.stub()
  };
  beforeEach(function () {
    loader.resolves(learnosityApp);
    learnosityApp.init.returns(questionsApp);
    fetcher.post.resolves();
  });
  afterEach(function () {
    sandbox.reset();
  });
  var quizSessionId = '42';
  var feature = {
    type: 'calculator',
    mode: 'basic',
    feature_id: 'calc_1553188426534'
  };
  var timeout = 200;

  function waitForLearnosityAppInit() {
    return new Promise(function (resolve) {
      var interval = setInterval(function () {
        if (learnosityApp.init.called) {
          clearInterval(interval);
          var callbacks = learnosityApp.init.firstCall.args[1];
          callbacks.readyListener();
          resolve(callbacks);
        }
      }, 50);
    });
  }

  describe('appendFeature', function () {
    context('with a feature container in the DOM', function () {
      var featureContainer;
      beforeEach(function () {
        featureContainer = document.createElement('div');
        featureContainer.className = "learnosity-feature feature-".concat(feature.feature_id);
        document.body.appendChild(featureContainer);
      });
      afterEach(function () {
        featureContainer.remove();
      });
      it('calls questionsApp.append and resolves when the learnosity ready callback is called',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        var promise, callbacks;
        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                promise = (0, _index.appendFeature)(quizSessionId, feature, timeout, loader, fetcher); // Learnosity calls the ready callback when the calculator has been
                // appended to the DOM

                _context.next = 3;
                return waitForLearnosityAppInit();

              case 3:
                callbacks = _context.sent;
                callbacks.readyListener();
                _context.next = 7;
                return expect(promise).to.be.fulfilled();

              case 7:
                expect(questionsApp.append).to.have.been.calledWithMatch({
                  features: [feature]
                });

              case 8:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      })));
      it("times out if the learnosity ready callback isn't called",
      /*#__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 expect((0, _index.appendFeature)(quizSessionId, feature, timeout, loader, fetcher)).to.be.rejectedWith(/timed out/i);

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      })));
    });
    context('without a feature container in the DOM', function () {
      it('rejects',
      /*#__PURE__*/
      (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee3() {
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.next = 2;
                return expect((0, _index.appendFeature)(quizSessionId, feature, timeout, loader, fetcher)).to.be.rejectedWith(/container element not found/i);

              case 2:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      })));
    });
  });
  describe('destroyFeature', function () {
    context('when calculator feature is found', function () {
      beforeEach(function () {
        questionsApp.feature.returns(calculator);
      });
      it('calls destroy on the feature', function () {
        (0, _index.destroyFeature)(feature.feature_id, loader, fetcher);
        expect(calculator.destroy).to.have.been.calledOnce();
      });
    });
    context('when calculator feature is not found', function () {
      it('does nothing', function () {
        (0, _index.destroyFeature)(feature.feature_id, loader, fetcher);
        expect(calculator.destroy).not.to.have.been.called();
      });
    });
  });
  describe('toggleFeature', function () {
    var _arr = [true, false, void 0];

    var _loop = function _loop() {
      var onOff = _arr[_i];
      context("with toggle state ".concat(onOff), function () {
        context('when calculator feature is found', function () {
          beforeEach(function () {
            questionsApp.feature.returns(calculator);
          });
          it('calls toggle on the feature', function () {
            (0, _index.toggleFeature)(feature.feature_id, onOff, loader, fetcher);
            expect(calculator.toggle).to.have.been.calledOnceWith(onOff);
          });
        });
        context('when calculator feature is not found', function () {
          it('does nothing', function () {
            (0, _index.toggleFeature)(feature.feature_id, onOff, loader, fetcher);
            expect(calculator.toggle).not.to.have.been.called();
          });
        });
      });
    };

    for (var _i = 0; _i < _arr.length; _i++) {
      _loop();
    }
  });
});