tokenbridge/oracle/test/utils.test.js

190 lines
5.3 KiB
JavaScript
Raw Permalink Normal View History

const sinon = require('sinon')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const BigNumber = require('bignumber.js')
const proxyquire = require('proxyquire')
const Web3Utils = require('web3-utils')
2019-05-24 15:42:26 -03:00
const { addExtraGas, syncForEach, generateGasPriceOptions } = require('../src/utils/utils')
2019-07-25 12:36:10 -03:00
const { GAS_PRICE_OPTIONS, ORACLE_GAS_PRICE_SPEEDS } = require('../../commons')
chai.use(chaiAsPromised)
const { expect } = chai
describe('utils', () => {
describe('addExtraGas', () => {
it('should return a BigNumber', () => {
const result = addExtraGas(100, 0.25)
expect(BigNumber.isBigNumber(result)).to.equal(true)
})
it('should work with numbers', () => {
const result = addExtraGas(100, 0.25)
expect(result.toString()).to.equal('125')
})
it('should work with BigNumbers', () => {
const result = addExtraGas(new BigNumber(100), 0.25)
expect(result.toString()).to.equal('125')
})
it('should accept factors bigger than 1', () => {
const result = addExtraGas(new BigNumber(100), 1.25)
expect(result.toString()).to.equal('225')
})
})
describe('checkHTTPS', () => {
let utils
const logger = { warn: sinon.stub() }
beforeEach(() => {
logger.warn.reset()
utils = proxyquire('../src/utils/utils', { '../services/logger': logger })
})
it('should do nothing if HTTP is allowed and the URL is https', () => {
utils.checkHTTPS('yes', logger)('home')('https://www.google.com')
expect(logger.warn.called).to.equal(false)
})
it('should emit a warning if HTTP is allowed and the URL is http', () => {
utils.checkHTTPS('yes', logger)('home')('http://www.google.com')
expect(logger.warn.called).to.equal(true)
})
it('should do nothing if HTTP is not allowed and the URL is https', () => {
utils.checkHTTPS('no', logger)('home')('https://www.google.com')
expect(logger.warn.called).to.equal(false)
})
it('should throw an error if HTTP is not allowed and the URL is http', () => {
expect(() => utils.checkHTTPS('no', logger)('home')('http://www.google.com')).to.throw()
})
})
describe('syncForEach', () => {
it('should execute callback sequentially', async () => {
const xs = []
await syncForEach(
[1, 2, 3],
x =>
new Promise(resolve => {
xs.push(x)
resolve()
})
)
expect(xs).to.deep.equal([1, 2, 3])
})
it('should receive the index and the full array', async () => {
const xs = []
const is = []
const arrays = []
await syncForEach(
[1, 2, 3],
(x, i, array) =>
new Promise(resolve => {
xs.push(x)
is.push(i)
arrays.push(array)
resolve()
})
)
expect(xs).to.deep.equal([1, 2, 3])
expect(is).to.deep.equal([0, 1, 2])
expect(arrays).to.deep.equal([[1, 2, 3], [1, 2, 3], [1, 2, 3]])
})
it('should fail and stop when the callback fails', () => {
const xs = []
const promise = syncForEach(
[1, 2, 3],
x =>
new Promise((resolve, reject) => {
if (x === 2) {
return reject()
}
xs.push(x)
return resolve()
})
)
return expect(promise).to.be.rejected.then(() => {
expect(xs).to.deep.equal([1])
})
})
it('should work with an empty array', () => {
const xs = []
const promise = syncForEach(
[],
x =>
new Promise(resolve => {
xs.push(x)
return resolve()
})
)
return expect(promise).to.be.fulfilled.then(() => {
expect(xs).to.deep.equal([])
})
})
})
2019-05-24 15:42:26 -03:00
describe('generateGasPriceOptions', () => {
it('should work for GAS_PRICE option', () => {
// given
const dataType = GAS_PRICE_OPTIONS.GAS_PRICE
2019-08-22 13:37:38 -03:00
const gasPrice = Web3Utils.toBN('0000000000000000000000000000000000000000000000000000000165a0bc00')
2019-05-24 15:42:26 -03:00
const gasPriceSpeed = null
const expectedResult = {
type: dataType,
value: Web3Utils.hexToNumberString(gasPrice)
2019-05-24 15:42:26 -03:00
}
// when
const gasPriceOptions = generateGasPriceOptions({ dataType, gasPrice, gasPriceSpeed })
// then
expect(gasPriceOptions.type).to.be.equal(expectedResult.type)
expect(gasPriceOptions.value).to.be.equal(expectedResult.value)
})
it('should work for SPEED option', () => {
// given
const dataType = GAS_PRICE_OPTIONS.SPEED
const gasPrice = null
const gasPriceSpeed = ORACLE_GAS_PRICE_SPEEDS.STANDARD
const expectedResult = {
type: dataType,
value: gasPriceSpeed
}
// when
const gasPriceOptions = generateGasPriceOptions({ dataType, gasPrice, gasPriceSpeed })
// then
expect(gasPriceOptions.type).to.be.equal(expectedResult.type)
expect(gasPriceOptions.value).to.be.equal(expectedResult.value)
})
it('should return null option for undefined option', () => {
// given
const dataType = GAS_PRICE_OPTIONS.UNDEFINED
const gasPrice = null
const gasPriceSpeed = null
// when
const gasPriceOptions = generateGasPriceOptions({ dataType, gasPrice, gasPriceSpeed })
// then
expect(gasPriceOptions).to.be.equal(null)
})
})
})