report-v3.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // null
  27. };
  28. MockXHR.prototype.clear = function () {
  29. this.sendData = '';
  30. this.openData = [];
  31. this.headerData = [];
  32. this.status = 0;
  33. this.readyState = 0;
  34. };
  35. MockXHR.prototype.open = function () {
  36. var args = [];
  37. for (var _i = 0; _i < arguments.length; _i++) {
  38. args[_i] = arguments[_i];
  39. }
  40. this.clear();
  41. this.openCount += 1;
  42. this.openData = args;
  43. };
  44. MockXHR.prototype.send = function (args) {
  45. this.sendData = args;
  46. };
  47. MockXHR.prototype.setRequestHeader = function () {
  48. var _a;
  49. var args = [];
  50. for (var _i = 0; _i < arguments.length; _i++) {
  51. args[_i] = arguments[_i];
  52. }
  53. (_a = this.headerData).push.apply(_a, __spread(args));
  54. };
  55. MockXHR.prototype.changeStatusAndState = function (readyState, status) {
  56. this.status = status;
  57. this.readyState = readyState;
  58. this.onreadystatechange();
  59. };
  60. return MockXHR;
  61. }());
  62. var mockXHR = new MockXHR();
  63. jest.mock('../utils', function () { return ({
  64. createXHR: function () { return mockXHR; },
  65. getAuthHeaders: function (t) { return t; }
  66. }); });
  67. describe('test report-v3', function () {
  68. var testData = {
  69. code: 200,
  70. reqId: 'reqId',
  71. host: 'host',
  72. remoteIp: 'remoteIp',
  73. port: 'port',
  74. duration: 1,
  75. time: 1,
  76. bytesSent: 1,
  77. upType: 'jssdk-h5',
  78. size: 1
  79. };
  80. test('stringify send Data', function () {
  81. reportV3('token', testData, 3);
  82. mockXHR.changeStatusAndState(0, 0);
  83. expect(mockXHR.sendData).toBe([
  84. testData.code || '',
  85. testData.reqId || '',
  86. testData.host || '',
  87. testData.remoteIp || '',
  88. testData.port || '',
  89. testData.duration || '',
  90. testData.time || '',
  91. testData.bytesSent || '',
  92. testData.upType || '',
  93. testData.size || ''
  94. ].join(','));
  95. });
  96. test('retry', function () {
  97. mockXHR.openCount = 0;
  98. reportV3('token', testData);
  99. for (var index = 1; index <= 10; index++) {
  100. mockXHR.changeStatusAndState(4, 0);
  101. }
  102. expect(mockXHR.openCount).toBe(4);
  103. mockXHR.openCount = 0;
  104. reportV3('token', testData, 4);
  105. for (var index = 1; index < 10; index++) {
  106. mockXHR.changeStatusAndState(4, 0);
  107. }
  108. expect(mockXHR.openCount).toBe(5);
  109. mockXHR.openCount = 0;
  110. reportV3('token', testData, 0);
  111. for (var index = 1; index < 10; index++) {
  112. mockXHR.changeStatusAndState(4, 0);
  113. }
  114. expect(mockXHR.openCount).toBe(1);
  115. });
  116. });
  117. //# sourceMappingURL=report-v3.test.js.map