2025-04-02 21:57:33 +08:00

117 lines
3.5 KiB
JavaScript

var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
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 = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
import { reportV3 } from './report-v3';
var MockXHR = /** @class */ (function () {
function MockXHR() {
}
MockXHR.prototype.onreadystatechange = function () {
// null
};
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 () { return mockXHR; },
getAuthHeaders: function (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('stringify send Data', function () {
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('retry', function () {
mockXHR.openCount = 0;
reportV3('token', testData);
for (var index = 1; index <= 10; index++) {
mockXHR.changeStatusAndState(4, 0);
}
expect(mockXHR.openCount).toBe(4);
mockXHR.openCount = 0;
reportV3('token', testData, 4);
for (var index = 1; index < 10; index++) {
mockXHR.changeStatusAndState(4, 0);
}
expect(mockXHR.openCount).toBe(5);
mockXHR.openCount = 0;
reportV3('token', testData, 0);
for (var index = 1; index < 10; index++) {
mockXHR.changeStatusAndState(4, 0);
}
expect(mockXHR.openCount).toBe(1);
});
});
//# sourceMappingURL=report-v3.test.js.map