"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 _QuizEventsLogger = _interopRequireWildcard(require("../QuizEventsLogger"));

var _kinesisTests = require("./kinesisTests");

describe('event logging tools', function () {
  describe('QuizEventsLogger', function () {
    var fakeStreamQueue = (0, _kinesisTests.fakeStreamQueueCreator)();
    var quizEventLogger = new _QuizEventsLogger.QuizEventsLogger(fakeStreamQueue);
    beforeEach(function () {
      sinon.stub(fakeStreamQueue, 'push');
    });
    afterEach(function () {
      fakeStreamQueue.push.restore();
    }); // Public API is #enqueueEvent

    context('valid data types', function () {
      var event = {
        eventType: 'EVENT'
      };
      it('queues up data', function () {
        quizEventLogger.enqueueEvent(event);
        expect(fakeStreamQueue.push.called).to.be.true();
      });
    });
    context('invalid data types', function () {
      var event = {
        eventType: 'NOT_AN_EVENT'
      };
      it('throws up on bad data', function () {
        var trigger = function trigger() {
          quizEventLogger.enqueueEvent(event);
        };

        expect(trigger).to.throw(Error, /Unable to locate a valid StreamQueue for event type NOT_AN_EVENT/);
        expect(fakeStreamQueue.push.called).to.be.false();
      });
    });
    context('needsRefresh', function () {
      var clock = null;
      beforeEach(function () {
        clock = sinon.useFakeTimers();
      });
      afterEach(function () {
        clock.restore();
      });
      it('is false just after having been built', function () {
        expect(quizEventLogger.needsRefresh()).to.be.false();
      });
      it('is true after 10 minutes have elapsed', function () {
        var staleQuizEventsLogger = new _QuizEventsLogger.QuizEventsLogger(fakeStreamQueue);
        clock.tick(600001);
        expect(staleQuizEventsLogger.needsRefresh()).to.be.true();
      });
    });
  });
  describe('getLogger', function () {
    var clock = null;
    beforeEach(function () {
      clock = sinon.useFakeTimers();
    });
    afterEach(function () {
      clock.restore();
    });
    var kinesisClientGetterStub = sinon.stub();
    kinesisClientGetterStub.withArgs('1').onFirstCall().returns((0, _kinesisTests.fakeKinesisConfigCreator)()).onSecondCall().returns((0, _kinesisTests.fakeKinesisConfigCreator)());
    it('caches loggers until they expire',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var logger, logger2, logger3;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return (0, _QuizEventsLogger.default)('1', kinesisClientGetterStub);

            case 2:
              logger = _context.sent;
              _context.next = 5;
              return (0, _QuizEventsLogger.default)('1', kinesisClientGetterStub);

            case 5:
              logger2 = _context.sent;
              expect(logger).to.be.an.instanceof(_QuizEventsLogger.QuizEventsLogger);
              expect(logger2).to.equal(logger);
              clock.tick(600001);
              _context.next = 11;
              return (0, _QuizEventsLogger.default)('1', kinesisClientGetterStub);

            case 11:
              logger3 = _context.sent;
              expect(logger3).to.be.an.instanceOf(_QuizEventsLogger.QuizEventsLogger);
              expect(logger3).to.not.equal(logger2);

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
});