Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzianis Dashkevich committed Jun 6, 2024
1 parent b9b8ba7 commit b34ee02
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
"start": "npm run build -- -w",
"build": "babel-config-cjs src -d lib",
"test:index": "run-browser test/index.js -b -m test/mock-server.js | tap-spec",
"test:interceptors": "run-browser test/interceptors.js -b -m test/mock-server.js | tap-spec",
"test:retry": "run-browser test/retry.js -b -m test/mock-server.js | tap-spec",
"test:http-handler": "run-browser test/http-handler.js -b -m test/mock-server.js | tap-spec",
"pretest": "npm run build",
"test": "npm run test:index && npm run test:http-handler",
"test": "npm run test:index && npm run test:http-handler && npm run test:interceptors && npm run test:retry",
"browser": "run-browser -m test/mock-server.js test/index.js"
}
}
12 changes: 10 additions & 2 deletions src/retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ class Retry {
return this.currentDelay_;
}

getCurrentMinPossibleDelay() {
return (1 - this.fuzzFactor_) * this.currentDelay_;
}

getCurrentMaxPossibleDelay() {
return (1 + this.fuzzFactor_) * this.currentDelay_;
}

/**
* For example fuzzFactor is 0.1
* This means ±10% deviation
Expand All @@ -101,8 +109,8 @@ class Retry {
* @private
*/
getCurrentFuzzedDelay() {
const lowValue = (1 - this.fuzzFactor_) * this.currentDelay_;
const highValue = (1 + this.fuzzFactor_) * this.currentDelay_;
const lowValue = this.getCurrentMinPossibleDelay();
const highValue = this.getCurrentMaxPossibleDelay();

return lowValue + Math.random() * (highValue - lowValue);
}
Expand Down
18 changes: 17 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ test("constructs and calls callback without throwing", { timeout: 500 }, functio
})
})

test("[func] Can GET a url (cross-domain)", { timeout: 2000 }, function(assert) {
// skipping this test because test url is no longer valid
test.skip("[func] Can GET a url (cross-domain)", { timeout: 2000 }, function(assert) {
xhr({
uri: "http://www.mocky.io/v2/55a02cb72651260b1a94f024",
useXDR: true
Expand Down Expand Up @@ -381,3 +382,18 @@ test('httpHandler is available on XHR', function(assert) {
assert.ok(xhr.httpHandler)
assert.end();
});

test('requestInterceptorStorage is available on XHR', function(assert) {
assert.ok(xhr.requestInterceptorsStorage);
assert.end();
});

test('responseInterceptorStorage is available on XHR', function(assert) {
assert.ok(xhr.responseInterceptorsStorage);
assert.end();
});

test('retryManager is available on XHR', function(assert) {
assert.ok(xhr.retryManager);
assert.end();
});
163 changes: 163 additions & 0 deletions test/interceptors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
var window = require("global/window")
var test = require("tape")

var InterceptorsStorage = require('../lib/interceptors');

test("should ignore duplicate interceptors", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

function interceptor(request) {
return request;
}

assert.equal(interceptorsStorage.addInterceptor('license', interceptor), true);
assert.equal(interceptorsStorage.addInterceptor('license', interceptor), false);
assert.end();
});

test("should execute registered interceptors", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

var count = 0;

function interceptor(request) {
assert.equal(request.data, 'license-data');
count++;
return request;
}

interceptorsStorage.addInterceptor('license', interceptor);
interceptorsStorage.execute('license', { data: 'license-data' });
assert.equal(count, 1);
assert.end();
});


test("should not execute removed interceptors", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

var count = 0;

function interceptor(request) {
assert.equal(request.data, 'license-data');
count++;
return request;
}

interceptorsStorage.addInterceptor('license', interceptor);
interceptorsStorage.removeInterceptor('license', interceptor);
interceptorsStorage.execute('license', { data: 'license-data' });
assert.equal(count, 0);
assert.end();
});

test("should return interceptors by type", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

function interceptorOne(request) {
return request;
}

function interceptorTwo(request) {
return request;
}

interceptorsStorage.addInterceptor('license', interceptorOne);
interceptorsStorage.addInterceptor('segment', interceptorTwo);

var setLicense = interceptorsStorage.getForType('license');
var setSegment = interceptorsStorage.getForType('segment');

assert.equal(setLicense.size, 1);
assert.equal(setSegment.size, 1);
assert.end();
});

test("should clear interceptors by type", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

function interceptorOne(request) {
return request;
}

function interceptorTwo(request) {
return request;
}

interceptorsStorage.addInterceptor('license', interceptorOne);
interceptorsStorage.addInterceptor('segment', interceptorTwo);

interceptorsStorage.clearInterceptorsByType('license');

var setLicense = interceptorsStorage.getForType('license');
var setSegment = interceptorsStorage.getForType('segment');

assert.equal(setLicense.size, 0);
assert.equal(setSegment.size, 1);
assert.end();
});


test("should clear all interceptors", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

function interceptorOne(request) {
return request;
}

function interceptorTwo(request) {
return request;
}

interceptorsStorage.addInterceptor('license', interceptorOne);
interceptorsStorage.addInterceptor('segment', interceptorTwo);

interceptorsStorage.clear();

var setLicense = interceptorsStorage.getForType('license');
var setSegment = interceptorsStorage.getForType('segment');

assert.equal(setLicense.size, 0);
assert.equal(setSegment.size, 0);
assert.end();
});


test("should set enabled", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

assert.equal(interceptorsStorage.getIsEnabled(), false);
interceptorsStorage.enable();
assert.equal(interceptorsStorage.getIsEnabled(), true);
interceptorsStorage.disable();
assert.equal(interceptorsStorage.getIsEnabled(), false);
assert.end();
});


test("should reset", function(assert) {
var interceptorsStorage = new InterceptorsStorage();

function interceptorOne(request) {
return request;
}

function interceptorTwo(request) {
return request;
}

interceptorsStorage.addInterceptor('license', interceptorOne);
interceptorsStorage.addInterceptor('segment', interceptorTwo);
interceptorsStorage.enable();

assert.equal( interceptorsStorage.getForType('license').size, 1);
assert.equal( interceptorsStorage.getForType('segment').size, 1);
assert.equal(interceptorsStorage.getIsEnabled(), true);

interceptorsStorage.reset();

assert.equal( interceptorsStorage.getForType('license').size, 0);
assert.equal( interceptorsStorage.getForType('segment').size, 0);
assert.equal(interceptorsStorage.getIsEnabled(), false);
assert.end();
});
Loading

0 comments on commit b34ee02

Please sign in to comment.