"use strict";

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

var _iterator = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol/iterator"));

var _symbol = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/symbol"));

var _reportV = require("./report-v3");

var __read = void 0 && (void 0).__read || function (o, n) {
  var m = typeof _symbol["default"] === "function" && o[_iterator["default"]];
  if (!m) return o;
  var i = m.call(o),
      r,
      ar = [],
      e;

  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done) {
      ar.push(r.value);
    }
  } catch (error) {
    e = {
      error: error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"])) m.call(i);
    } finally {
      if (e) throw e.error;
    }
  }

  return ar;
};

var __spread = void 0 && (void 0).__spread || function () {
  for (var ar = [], i = 0; i < arguments.length; i++) {
    ar = ar.concat(__read(arguments[i]));
  }

  return ar;
};

var MockXHR =
/** @class */
function () {
  function MockXHR() {}

  MockXHR.prototype.onreadystatechange = function () {};

  MockXHR.prototype.clear = function () {
    this.sendData = '';
    this.openData = [];
    this.headerData = [];
    this.status = 0;
    this.readyState = 0;
  };

  MockXHR.prototype.open = function () {
    var args = [];

    for (var _i = 0; _i < arguments.length; _i++) {
      args[_i] = arguments[_i];
    }

    this.clear();
    this.openCount += 1;
    this.openData = args;
  };

  MockXHR.prototype.send = function (args) {
    this.sendData = args;
  };

  MockXHR.prototype.setRequestHeader = function () {
    var _a;

    var args = [];

    for (var _i = 0; _i < arguments.length; _i++) {
      args[_i] = arguments[_i];
    }

    (_a = this.headerData).push.apply(_a, __spread(args));
  };

  MockXHR.prototype.changeStatusAndState = function (readyState, status) {
    this.status = status;
    this.readyState = readyState;
    this.onreadystatechange();
  };

  return MockXHR;
}();

var mockXHR = new MockXHR();
jest.mock('../utils', function () {
  return {
    createXHR: function createXHR() {
      return mockXHR;
    },
    getAuthHeaders: function getAuthHeaders(t) {
      return t;
    }
  };
});
describe('test report-v3', function () {
  var testData = {
    code: 200,
    reqId: 'reqId',
    host: 'host',
    remoteIp: 'remoteIp',
    port: 'port',
    duration: 1,
    time: 1,
    bytesSent: 1,
    upType: 'jssdk-h5',
    size: 1
  };
  test('test stringify send Data', function () {
    (0, _reportV.reportV3)('token', testData, 3);
    mockXHR.changeStatusAndState(0, 0);
    expect(mockXHR.sendData).toBe([testData.code || '', testData.reqId || '', testData.host || '', testData.remoteIp || '', testData.port || '', testData.duration || '', testData.time || '', testData.bytesSent || '', testData.upType || '', testData.size || ''].join(','));
  });
  test('test retry', function () {
    mockXHR.openCount = 0;
    (0, _reportV.reportV3)('token', testData);

    for (var index = 1; index <= 10; index++) {
      mockXHR.changeStatusAndState(4, 0);
    }

    expect(mockXHR.openCount).toBe(4);
    mockXHR.openCount = 0;
    (0, _reportV.reportV3)('token', testData, 4);

    for (var index = 1; index < 10; index++) {
      mockXHR.changeStatusAndState(4, 0);
    }

    expect(mockXHR.openCount).toBe(5);
    mockXHR.openCount = 0;
    (0, _reportV.reportV3)('token', testData, 0);

    for (var index = 1; index < 10; index++) {
      mockXHR.changeStatusAndState(4, 0);
    }

    expect(mockXHR.openCount).toBe(1);
  });
});