index.js 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. 'use strict';
  2. var fetchBuilder = require('../../');
  3. var sinon = require('sinon');
  4. var expect = require('expectations');
  5. describe('fetchBuilder', function () {
  6. it('throws when fetch is not a function', function () {
  7. expect(function () {
  8. fetchBuilder();
  9. }).toThrow({
  10. name: 'ArgumentError',
  11. message: 'fetch must be a function'
  12. });
  13. });
  14. it('throws when default is not an object', function () {
  15. expect(function () {
  16. fetchBuilder(function () { }, 'this is a string, not an object');
  17. }).toThrow({
  18. name: 'ArgumentError',
  19. message: 'defaults must be an object'
  20. });
  21. });
  22. it('returns fetchRetry when provided valid constructor arguments', function () {
  23. expect(typeof fetchBuilder(function () { }, {retries: 1})).toBe('function');
  24. });
  25. });
  26. describe('fetch-retry', function () {
  27. var fetch;
  28. var fetchRetry;
  29. var deferred1;
  30. var deferred2;
  31. var deferred3;
  32. var deferred4;
  33. var thenCallback;
  34. var catchCallback;
  35. var clock;
  36. var delay;
  37. beforeEach(function () {
  38. delay = 1000;
  39. clock = sinon.useFakeTimers();
  40. });
  41. afterEach(function () {
  42. clock.restore();
  43. });
  44. beforeEach(function () {
  45. deferred1 = defer();
  46. deferred2 = defer();
  47. deferred3 = defer();
  48. deferred4 = defer();
  49. fetch = sinon.stub();
  50. fetch.onCall(0).returns(deferred1.promise);
  51. fetch.onCall(1).returns(deferred2.promise);
  52. fetch.onCall(2).returns(deferred3.promise);
  53. fetch.onCall(3).returns(deferred4.promise);
  54. fetchRetry = fetchBuilder(fetch);
  55. });
  56. describe('#input', function () {
  57. var expectedUrl = 'http://some-url.com';
  58. beforeEach(function () {
  59. fetchRetry(expectedUrl);
  60. });
  61. it('passes #input to fetch', function () {
  62. expect(fetch.getCall(0).args[0]).toBe(expectedUrl);
  63. });
  64. });
  65. describe('#init', function () {
  66. describe('when #init is provided', function () {
  67. var init;
  68. beforeEach(function () {
  69. init = {
  70. retries: 3,
  71. whatever: 'something'
  72. };
  73. fetchRetry('http://someUrl', init);
  74. });
  75. it('passes init to fetch', function () {
  76. expect(fetch.getCall(0).args[1]).toEqual(init);
  77. });
  78. describe('when #init.retryOn is not an array or function', () => {
  79. it('throws exception', () => {
  80. expect(function () {
  81. init.retryOn = 503;
  82. fetchRetry('http://someUrl', init);
  83. }).toThrow({
  84. name: 'ArgumentError',
  85. message: 'retryOn property expects an array or function'
  86. });
  87. });
  88. });
  89. });
  90. describe('when #init is undefined or null', function () {
  91. [undefined, null].forEach(function (testCase) {
  92. beforeEach(function () {
  93. fetchRetry('http://someUrl', testCase);
  94. });
  95. it('does not pass through init to fetch', function () {
  96. expect(fetch.getCall(0).args[1]).toEqual(undefined);
  97. });
  98. });
  99. });
  100. });
  101. describe('#init.retries', function () {
  102. describe('when #init.retries=3 (default)', function () {
  103. beforeEach(function () {
  104. thenCallback = sinon.spy();
  105. catchCallback = sinon.spy();
  106. fetchRetry('http://someurl')
  107. .then(thenCallback)
  108. .catch(catchCallback);
  109. });
  110. describe('when first call is a success', function () {
  111. beforeEach(function () {
  112. deferred1.resolve({ status: 200 });
  113. });
  114. describe('when resolved', function () {
  115. it('invokes the then callback', function () {
  116. expect(thenCallback.called).toBe(true);
  117. });
  118. it('calls fetch once', function () {
  119. expect(fetch.callCount).toBe(1);
  120. });
  121. });
  122. });
  123. describe('when first call is a failure', function () {
  124. beforeEach(function () {
  125. deferred1.reject();
  126. });
  127. describe('when second call is a success', function () {
  128. beforeEach(function () {
  129. clock.tick(delay);
  130. deferred2.resolve({ status: 200 });
  131. });
  132. describe('when resolved', function () {
  133. it('invokes the then callback', function () {
  134. expect(thenCallback.called).toBe(true);
  135. });
  136. it('calls fetch twice', function () {
  137. expect(fetch.callCount).toBe(2);
  138. });
  139. });
  140. });
  141. describe('when second call is a failure', function () {
  142. beforeEach(function () {
  143. deferred2.reject();
  144. clock.tick(delay);
  145. });
  146. describe('when third call is a success', function () {
  147. beforeEach(function () {
  148. deferred3.resolve({ status: 200 });
  149. clock.tick(delay);
  150. });
  151. describe('when resolved', function () {
  152. it('invokes the then callback', function () {
  153. expect(thenCallback.called).toBe(true);
  154. });
  155. it('calls fetch three times', function () {
  156. expect(fetch.callCount).toBe(3);
  157. });
  158. });
  159. });
  160. describe('when third call is a failure', function () {
  161. beforeEach(function () {
  162. deferred3.reject();
  163. clock.tick(delay);
  164. });
  165. describe('when fourth call is a success', function () {
  166. beforeEach(function () {
  167. deferred4.resolve({ status: 200 });
  168. clock.tick(delay);
  169. });
  170. describe('when resolved', function () {
  171. it('invokes the then callback', function () {
  172. expect(thenCallback.called).toBe(true);
  173. });
  174. it('calls fetch four times', function () {
  175. expect(fetch.callCount).toBe(4);
  176. });
  177. });
  178. });
  179. describe('when fourth call is a failure', function () {
  180. beforeEach(function () {
  181. deferred4.reject();
  182. clock.tick(delay);
  183. });
  184. describe('when rejected', function () {
  185. it('invokes the catch callback', function () {
  186. expect(catchCallback.called).toBe(true);
  187. });
  188. it('does not call fetch again', function () {
  189. expect(fetch.callCount).toBe(4);
  190. });
  191. });
  192. });
  193. });
  194. });
  195. });
  196. });
  197. describe('when #defaults.retries is not a a positive integer', () => {
  198. ['1', -1, 'not a number', null].forEach(invalidRetries => {
  199. it('throws error', () => {
  200. const expectedError = {
  201. name: 'ArgumentError',
  202. message: 'retries must be a positive integer'
  203. };
  204. expect(() => {
  205. var fetchRetryWithDefaults = fetchBuilder(fetch, {retries: invalidRetries});
  206. fetchRetryWithDefaults('http://someurl');
  207. }).toThrow(expectedError);
  208. });
  209. });
  210. });
  211. describe('when #defaults.retryDelay is not a a positive integer', () => {
  212. ['1', -1, 'not a number', null].forEach(invalidDelay => {
  213. it('throws error', () => {
  214. const expectedError = {
  215. name: 'ArgumentError',
  216. message: 'retryDelay must be a positive integer or a function returning a positive integer'
  217. };
  218. expect(() => {
  219. var fetchRetryWithDefaults = fetchBuilder(fetch, { retryDelay: invalidDelay });
  220. fetchRetryWithDefaults('http://someurl');
  221. }).toThrow(expectedError);
  222. });
  223. });
  224. });
  225. describe('when #defaults.retryDelay is a function', function () {
  226. var defaults;
  227. var retryDelay;
  228. beforeEach(function () {
  229. retryDelay = sinon.stub().returns(5000);
  230. defaults = {
  231. retryDelay: retryDelay
  232. };
  233. thenCallback = sinon.spy();
  234. var fetchRetryWithDefaults = fetchBuilder(fetch, defaults);
  235. fetchRetryWithDefaults('http://someUrl')
  236. .then(thenCallback);
  237. });
  238. });
  239. describe('when #defaults.retryOn is not an array or function', function () {
  240. var defaults = {};
  241. describe('when #defaults.retryOn is not an array or function', () => {
  242. it('throws exception', () => {
  243. expect(function () {
  244. defaults.retryOn = 503;
  245. var fetchRetryWithDefaults = fetchBuilder(fetch, defaults);
  246. fetchRetryWithDefaults('http://someUrl');
  247. }).toThrow({
  248. name: 'ArgumentError',
  249. message: 'retryOn property expects an array or function'
  250. });
  251. });
  252. });
  253. });
  254. describe('when #defaults.retries=0', function () {
  255. beforeEach(function () {
  256. thenCallback = sinon.spy();
  257. catchCallback = sinon.spy();
  258. var fetchRetryWithDefaults = fetchBuilder(fetch, {retries: 0});
  259. fetchRetryWithDefaults('http://someurl')
  260. .then(thenCallback)
  261. .catch(catchCallback);
  262. });
  263. describe('when first call is a failure', function () {
  264. beforeEach(function () {
  265. deferred1.reject();
  266. });
  267. describe('when rejected', function () {
  268. it('invokes the catch callback', function () {
  269. expect(catchCallback.called).toBe(true);
  270. });
  271. it('does not call fetch again', function () {
  272. expect(fetch.callCount).toBe(1);
  273. });
  274. });
  275. });
  276. });
  277. describe('when #init.retries=1', function () {
  278. beforeEach(function () {
  279. thenCallback = sinon.spy();
  280. catchCallback = sinon.spy();
  281. fetchRetry('http://someurl', { retries: 1 })
  282. .then(thenCallback)
  283. .catch(catchCallback);
  284. });
  285. describe('when first call is a success', function () {
  286. beforeEach(function () {
  287. deferred1.resolve({ status: 200 });
  288. });
  289. describe('when resolved', function () {
  290. it('invokes the then callback', function () {
  291. expect(thenCallback.called).toBe(true);
  292. });
  293. it('calls fetch once', function () {
  294. expect(fetch.callCount).toBe(1);
  295. });
  296. });
  297. });
  298. describe('when first call is a failure', function () {
  299. beforeEach(function () {
  300. deferred1.reject();
  301. clock.tick(delay);
  302. });
  303. describe('when second call is a success', function () {
  304. beforeEach(function () {
  305. deferred2.resolve({ status: 200 });
  306. clock.tick(delay);
  307. });
  308. describe('when resolved', function () {
  309. it('invokes the then callback', function () {
  310. expect(thenCallback.called).toBe(true);
  311. });
  312. it('calls fetch twice', function () {
  313. expect(fetch.callCount).toBe(2);
  314. });
  315. });
  316. });
  317. describe('when second call is a failure', function () {
  318. beforeEach(function () {
  319. deferred2.reject();
  320. clock.tick(delay);
  321. });
  322. describe('when rejected', function () {
  323. it('invokes the catch callback', function () {
  324. expect(catchCallback.called).toBe(true);
  325. });
  326. it('does not call fetch again', function () {
  327. expect(fetch.callCount).toBe(2);
  328. });
  329. });
  330. });
  331. });
  332. });
  333. describe('when #init.retries=0', function () {
  334. beforeEach(function () {
  335. thenCallback = sinon.spy();
  336. catchCallback = sinon.spy();
  337. fetchRetry('http://someurl', { retries: 0 })
  338. .then(thenCallback)
  339. .catch(catchCallback);
  340. });
  341. describe('when first call is a success', function () {
  342. beforeEach(function () {
  343. deferred1.resolve({ status: 200 });
  344. });
  345. describe('when resolved', function () {
  346. it('invokes the then callback', function () {
  347. expect(thenCallback.called).toBe(true);
  348. });
  349. it('calls fetch once', function () {
  350. expect(fetch.callCount).toBe(1);
  351. });
  352. });
  353. });
  354. describe('when first call is a failure', function () {
  355. beforeEach(function () {
  356. deferred1.reject();
  357. });
  358. describe('when rejected', () => {
  359. it('invokes the catch callback', function () {
  360. expect(catchCallback.called).toBe(true);
  361. });
  362. });
  363. });
  364. });
  365. describe('when #init.retries is not a a positive integer', () => {
  366. ['1', -1, 'not a number', null].forEach(invalidRetries => {
  367. it('throws error', () => {
  368. const expectedError = {
  369. name: 'ArgumentError',
  370. message: 'retries must be a positive integer'
  371. };
  372. expect(() => {
  373. fetchRetry('http://someurl', { retries: invalidRetries });
  374. }).toThrow(expectedError);
  375. });
  376. });
  377. });
  378. });
  379. describe('#init.retryDelay', function () {
  380. describe('when #init.retryDelay is a number', function () {
  381. var init;
  382. var retryDelay;
  383. beforeEach(function () {
  384. retryDelay = 5000;
  385. init = {
  386. retryDelay: retryDelay
  387. };
  388. thenCallback = sinon.spy();
  389. fetchRetry('http://someUrl', init)
  390. .then(thenCallback);
  391. });
  392. describe('when first call is unsuccessful', function () {
  393. beforeEach(function () {
  394. deferred1.reject();
  395. });
  396. describe('after specified time', function () {
  397. beforeEach(function () {
  398. clock.tick(retryDelay);
  399. });
  400. it('invokes fetch again', function () {
  401. expect(fetch.callCount).toBe(2);
  402. });
  403. });
  404. describe('after less than specified time', function () {
  405. beforeEach(function () {
  406. clock.tick(1000);
  407. });
  408. it('does not invoke fetch again', function () {
  409. expect(fetch.callCount).toBe(1);
  410. });
  411. });
  412. });
  413. });
  414. describe('when #init.retryDelay is 0', function () {
  415. var init;
  416. var retryDelay;
  417. beforeEach(function () {
  418. retryDelay = 0;
  419. init = {
  420. retryDelay: retryDelay
  421. };
  422. thenCallback = sinon.spy();
  423. fetchRetry('http://someUrl', init)
  424. .then(thenCallback);
  425. });
  426. describe('when first call is unsuccessful', function () {
  427. beforeEach(function () {
  428. deferred1.reject();
  429. });
  430. describe('after one event loop tick', function () {
  431. beforeEach(function () {
  432. clock.tick(0);
  433. });
  434. it('invokes fetch again', function () {
  435. expect(fetch.callCount).toBe(2);
  436. });
  437. });
  438. });
  439. });
  440. describe('when #init.retryDelay is not a a positive integer', () => {
  441. ['1', -1, 'not a number', null].forEach(invalidDelay => {
  442. it('throws error', () => {
  443. const expectedError = {
  444. name: 'ArgumentError',
  445. message: 'retryDelay must be a positive integer or a function returning a positive integer'
  446. };
  447. expect(() => {
  448. fetchRetry('http://someurl', { retryDelay: invalidDelay });
  449. }).toThrow(expectedError);
  450. });
  451. });
  452. });
  453. describe('when #init.retryDelay is a function', function () {
  454. var init;
  455. var retryDelay;
  456. beforeEach(function () {
  457. retryDelay = sinon.stub().returns(5000);
  458. init = {
  459. retryDelay: retryDelay
  460. };
  461. thenCallback = sinon.spy();
  462. fetchRetry('http://someUrl', init)
  463. .then(thenCallback);
  464. });
  465. describe('when first call is unsuccessful', function () {
  466. beforeEach(function () {
  467. deferred1.reject(new Error('first error'));
  468. });
  469. describe('when the second call is a success', function () {
  470. beforeEach(function () {
  471. deferred2.resolve({ status: 200 });
  472. clock.tick(5000);
  473. });
  474. it('invokes the retryDelay function', function () {
  475. expect(retryDelay.called).toBe(true);
  476. expect(retryDelay.lastCall.args[0]).toEqual(0);
  477. expect(retryDelay.lastCall.args[1].message).toEqual('first error');
  478. });
  479. });
  480. describe('when second call is a failure', function () {
  481. beforeEach(function () {
  482. deferred2.reject(new Error('second error'));
  483. clock.tick(5000);
  484. });
  485. describe('when the third call is a success', function () {
  486. beforeEach(function () {
  487. deferred3.resolve({ status: 200 });
  488. clock.tick(5000);
  489. });
  490. it('invokes the retryDelay function again', function () {
  491. expect(retryDelay.callCount).toBe(2);
  492. expect(retryDelay.lastCall.args[0]).toEqual(1);
  493. expect(retryDelay.lastCall.args[1].message).toEqual('second error');
  494. });
  495. });
  496. });
  497. });
  498. });
  499. });
  500. describe('#init.retryOn', () => {
  501. describe('when #init.retryOn is an array', () => {
  502. var init;
  503. var retryOn;
  504. beforeEach(function () {
  505. retryOn = [503, 404];
  506. init = {
  507. retryOn: retryOn
  508. };
  509. thenCallback = sinon.spy();
  510. catchCallback = sinon.spy();
  511. fetchRetry('http://someUrl', init)
  512. .then(thenCallback)
  513. .catch((catchCallback));
  514. });
  515. describe('when first fetch is resolved with status code specified in retryOn array', () => {
  516. beforeEach(() => {
  517. deferred1.resolve({ status: 503 });
  518. });
  519. describe('after specified delay', () => {
  520. beforeEach(() => {
  521. clock.tick(delay);
  522. });
  523. it('retries fetch', () => {
  524. expect(fetch.callCount).toBe(2);
  525. });
  526. describe('when second fetch resolves with a different status code', () => {
  527. beforeEach(() => {
  528. deferred2.resolve({ status: 200 });
  529. });
  530. describe('when resolved', () => {
  531. it('invokes the then callback', function () {
  532. expect(thenCallback.called).toBe(true);
  533. });
  534. it('has called fetch twice', function () {
  535. expect(fetch.callCount).toBe(2);
  536. });
  537. });
  538. });
  539. });
  540. });
  541. });
  542. describe('when #init.retryOn is a function', function () {
  543. var init;
  544. var retryOn;
  545. var fetchRetryChain;
  546. beforeEach(function () {
  547. retryOn = sinon.stub();
  548. init = {
  549. retryOn: retryOn
  550. };
  551. thenCallback = sinon.spy();
  552. catchCallback = sinon.spy();
  553. fetchRetryChain = fetchRetry('http://someUrl', init)
  554. .then(thenCallback)
  555. .catch((catchCallback));
  556. });
  557. describe('when first attempt is rejected due to network error', function () {
  558. describe('when #retryOn() returns true', () => {
  559. beforeEach(function () {
  560. retryOn.returns(true);
  561. deferred1.reject(new Error('first error'));
  562. });
  563. describe('when rejected', function () {
  564. it('invokes #retryOn function with an error', function () {
  565. expect(retryOn.called).toBe(true);
  566. expect(retryOn.lastCall.args.length).toBe(3);
  567. expect(retryOn.lastCall.args[0]).toBe(0);
  568. expect(retryOn.lastCall.args[1] instanceof Error).toBe(true);
  569. expect(retryOn.lastCall.args[2]).toBe(null);
  570. });
  571. describe('after specified time', function () {
  572. beforeEach(function () {
  573. clock.tick(delay);
  574. });
  575. it('invokes fetch again', function () {
  576. expect(fetch.callCount).toBe(2);
  577. });
  578. describe('when the second call is unsuccessful', function () {
  579. beforeEach(function () {
  580. deferred2.reject(new Error('second error'));
  581. clock.tick(delay);
  582. });
  583. describe('when rejected', function () {
  584. it('invokes the #retryOn function twice', function () {
  585. expect(retryOn.callCount).toBe(2);
  586. expect(retryOn.lastCall.args[0]).toBe(1);
  587. });
  588. });
  589. });
  590. });
  591. });
  592. });
  593. describe('when #retryOn() returns false', () => {
  594. beforeEach(function () {
  595. retryOn.returns(false);
  596. deferred1.reject(new Error('first error'));
  597. });
  598. describe('when rejected', function () {
  599. it('invokes #retryOn function with an error', function () {
  600. expect(retryOn.called).toBe(true);
  601. expect(retryOn.lastCall.args.length).toBe(3);
  602. expect(retryOn.lastCall.args[0]).toBe(0);
  603. expect(retryOn.lastCall.args[1] instanceof Error).toBe(true);
  604. expect(retryOn.lastCall.args[2]).toBe(null);
  605. });
  606. describe('after specified time', function () {
  607. beforeEach(function () {
  608. clock.tick(delay);
  609. });
  610. it('invokes the catch callback', function () {
  611. expect(catchCallback.called).toBe(true);
  612. });
  613. it('does not call fetch again', function () {
  614. expect(fetch.callCount).toBe(1);
  615. });
  616. });
  617. });
  618. });
  619. });
  620. describe('when first attempt is resolved', function () {
  621. describe('when #retryOn() returns true', () => {
  622. beforeEach(function () {
  623. retryOn.returns(true);
  624. deferred1.resolve({ status: 200 });
  625. });
  626. describe('after specified delay', () => {
  627. beforeEach(function () {
  628. clock.tick(delay);
  629. });
  630. it('calls fetch again', function () {
  631. expect(fetch.callCount).toBe(2);
  632. });
  633. describe('when second call is resolved', () => {
  634. beforeEach(function () {
  635. deferred2.resolve({ status: 200 });
  636. clock.tick(delay);
  637. });
  638. it('invokes the #retryOn function with the response', function () {
  639. expect(retryOn.called).toBe(true);
  640. expect(retryOn.lastCall.args.length).toBe(3);
  641. expect(retryOn.lastCall.args[0]).toBe(0);
  642. expect(retryOn.lastCall.args[1]).toBe(null);
  643. expect(retryOn.lastCall.args[2]).toEqual({ status: 200 });
  644. });
  645. });
  646. });
  647. });
  648. describe('when #retryOn() returns false', () => {
  649. beforeEach(function () {
  650. retryOn.returns(false);
  651. deferred1.resolve({ status: 502 });
  652. });
  653. describe('when resolved', () => {
  654. it('invokes the then callback', function () {
  655. expect(thenCallback.called).toBe(true);
  656. });
  657. it('calls fetch 1 time only', function () {
  658. expect(fetch.callCount).toBe(1);
  659. });
  660. });
  661. });
  662. });
  663. describe('when first attempt is resolved with Promise', function() {
  664. describe('when #retryOn() returns Promise with true resolve', () => {
  665. beforeEach(function() {
  666. retryOn.resolves(true);
  667. deferred1.resolve({ status: 200 });
  668. });
  669. describe('after specified delay', () => {
  670. beforeEach(function() {
  671. clock.tick(delay);
  672. });
  673. it('calls fetch again', function() {
  674. expect(fetch.callCount).toBe(2);
  675. });
  676. describe('when second call is resolved', () => {
  677. beforeEach(function() {
  678. deferred2.resolve({ status: 200 });
  679. clock.tick(delay);
  680. });
  681. it('invokes the #retryOn function with the response', function() {
  682. expect(retryOn.called).toBe(true);
  683. expect(retryOn.lastCall.args.length).toBe(3);
  684. expect(retryOn.lastCall.args[0]).toBe(0);
  685. expect(retryOn.lastCall.args[1]).toBe(null);
  686. expect(retryOn.lastCall.args[2]).toEqual({ status: 200 });
  687. });
  688. });
  689. });
  690. });
  691. describe('when #retryOn() returns Promise with false resolve', () => {
  692. beforeEach(function() {
  693. retryOn.resolves(false);
  694. deferred1.resolve({ status: 502 });
  695. });
  696. describe('when resolved', () => {
  697. it('invokes the then callback', function() {
  698. expect(thenCallback.called).toBe(true);
  699. });
  700. it('calls fetch 1 time only', function() {
  701. expect(fetch.callCount).toBe(1);
  702. });
  703. });
  704. });
  705. describe('when #retryOn() throws an error', () => {
  706. beforeEach(function() {
  707. retryOn.throws();
  708. });
  709. describe('when rejected', () => {
  710. beforeEach(function() {
  711. deferred1.reject();
  712. });
  713. it('retryOn called only once', () => {
  714. return fetchRetryChain.finally(() => {
  715. expect(retryOn.callCount).toBe(1);
  716. });
  717. });
  718. it('invokes the catch callback', function() {
  719. return fetchRetryChain.finally(() => {
  720. expect(catchCallback.called).toBe(true);
  721. });
  722. });
  723. it('called fetch', function() {
  724. expect(fetch.callCount).toBe(1);
  725. });
  726. });
  727. describe('when resolved', () => {
  728. beforeEach(function() {
  729. deferred1.resolve({ status: 200 });
  730. });
  731. it('retryOn called only once', () => {
  732. return fetchRetryChain.finally(() => {
  733. expect(retryOn.callCount).toBe(1);
  734. });
  735. });
  736. it('invokes the catch callback', function() {
  737. return fetchRetryChain.finally(() => {
  738. expect(catchCallback.called).toBe(true);
  739. });
  740. });
  741. it('called fetch', function() {
  742. expect(fetch.callCount).toBe(1);
  743. });
  744. });
  745. });
  746. describe('when #retryOn() returns a Promise that rejects', () => {
  747. beforeEach(function() {
  748. retryOn.rejects();
  749. });
  750. describe('when rejected', () => {
  751. beforeEach(function() {
  752. deferred1.reject();
  753. });
  754. it('retryOn called only once', () => {
  755. return fetchRetryChain.finally(() => {
  756. expect(retryOn.callCount).toBe(1);
  757. });
  758. });
  759. it('invokes the catch callback', function() {
  760. return fetchRetryChain.finally(() => {
  761. expect(catchCallback.called).toBe(true);
  762. });
  763. });
  764. it('called fetch', function() {
  765. expect(fetch.callCount).toBe(1);
  766. });
  767. });
  768. describe('when resolved', () => {
  769. beforeEach(function() {
  770. deferred1.resolve({ status: 200 });
  771. });
  772. it('retryOn called only once', () => {
  773. return fetchRetryChain.finally(() => {
  774. expect(retryOn.callCount).toBe(1);
  775. });
  776. });
  777. it('invokes the catch callback', function() {
  778. return fetchRetryChain.finally(() => {
  779. expect(catchCallback.called).toBe(true);
  780. });
  781. });
  782. it('called fetch', function() {
  783. expect(fetch.callCount).toBe(1);
  784. });
  785. });
  786. });
  787. });
  788. });
  789. describe('when #init.retryOn is not an array or function', function () {
  790. var init;
  791. describe('when #init.retryOn is not an array or function', () => {
  792. it('throws exception', () => {
  793. expect(function () {
  794. init.retryOn = 503;
  795. fetchRetry('http://someUrl', init);
  796. }).toThrow({
  797. name: 'ArgumentError',
  798. message: 'retryOn property expects an array or function'
  799. });
  800. });
  801. });
  802. });
  803. });
  804. });
  805. function defer() {
  806. var resolve, reject;
  807. // eslint-disable-next-line no-undef
  808. var promise = new Promise(function () {
  809. resolve = arguments[0];
  810. reject = arguments[1];
  811. });
  812. return {
  813. resolve: resolve,
  814. reject: reject,
  815. promise: promise
  816. };
  817. }