"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/asyncToGenerator"));
var _webWorker = _interopRequireDefault(require("./webWorker"));
var _types = require("../../common/types");
global.self = global;
describe('webWorker', function () {
  var postMessageSpy;
  var capturedIntervalCallback;
  var capturedIntervalTimer;
  var clearIntervalSpy;
  beforeEach(function () {
    jest.useFakeTimers();
    postMessageSpy = jest.spyOn(global, 'postMessage').mockImplementation(function () {});
    clearIntervalSpy = jest.spyOn(global, 'clearInterval');

    // Overriding setInterval so that we capture the callback rather than schedule a timer
    jest.spyOn(global, 'setInterval').mockImplementation(
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    function (callback, interval) {
      capturedIntervalCallback = callback;
      // Create a dummy timer object (could be any non-null value)
      capturedIntervalTimer = {
        dummy: true
      };
      return capturedIntervalTimer;
    });
  });
  afterEach(function () {
    jest.clearAllMocks();
    jest.clearAllTimers();
    jest.useRealTimers();
  });
  it('should post SEND_KEEPALIVE on a keepalive tick', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee() {
    return _regenerator.default.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.START_KEEPALIVE,
              interval: 1,
              retryCountThreshold: 3
            }
          });
          _context.next = 1;
          return capturedIntervalCallback();
        case 1:
          expect(postMessageSpy).toHaveBeenCalledWith({
            type: _types.WorkerMessageType.SEND_KEEPALIVE
          });
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  })));
  it('should post KEEPALIVE_FAILURE when keepalive result is a failure', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee2() {
    return _regenerator.default.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.START_KEEPALIVE,
              interval: 1,
              retryCountThreshold: 3
            }
          });
          _context2.next = 1;
          return capturedIntervalCallback();
        case 1:
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.KEEPALIVE_RESULT,
              err: {
                statusCode: 401,
                statusText: 'Unauthorized',
                type: undefined
              }
            }
          });
          expect(postMessageSpy).toHaveBeenCalledWith({
            type: _types.WorkerMessageType.KEEPALIVE_FAILURE,
            err: {
              statusCode: 401,
              statusText: 'Unauthorized',
              type: undefined
            },
            keepAliveRetryCount: 1
          });
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  })));
  it('should post KEEPALIVE_SUCCESS after a failure when keepalive result succeeds', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee3() {
    return _regenerator.default.wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.START_KEEPALIVE,
              interval: 1,
              retryCountThreshold: 3
            }
          });
          _context3.next = 1;
          return capturedIntervalCallback();
        case 1:
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.KEEPALIVE_RESULT,
              err: {
                statusCode: 404,
                statusText: 'Not Found'
              }
            }
          });
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.KEEPALIVE_RESULT,
              statusCode: 200
            }
          });
          expect(postMessageSpy.mock.calls[2][0].type).toBe(_types.WorkerMessageType.KEEPALIVE_SUCCESS);
          expect(postMessageSpy.mock.calls[2][0].statusCode).toBe(200);
        case 2:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  })));
  it('should clear keepalive timer on receiving CLEAR_KEEPALIVE message', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee4() {
    var startEvent;
    return _regenerator.default.wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          startEvent = {
            data: {
              type: _types.WorkerMessageType.START_KEEPALIVE,
              interval: 1,
              retryCountThreshold: 1
            }
          };
          (0, _webWorker.default)(startEvent);
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.CLEAR_KEEPALIVE
            }
          });
          jest.advanceTimersByTime(3000);
          expect(clearIntervalSpy).toHaveBeenCalled();
        case 1:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  })));
  it('improve coverage: should not clear keepalive timer on receiving CLEAR_KEEPALIVE message without keepTimer', /*#__PURE__*/(0, _asyncToGenerator2.default)(/*#__PURE__*/_regenerator.default.mark(function _callee5() {
    var startEvent;
    return _regenerator.default.wrap(function (_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          jest.spyOn(global, 'setInterval').mockReturnValue(undefined);
          startEvent = {
            data: {
              type: _types.WorkerMessageType.START_KEEPALIVE,
              interval: 1,
              retryCountThreshold: 1
            }
          };
          (0, _webWorker.default)(startEvent);
          (0, _webWorker.default)({
            data: {
              type: _types.WorkerMessageType.CLEAR_KEEPALIVE
            }
          });
          jest.advanceTimersByTime(3000);
          expect(clearIntervalSpy).not.toHaveBeenCalled();
        case 1:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  })));
});
//# sourceMappingURL=webWorker.test.js.map
