fetch-retry
Advanced tools
Comparing version 0.0.5 to 0.1.1
@@ -8,2 +8,3 @@ 'use strict'; | ||
var retries = 3; | ||
var timeout = 1000; | ||
@@ -14,2 +15,6 @@ if (options && options.retries) { | ||
if (options && options.timeout) { | ||
timeout = options.timeout; | ||
} | ||
return new Promise(function(resolve, reject) { | ||
@@ -25,3 +30,3 @@ var wrappedFetch = function(n) { | ||
wrappedFetch(--n); | ||
}, 1000); | ||
}, timeout); | ||
} else { | ||
@@ -28,0 +33,0 @@ reject(error); |
{ | ||
"name": "fetch-retry", | ||
"version": "0.0.5", | ||
"version": "0.1.1", | ||
"description": "Fetch API with retry functionality", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/jonbern/fetch-retry.git", |
@@ -19,3 +19,6 @@ # fetch-retry | ||
```javascript | ||
fetch(url, { retries: 5 }) | ||
fetch(url, { | ||
retries: 5, | ||
timeout: 1500 | ||
}) | ||
.then(function(response) { | ||
@@ -22,0 +25,0 @@ return response.json(); |
110
tests.js
@@ -20,15 +20,12 @@ 'use strict'; | ||
var _setTimeout; | ||
var clock; | ||
var delay; | ||
beforeEach(function() { | ||
_setTimeout = setTimeout; | ||
setTimeout = function(callback) { | ||
callback(); | ||
}; | ||
setTimeout = sinon.spy(setTimeout); | ||
delay = 1000; | ||
clock = sinon.useFakeTimers(); | ||
}); | ||
afterEach(function() { | ||
setTimeout = _setTimeout; | ||
clock.restore(); | ||
}); | ||
@@ -55,3 +52,2 @@ | ||
describe('#url', function() { | ||
@@ -73,13 +69,35 @@ | ||
var expectedOptions = { | ||
retries: 3, | ||
whatever: 'something' | ||
}; | ||
describe('when #options are valid', function() { | ||
beforeEach(function() { | ||
fetchRetry('http://someUrl', expectedOptions); | ||
var options; | ||
beforeEach(function() { | ||
options = { | ||
retries: 3, | ||
whatever: 'something' | ||
}; | ||
fetchRetry('http://someUrl', options); | ||
}); | ||
it('passes options to fetch', function() { | ||
expect(fetch.getCall(0).args[1]).toEqual(options); | ||
}); | ||
}); | ||
it('passes #url to fetch', function() { | ||
expect(fetch.getCall(0).args[1]).toBe(expectedOptions); | ||
describe('when #options are undefined or null', function() { | ||
[undefined, null].forEach(function(testCase) { | ||
beforeEach(function() { | ||
fetchRetry('http://someUrl', testCase); | ||
}); | ||
it('does not pass through options to fetch', function() { | ||
expect(fetch.getCall(0).args[1]).toEqual(undefined); | ||
}); | ||
}); | ||
}); | ||
@@ -129,2 +147,3 @@ | ||
beforeEach(function() { | ||
clock.tick(delay); | ||
deferred2.resolve(); | ||
@@ -151,2 +170,3 @@ }); | ||
deferred2.reject(); | ||
clock.tick(delay); | ||
}); | ||
@@ -158,2 +178,3 @@ | ||
deferred3.resolve(); | ||
clock.tick(delay); | ||
}); | ||
@@ -179,2 +200,3 @@ | ||
deferred3.reject(); | ||
clock.tick(delay); | ||
}); | ||
@@ -186,2 +208,3 @@ | ||
deferred4.resolve(); | ||
clock.tick(delay); | ||
}); | ||
@@ -207,2 +230,3 @@ | ||
deferred4.reject(); | ||
clock.tick(delay); | ||
}); | ||
@@ -267,2 +291,3 @@ | ||
deferred1.reject(); | ||
clock.tick(delay); | ||
}); | ||
@@ -274,2 +299,3 @@ | ||
deferred2.resolve(); | ||
clock.tick(delay); | ||
}); | ||
@@ -295,2 +321,3 @@ | ||
deferred2.reject(); | ||
clock.tick(delay); | ||
}); | ||
@@ -316,2 +343,51 @@ | ||
describe('when #options.timeout is provided', function() { | ||
var options; | ||
beforeEach(function() { | ||
options = { | ||
timeout: 5000 | ||
}; | ||
thenCallback = sinon.spy(); | ||
fetchRetry('http://someUrl', options) | ||
.then(thenCallback) | ||
}); | ||
describe('when first call is unsuccessful', function() { | ||
beforeEach(function() { | ||
deferred1.reject(); | ||
}); | ||
describe('after specified time', function() { | ||
beforeEach(function() { | ||
clock.tick(options.timeout); | ||
}); | ||
it('invokes fetch again', function() { | ||
expect(fetch.callCount).toBe(2); | ||
}); | ||
}); | ||
describe('after less than specified time', function() { | ||
beforeEach(function() { | ||
clock.tick(1000); | ||
}); | ||
it('does not invoke fetch again', function() { | ||
expect(fetch.callCount).toBe(1); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
@@ -318,0 +394,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12021
299
41
19