tokenbridge/oracle/test/tryEach.test.js
Przemyslaw Rzad d656025378
Update the token-bridge sub-repo (#19)
* Moved audit to root directory.

* Moved code of conduct, contributing and licence files.

* Removed travis config and badge.

* Updated repository links in CONTRIBUTING.

* Moved Gitter badge to main readme. Updated links to contributing and licence.

* Updated main readme.

* Moved references to main readme.

* Renamed token-bridge to oracle.

* Update README.md

Co-Authored-By: rzadp <rzadp@student.mini.pw.edu.pl>
2019-05-07 16:52:44 +02:00

66 lines
1.7 KiB
JavaScript

const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const tryEach = require('../src/utils/tryEach')
chai.use(chaiAsPromised)
const { expect } = chai
describe('tryEach', () => {
it('should work with the first element', async () => {
// given
const array = [1, 2, 3]
const f = x => (x === 1 ? Promise.resolve(2 * x) : Promise.reject())
// when
const [result, index] = await tryEach(array, f)
// then
expect(result).to.equal(2)
expect(index).to.equal(0)
})
it('should work with the second element', async () => {
// given
const array = [1, 2, 3]
const f = x => (x === 2 ? Promise.resolve(2 * x) : Promise.reject())
// when
const [result, index] = await tryEach(array, f)
// then
expect(result).to.equal(4)
expect(index).to.equal(1)
})
it('should work with the last element', async () => {
// given
const array = [1, 2, 3]
const f = x => (x === array[array.length - 1] ? Promise.resolve(2 * x) : Promise.reject())
// when
const [result, index] = await tryEach(array, f)
// then
expect(result).to.equal(6)
expect(index).to.equal(2)
})
it('should return array with errors if all elements fail', async () => {
// given
const array = [1, 2, 3]
const f = x => Promise.reject(-x) // eslint-disable-line prefer-promise-reject-errors
// when
return expect(tryEach(array, f)).to.be.rejected.and.eventually.deep.equal([-1, -2, -3])
})
it('should return an empty array if input array is empty', async () => {
// given
const array = []
const f = x => Promise.resolve(x)
// when
return expect(tryEach(array, f)).to.be.rejected.and.eventually.deep.equal([])
})
})