12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169 |
- 'use strict';
- var fetchBuilder = require('../../');
- var sinon = require('sinon');
- var expect = require('expectations');
- describe('fetchBuilder', function () {
- it('throws when fetch is not a function', function () {
- expect(function () {
- fetchBuilder();
- }).toThrow({
- name: 'ArgumentError',
- message: 'fetch must be a function'
- });
- });
- it('throws when default is not an object', function () {
- expect(function () {
- fetchBuilder(function () { }, 'this is a string, not an object');
- }).toThrow({
- name: 'ArgumentError',
- message: 'defaults must be an object'
- });
- });
- it('returns fetchRetry when provided valid constructor arguments', function () {
- expect(typeof fetchBuilder(function () { }, {retries: 1})).toBe('function');
- });
- });
- describe('fetch-retry', function () {
- var fetch;
- var fetchRetry;
- var deferred1;
- var deferred2;
- var deferred3;
- var deferred4;
- var thenCallback;
- var catchCallback;
- var clock;
- var delay;
- beforeEach(function () {
- delay = 1000;
- clock = sinon.useFakeTimers();
- });
- afterEach(function () {
- clock.restore();
- });
- beforeEach(function () {
- deferred1 = defer();
- deferred2 = defer();
- deferred3 = defer();
- deferred4 = defer();
- fetch = sinon.stub();
- fetch.onCall(0).returns(deferred1.promise);
- fetch.onCall(1).returns(deferred2.promise);
- fetch.onCall(2).returns(deferred3.promise);
- fetch.onCall(3).returns(deferred4.promise);
- fetchRetry = fetchBuilder(fetch);
- });
- describe('#input', function () {
- var expectedUrl = 'http://some-url.com';
- beforeEach(function () {
- fetchRetry(expectedUrl);
- });
- it('passes #input to fetch', function () {
- expect(fetch.getCall(0).args[0]).toBe(expectedUrl);
- });
- });
- describe('#init', function () {
- describe('when #init is provided', function () {
- var init;
- beforeEach(function () {
- init = {
- retries: 3,
- whatever: 'something'
- };
- fetchRetry('http://someUrl', init);
- });
- it('passes init to fetch', function () {
- expect(fetch.getCall(0).args[1]).toEqual(init);
- });
- describe('when #init.retryOn is not an array or function', () => {
- it('throws exception', () => {
- expect(function () {
- init.retryOn = 503;
- fetchRetry('http://someUrl', init);
- }).toThrow({
- name: 'ArgumentError',
- message: 'retryOn property expects an array or function'
- });
- });
- });
- });
- describe('when #init is undefined or null', function () {
- [undefined, null].forEach(function (testCase) {
- beforeEach(function () {
- fetchRetry('http://someUrl', testCase);
- });
- it('does not pass through init to fetch', function () {
- expect(fetch.getCall(0).args[1]).toEqual(undefined);
- });
- });
- });
- });
- describe('#init.retries', function () {
- describe('when #init.retries=3 (default)', function () {
- beforeEach(function () {
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- fetchRetry('http://someurl')
- .then(thenCallback)
- .catch(catchCallback);
- });
- describe('when first call is a success', function () {
- beforeEach(function () {
- deferred1.resolve({ status: 200 });
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch once', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- describe('when first call is a failure', function () {
- beforeEach(function () {
- deferred1.reject();
- });
- describe('when second call is a success', function () {
- beforeEach(function () {
- clock.tick(delay);
- deferred2.resolve({ status: 200 });
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch twice', function () {
- expect(fetch.callCount).toBe(2);
- });
- });
- });
- describe('when second call is a failure', function () {
- beforeEach(function () {
- deferred2.reject();
- clock.tick(delay);
- });
- describe('when third call is a success', function () {
- beforeEach(function () {
- deferred3.resolve({ status: 200 });
- clock.tick(delay);
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch three times', function () {
- expect(fetch.callCount).toBe(3);
- });
- });
- });
- describe('when third call is a failure', function () {
- beforeEach(function () {
- deferred3.reject();
- clock.tick(delay);
- });
- describe('when fourth call is a success', function () {
- beforeEach(function () {
- deferred4.resolve({ status: 200 });
- clock.tick(delay);
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch four times', function () {
- expect(fetch.callCount).toBe(4);
- });
- });
- });
- describe('when fourth call is a failure', function () {
- beforeEach(function () {
- deferred4.reject();
- clock.tick(delay);
- });
- describe('when rejected', function () {
- it('invokes the catch callback', function () {
- expect(catchCallback.called).toBe(true);
- });
- it('does not call fetch again', function () {
- expect(fetch.callCount).toBe(4);
- });
- });
- });
- });
- });
- });
- });
- describe('when #defaults.retries is not a a positive integer', () => {
- ['1', -1, 'not a number', null].forEach(invalidRetries => {
- it('throws error', () => {
- const expectedError = {
- name: 'ArgumentError',
- message: 'retries must be a positive integer'
- };
- expect(() => {
- var fetchRetryWithDefaults = fetchBuilder(fetch, {retries: invalidRetries});
- fetchRetryWithDefaults('http://someurl');
- }).toThrow(expectedError);
- });
- });
- });
- describe('when #defaults.retryDelay is not a a positive integer', () => {
- ['1', -1, 'not a number', null].forEach(invalidDelay => {
- it('throws error', () => {
- const expectedError = {
- name: 'ArgumentError',
- message: 'retryDelay must be a positive integer or a function returning a positive integer'
- };
- expect(() => {
- var fetchRetryWithDefaults = fetchBuilder(fetch, { retryDelay: invalidDelay });
- fetchRetryWithDefaults('http://someurl');
- }).toThrow(expectedError);
- });
- });
- });
- describe('when #defaults.retryDelay is a function', function () {
- var defaults;
- var retryDelay;
- beforeEach(function () {
- retryDelay = sinon.stub().returns(5000);
- defaults = {
- retryDelay: retryDelay
- };
- thenCallback = sinon.spy();
- var fetchRetryWithDefaults = fetchBuilder(fetch, defaults);
- fetchRetryWithDefaults('http://someUrl')
- .then(thenCallback);
- });
- });
- describe('when #defaults.retryOn is not an array or function', function () {
- var defaults = {};
- describe('when #defaults.retryOn is not an array or function', () => {
- it('throws exception', () => {
- expect(function () {
- defaults.retryOn = 503;
- var fetchRetryWithDefaults = fetchBuilder(fetch, defaults);
- fetchRetryWithDefaults('http://someUrl');
- }).toThrow({
- name: 'ArgumentError',
- message: 'retryOn property expects an array or function'
- });
- });
- });
- });
- describe('when #defaults.retries=0', function () {
- beforeEach(function () {
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- var fetchRetryWithDefaults = fetchBuilder(fetch, {retries: 0});
- fetchRetryWithDefaults('http://someurl')
- .then(thenCallback)
- .catch(catchCallback);
- });
- describe('when first call is a failure', function () {
- beforeEach(function () {
- deferred1.reject();
- });
- describe('when rejected', function () {
- it('invokes the catch callback', function () {
- expect(catchCallback.called).toBe(true);
- });
- it('does not call fetch again', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- });
- describe('when #init.retries=1', function () {
- beforeEach(function () {
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- fetchRetry('http://someurl', { retries: 1 })
- .then(thenCallback)
- .catch(catchCallback);
- });
- describe('when first call is a success', function () {
- beforeEach(function () {
- deferred1.resolve({ status: 200 });
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch once', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- describe('when first call is a failure', function () {
- beforeEach(function () {
- deferred1.reject();
- clock.tick(delay);
- });
- describe('when second call is a success', function () {
- beforeEach(function () {
- deferred2.resolve({ status: 200 });
- clock.tick(delay);
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch twice', function () {
- expect(fetch.callCount).toBe(2);
- });
- });
- });
- describe('when second call is a failure', function () {
- beforeEach(function () {
- deferred2.reject();
- clock.tick(delay);
- });
- describe('when rejected', function () {
- it('invokes the catch callback', function () {
- expect(catchCallback.called).toBe(true);
- });
- it('does not call fetch again', function () {
- expect(fetch.callCount).toBe(2);
- });
- });
- });
- });
- });
- describe('when #init.retries=0', function () {
- beforeEach(function () {
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- fetchRetry('http://someurl', { retries: 0 })
- .then(thenCallback)
- .catch(catchCallback);
- });
- describe('when first call is a success', function () {
- beforeEach(function () {
- deferred1.resolve({ status: 200 });
- });
- describe('when resolved', function () {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch once', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- describe('when first call is a failure', function () {
- beforeEach(function () {
- deferred1.reject();
- });
- describe('when rejected', () => {
- it('invokes the catch callback', function () {
- expect(catchCallback.called).toBe(true);
- });
- });
- });
- });
- describe('when #init.retries is not a a positive integer', () => {
- ['1', -1, 'not a number', null].forEach(invalidRetries => {
- it('throws error', () => {
- const expectedError = {
- name: 'ArgumentError',
- message: 'retries must be a positive integer'
- };
- expect(() => {
- fetchRetry('http://someurl', { retries: invalidRetries });
- }).toThrow(expectedError);
- });
- });
- });
- });
- describe('#init.retryDelay', function () {
- describe('when #init.retryDelay is a number', function () {
- var init;
- var retryDelay;
- beforeEach(function () {
- retryDelay = 5000;
- init = {
- retryDelay: retryDelay
- };
- thenCallback = sinon.spy();
- fetchRetry('http://someUrl', init)
- .then(thenCallback);
- });
- describe('when first call is unsuccessful', function () {
- beforeEach(function () {
- deferred1.reject();
- });
- describe('after specified time', function () {
- beforeEach(function () {
- clock.tick(retryDelay);
- });
- 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);
- });
- });
- });
- });
- describe('when #init.retryDelay is 0', function () {
- var init;
- var retryDelay;
- beforeEach(function () {
- retryDelay = 0;
- init = {
- retryDelay: retryDelay
- };
- thenCallback = sinon.spy();
- fetchRetry('http://someUrl', init)
- .then(thenCallback);
- });
- describe('when first call is unsuccessful', function () {
- beforeEach(function () {
- deferred1.reject();
- });
- describe('after one event loop tick', function () {
- beforeEach(function () {
- clock.tick(0);
- });
- it('invokes fetch again', function () {
- expect(fetch.callCount).toBe(2);
- });
- });
- });
- });
- describe('when #init.retryDelay is not a a positive integer', () => {
- ['1', -1, 'not a number', null].forEach(invalidDelay => {
- it('throws error', () => {
- const expectedError = {
- name: 'ArgumentError',
- message: 'retryDelay must be a positive integer or a function returning a positive integer'
- };
- expect(() => {
- fetchRetry('http://someurl', { retryDelay: invalidDelay });
- }).toThrow(expectedError);
- });
- });
- });
- describe('when #init.retryDelay is a function', function () {
- var init;
- var retryDelay;
- beforeEach(function () {
- retryDelay = sinon.stub().returns(5000);
- init = {
- retryDelay: retryDelay
- };
- thenCallback = sinon.spy();
- fetchRetry('http://someUrl', init)
- .then(thenCallback);
- });
- describe('when first call is unsuccessful', function () {
- beforeEach(function () {
- deferred1.reject(new Error('first error'));
- });
- describe('when the second call is a success', function () {
- beforeEach(function () {
- deferred2.resolve({ status: 200 });
- clock.tick(5000);
- });
- it('invokes the retryDelay function', function () {
- expect(retryDelay.called).toBe(true);
- expect(retryDelay.lastCall.args[0]).toEqual(0);
- expect(retryDelay.lastCall.args[1].message).toEqual('first error');
- });
- });
- describe('when second call is a failure', function () {
- beforeEach(function () {
- deferred2.reject(new Error('second error'));
- clock.tick(5000);
- });
- describe('when the third call is a success', function () {
- beforeEach(function () {
- deferred3.resolve({ status: 200 });
- clock.tick(5000);
- });
- it('invokes the retryDelay function again', function () {
- expect(retryDelay.callCount).toBe(2);
- expect(retryDelay.lastCall.args[0]).toEqual(1);
- expect(retryDelay.lastCall.args[1].message).toEqual('second error');
- });
- });
- });
- });
- });
- });
- describe('#init.retryOn', () => {
- describe('when #init.retryOn is an array', () => {
- var init;
- var retryOn;
- beforeEach(function () {
- retryOn = [503, 404];
- init = {
- retryOn: retryOn
- };
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- fetchRetry('http://someUrl', init)
- .then(thenCallback)
- .catch((catchCallback));
- });
- describe('when first fetch is resolved with status code specified in retryOn array', () => {
- beforeEach(() => {
- deferred1.resolve({ status: 503 });
- });
- describe('after specified delay', () => {
- beforeEach(() => {
- clock.tick(delay);
- });
- it('retries fetch', () => {
- expect(fetch.callCount).toBe(2);
- });
- describe('when second fetch resolves with a different status code', () => {
- beforeEach(() => {
- deferred2.resolve({ status: 200 });
- });
- describe('when resolved', () => {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('has called fetch twice', function () {
- expect(fetch.callCount).toBe(2);
- });
- });
- });
- });
- });
- });
- describe('when #init.retryOn is a function', function () {
- var init;
- var retryOn;
- var fetchRetryChain;
- beforeEach(function () {
- retryOn = sinon.stub();
- init = {
- retryOn: retryOn
- };
- thenCallback = sinon.spy();
- catchCallback = sinon.spy();
- fetchRetryChain = fetchRetry('http://someUrl', init)
- .then(thenCallback)
- .catch((catchCallback));
- });
- describe('when first attempt is rejected due to network error', function () {
- describe('when #retryOn() returns true', () => {
- beforeEach(function () {
- retryOn.returns(true);
- deferred1.reject(new Error('first error'));
- });
- describe('when rejected', function () {
- it('invokes #retryOn function with an error', function () {
- expect(retryOn.called).toBe(true);
- expect(retryOn.lastCall.args.length).toBe(3);
- expect(retryOn.lastCall.args[0]).toBe(0);
- expect(retryOn.lastCall.args[1] instanceof Error).toBe(true);
- expect(retryOn.lastCall.args[2]).toBe(null);
- });
- describe('after specified time', function () {
- beforeEach(function () {
- clock.tick(delay);
- });
- it('invokes fetch again', function () {
- expect(fetch.callCount).toBe(2);
- });
- describe('when the second call is unsuccessful', function () {
- beforeEach(function () {
- deferred2.reject(new Error('second error'));
- clock.tick(delay);
- });
- describe('when rejected', function () {
- it('invokes the #retryOn function twice', function () {
- expect(retryOn.callCount).toBe(2);
- expect(retryOn.lastCall.args[0]).toBe(1);
- });
- });
- });
- });
- });
- });
- describe('when #retryOn() returns false', () => {
- beforeEach(function () {
- retryOn.returns(false);
- deferred1.reject(new Error('first error'));
- });
- describe('when rejected', function () {
- it('invokes #retryOn function with an error', function () {
- expect(retryOn.called).toBe(true);
- expect(retryOn.lastCall.args.length).toBe(3);
- expect(retryOn.lastCall.args[0]).toBe(0);
- expect(retryOn.lastCall.args[1] instanceof Error).toBe(true);
- expect(retryOn.lastCall.args[2]).toBe(null);
- });
- describe('after specified time', function () {
- beforeEach(function () {
- clock.tick(delay);
- });
- it('invokes the catch callback', function () {
- expect(catchCallback.called).toBe(true);
- });
- it('does not call fetch again', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- });
- });
- describe('when first attempt is resolved', function () {
- describe('when #retryOn() returns true', () => {
- beforeEach(function () {
- retryOn.returns(true);
- deferred1.resolve({ status: 200 });
- });
- describe('after specified delay', () => {
- beforeEach(function () {
- clock.tick(delay);
- });
- it('calls fetch again', function () {
- expect(fetch.callCount).toBe(2);
- });
- describe('when second call is resolved', () => {
- beforeEach(function () {
- deferred2.resolve({ status: 200 });
- clock.tick(delay);
- });
- it('invokes the #retryOn function with the response', function () {
- expect(retryOn.called).toBe(true);
- expect(retryOn.lastCall.args.length).toBe(3);
- expect(retryOn.lastCall.args[0]).toBe(0);
- expect(retryOn.lastCall.args[1]).toBe(null);
- expect(retryOn.lastCall.args[2]).toEqual({ status: 200 });
- });
- });
- });
- });
- describe('when #retryOn() returns false', () => {
- beforeEach(function () {
- retryOn.returns(false);
- deferred1.resolve({ status: 502 });
- });
- describe('when resolved', () => {
- it('invokes the then callback', function () {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch 1 time only', function () {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- });
- describe('when first attempt is resolved with Promise', function() {
- describe('when #retryOn() returns Promise with true resolve', () => {
- beforeEach(function() {
- retryOn.resolves(true);
- deferred1.resolve({ status: 200 });
- });
- describe('after specified delay', () => {
- beforeEach(function() {
- clock.tick(delay);
- });
- it('calls fetch again', function() {
- expect(fetch.callCount).toBe(2);
- });
- describe('when second call is resolved', () => {
- beforeEach(function() {
- deferred2.resolve({ status: 200 });
- clock.tick(delay);
- });
- it('invokes the #retryOn function with the response', function() {
- expect(retryOn.called).toBe(true);
- expect(retryOn.lastCall.args.length).toBe(3);
- expect(retryOn.lastCall.args[0]).toBe(0);
- expect(retryOn.lastCall.args[1]).toBe(null);
- expect(retryOn.lastCall.args[2]).toEqual({ status: 200 });
- });
- });
- });
- });
- describe('when #retryOn() returns Promise with false resolve', () => {
- beforeEach(function() {
- retryOn.resolves(false);
- deferred1.resolve({ status: 502 });
- });
- describe('when resolved', () => {
- it('invokes the then callback', function() {
- expect(thenCallback.called).toBe(true);
- });
- it('calls fetch 1 time only', function() {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- describe('when #retryOn() throws an error', () => {
- beforeEach(function() {
- retryOn.throws();
- });
- describe('when rejected', () => {
- beforeEach(function() {
- deferred1.reject();
- });
- it('retryOn called only once', () => {
- return fetchRetryChain.finally(() => {
- expect(retryOn.callCount).toBe(1);
- });
- });
- it('invokes the catch callback', function() {
- return fetchRetryChain.finally(() => {
- expect(catchCallback.called).toBe(true);
- });
- });
- it('called fetch', function() {
- expect(fetch.callCount).toBe(1);
- });
- });
- describe('when resolved', () => {
- beforeEach(function() {
- deferred1.resolve({ status: 200 });
- });
- it('retryOn called only once', () => {
- return fetchRetryChain.finally(() => {
- expect(retryOn.callCount).toBe(1);
- });
- });
- it('invokes the catch callback', function() {
- return fetchRetryChain.finally(() => {
- expect(catchCallback.called).toBe(true);
- });
- });
- it('called fetch', function() {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- describe('when #retryOn() returns a Promise that rejects', () => {
- beforeEach(function() {
- retryOn.rejects();
- });
- describe('when rejected', () => {
- beforeEach(function() {
- deferred1.reject();
- });
- it('retryOn called only once', () => {
- return fetchRetryChain.finally(() => {
- expect(retryOn.callCount).toBe(1);
- });
- });
- it('invokes the catch callback', function() {
- return fetchRetryChain.finally(() => {
- expect(catchCallback.called).toBe(true);
- });
- });
- it('called fetch', function() {
- expect(fetch.callCount).toBe(1);
- });
- });
- describe('when resolved', () => {
- beforeEach(function() {
- deferred1.resolve({ status: 200 });
- });
- it('retryOn called only once', () => {
- return fetchRetryChain.finally(() => {
- expect(retryOn.callCount).toBe(1);
- });
- });
- it('invokes the catch callback', function() {
- return fetchRetryChain.finally(() => {
- expect(catchCallback.called).toBe(true);
- });
- });
- it('called fetch', function() {
- expect(fetch.callCount).toBe(1);
- });
- });
- });
- });
- });
- describe('when #init.retryOn is not an array or function', function () {
- var init;
- describe('when #init.retryOn is not an array or function', () => {
- it('throws exception', () => {
- expect(function () {
- init.retryOn = 503;
- fetchRetry('http://someUrl', init);
- }).toThrow({
- name: 'ArgumentError',
- message: 'retryOn property expects an array or function'
- });
- });
- });
- });
- });
- });
- function defer() {
- var resolve, reject;
- // eslint-disable-next-line no-undef
- var promise = new Promise(function () {
- resolve = arguments[0];
- reject = arguments[1];
- });
- return {
- resolve: resolve,
- reject: reject,
- promise: promise
- };
- }
|