"use strict";

require("core-js/modules/es.object.define-property.js");

require("core-js/modules/es.object.keys.js");

require("core-js/modules/es.symbol.js");

require("core-js/modules/es.array.filter.js");

require("core-js/modules/es.object.get-own-property-descriptor.js");

require("core-js/modules/es.array.for-each.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.object.get-own-property-descriptors.js");

require("core-js/modules/es.object.define-properties.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.promise.js");

require("regenerator-runtime/runtime.js");

var _request = require("../../request");

var _utils = require("../utils");

var _jestMockAxios = _interopRequireDefault(require("jest-mock-axios"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

describe("Request", function () {
  describe("attemptRequest", function () {
    afterEach(function () {
      _jestMockAxios["default"].reset();

      _jestMockAxios["default"].mockClear();
    });
    test("Should include response", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
      var request, resp, promise, result;
      return regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              request = new _request.Request("GET", "/", 5000);
              resp = {
                data: {
                  dataKey1: "value1"
                },
                status: 200,
                headers: {
                  headerKey1: "value1"
                }
              };
              promise = request.send();

              _jestMockAxios["default"].mockResponse(resp);

              expect(_jestMockAxios["default"]).toHaveBeenCalledTimes(1);
              _context.next = 7;
              return promise;

            case 7:
              result = _context.sent;
              expect(result).toEqual(_objectSpread(_objectSpread({}, resp), {}, {
                error: undefined
              }));

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    test("Should include error", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
      var request, promise, error, result;
      return regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              request = new _request.Request("GET", "/", 5000);
              promise = request.send();
              error = {
                message: "Something bad happened",
                code: 1
              };

              _jestMockAxios["default"].mockError(_objectSpread({}, error));

              expect(_jestMockAxios["default"]).toHaveBeenCalledTimes(1);
              _context2.next = 7;
              return promise;

            case 7:
              result = _context2.sent;
              expect(result.error).toEqual(_objectSpread({}, error));

            case 9:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
    test("Should include response from error", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
      var timeout, request, resp, error, promise, result;
      return regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              timeout = 5000;
              request = new _request.Request("GET", "/", timeout);
              resp = {
                data: {
                  dataKey1: "value1"
                },
                status: 200,
                headers: {
                  headerKey1: "value1"
                }
              };
              error = {
                message: "Something bad happened",
                code: 1
              };
              promise = request.send();

              _jestMockAxios["default"].mockError(_objectSpread(_objectSpread({}, error), {}, {
                response: resp
              }));

              expect(_jestMockAxios["default"]).toHaveBeenCalledTimes(1);
              _context3.next = 9;
              return promise;

            case 9:
              result = _context3.sent;
              expect(result).toEqual(_objectSpread(_objectSpread({}, resp), {}, {
                error: error
              }));

            case 11:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
    test("Should retry on timeout", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
      var retries, attempts, timeout, request, error, promise, index, result;
      return regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              retries = 3;
              attempts = retries + 1;
              timeout = 5000;
              request = new _request.Request("GET", "/", timeout, {
                retries: retries
              });
              error = {
                message: "timeout of ".concat(timeout, "ms exceeded"),
                code: "ECONNABORTED"
              };
              promise = request.send();
              index = 0;

            case 7:
              if (!(index < attempts)) {
                _context4.next = 14;
                break;
              }

              _jestMockAxios["default"].mockError(_objectSpread({}, error)); // sleep so next server call is attempted before next loop.


              _context4.next = 11;
              return (0, _utils.sleep)(0);

            case 11:
              index++;
              _context4.next = 7;
              break;

            case 14:
              _context4.next = 16;
              return promise;

            case 16:
              result = _context4.sent;
              expect(_jestMockAxios["default"]).toHaveBeenCalledTimes(attempts);
              expect(result).toEqual({
                error: _objectSpread({}, error)
              });

            case 19:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
  });
});
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 