From 3bc72202329c9502236835ccd514da287dcf64b6 Mon Sep 17 00:00:00 2001 From: Vitaly Tomilov Date: Fri, 2 Aug 2024 17:27:07 +0100 Subject: [PATCH] testing added --- test/ops/retry/index.spec.ts | 2 ++ test/ops/retry/sync.ts | 51 +++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/test/ops/retry/index.spec.ts b/test/ops/retry/index.spec.ts index b166a053..f4363d8c 100644 --- a/test/ops/retry/index.spec.ts +++ b/test/ops/retry/index.spec.ts @@ -1,5 +1,7 @@ +import sync from './sync'; import async from './async'; describe('retry', () => { + describe('sync', sync); describe('async', async); }); diff --git a/test/ops/retry/sync.ts b/test/ops/retry/sync.ts index 5b1a47bc..943d8f96 100644 --- a/test/ops/retry/sync.ts +++ b/test/ops/retry/sync.ts @@ -1,5 +1,54 @@ import {expect} from '../../header'; -import {pipe, retry} from '../../../src'; +import {pipe, retry, tap} from '../../../src'; export default () => { + it('must not retry on 0 attempts', () => { + let count = 0; + const i = pipe( + [1, 2, 3], + tap(() => { + if (!count++) { + throw 'ops!'; // throw only once + } + }), + retry(0) + ); + expect(() => { + [...i]; + }).to.throw('ops!'); + }); + it('must retry the number of attempts', () => { + let count = 0; + const i = pipe( + [1, 2, 3], + tap(() => { + if (!count++) { + throw 'ops!'; // throw only once + } + }), + retry(1) + ); + expect([...i]).to.eql([2, 3]); + }); + it('must retry on callback result', () => { + let count = 0; + const indexes: Array = [], + attempts: Array = []; + const i = pipe( + [1, 2, 3, 4, 5], + tap(() => { + if (count++ < 3) { + throw 'ops!'; // throw 3 times + } + }), + retry((idx, att) => { + indexes.push(idx); + attempts.push(att); + return true; + }) + ); + expect([...i]).to.eql([4, 5]); + expect(indexes).to.eql([0, 0, 0]); + expect(attempts).to.eql([0, 1, 2]); + }); };