report-v3.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var __read = (this && this.__read) || function (o, n) {
  2. var m = typeof Symbol === "function" && o[Symbol.iterator];
  3. if (!m) return o;
  4. var i = m.call(o), r, ar = [], e;
  5. try {
  6. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  7. }
  8. catch (error) { e = { error: error }; }
  9. finally {
  10. try {
  11. if (r && !r.done && (m = i["return"])) m.call(i);
  12. }
  13. finally { if (e) throw e.error; }
  14. }
  15. return ar;
  16. };
  17. var __spread = (this && this.__spread) || function () {
  18. for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
  19. return ar;
  20. };
  21. import { reportV3 } from './report-v3';
  22. var MockXHR = /** @class */ (function () {
  23. function MockXHR() {
  24. }
  25. MockXHR.prototype.onreadystatechange = function () { };
  26. MockXHR.prototype.clear = function () {
  27. this.sendData = '';
  28. this.openData = [];
  29. this.headerData = [];
  30. this.status = 0;
  31. this.readyState = 0;
  32. };
  33. MockXHR.prototype.open = function () {
  34. var args = [];
  35. for (var _i = 0; _i < arguments.length; _i++) {
  36. args[_i] = arguments[_i];
  37. }
  38. this.clear();
  39. this.openCount += 1;
  40. this.openData = args;
  41. };
  42. MockXHR.prototype.send = function (args) {
  43. this.sendData = args;
  44. };
  45. MockXHR.prototype.setRequestHeader = function () {
  46. var _a;
  47. var args = [];
  48. for (var _i = 0; _i < arguments.length; _i++) {
  49. args[_i] = arguments[_i];
  50. }
  51. (_a = this.headerData).push.apply(_a, __spread(args));
  52. };
  53. MockXHR.prototype.changeStatusAndState = function (readyState, status) {
  54. this.status = status;
  55. this.readyState = readyState;
  56. this.onreadystatechange();
  57. };
  58. return MockXHR;
  59. }());
  60. var mockXHR = new MockXHR();
  61. jest.mock('../utils', function () { return ({
  62. createXHR: function () { return mockXHR; },
  63. getAuthHeaders: function (t) { return t; }
  64. }); });
  65. describe('test report-v3', function () {
  66. var testData = {
  67. code: 200,
  68. reqId: 'reqId',
  69. host: 'host',
  70. remoteIp: 'remoteIp',
  71. port: 'port',
  72. duration: 1,
  73. time: 1,
  74. bytesSent: 1,
  75. upType: 'jssdk-h5',
  76. size: 1
  77. };
  78. test('test stringify send Data', function () {
  79. reportV3('token', testData, 3);
  80. mockXHR.changeStatusAndState(0, 0);
  81. expect(mockXHR.sendData).toBe([
  82. testData.code || '',
  83. testData.reqId || '',
  84. testData.host || '',
  85. testData.remoteIp || '',
  86. testData.port || '',
  87. testData.duration || '',
  88. testData.time || '',
  89. testData.bytesSent || '',
  90. testData.upType || '',
  91. testData.size || ''
  92. ].join(','));
  93. });
  94. test('test retry', function () {
  95. mockXHR.openCount = 0;
  96. reportV3('token', testData);
  97. for (var index = 1; index <= 10; index++) {
  98. mockXHR.changeStatusAndState(4, 0);
  99. }
  100. expect(mockXHR.openCount).toBe(4);
  101. mockXHR.openCount = 0;
  102. reportV3('token', testData, 4);
  103. for (var index = 1; index < 10; index++) {
  104. mockXHR.changeStatusAndState(4, 0);
  105. }
  106. expect(mockXHR.openCount).toBe(5);
  107. mockXHR.openCount = 0;
  108. reportV3('token', testData, 0);
  109. for (var index = 1; index < 10; index++) {
  110. mockXHR.changeStatusAndState(4, 0);
  111. }
  112. expect(mockXHR.openCount).toBe(1);
  113. });
  114. });
  115. //# sourceMappingURL=report-v3.test.js.map