-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import sync from './sync'; | ||
import async from './async'; | ||
|
||
describe('retry', () => { | ||
describe('sync', sync); | ||
describe('async', async); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<number> = [], | ||
attempts: Array<number> = []; | ||
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]); | ||
}); | ||
}; |