Files
bch-js/test/unit/util.js
T

70 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-01-02 15:41:12 -08:00
const assert = require('assert')
const assert2 = require('chai').assert
2021-01-02 15:41:12 -08:00
const axios = require('axios')
const BCHJS = require('../../src/bch-js')
2020-07-27 21:32:51 -07:00
const bchjs = new BCHJS()
2021-01-02 15:41:12 -08:00
const sinon = require('sinon')
2020-07-27 21:32:51 -07:00
2021-01-02 15:41:12 -08:00
describe('#Util', () => {
describe('#validateAddress', () => {
2020-07-27 21:32:51 -07:00
let sandbox
beforeEach(() => (sandbox = sinon.createSandbox()))
afterEach(() => sandbox.restore())
2021-01-02 15:41:12 -08:00
it('should validate address', done => {
2020-07-27 21:32:51 -07:00
const data = {
isvalid: true,
2021-01-02 15:41:12 -08:00
address: 'bitcoincash:qpz7qtkuyhrsz4qmnnrvf8gz9zd0u9v7eqsewyk4w5',
scriptPubKey: '76a91445e02edc25c701541b9cc6c49d02289afe159ec888ac',
2020-07-27 21:32:51 -07:00
ismine: false,
iswatchonly: false,
isscript: false
}
2021-01-02 16:39:44 -08:00
const resolved = new Promise(resolve => resolve({ data: data }))
2021-01-02 15:41:12 -08:00
sandbox.stub(axios, 'get').returns(resolved)
2020-07-27 21:32:51 -07:00
bchjs.Util.validateAddress(
2021-01-02 15:41:12 -08:00
'bitcoincash:qpz7qtkuyhrsz4qmnnrvf8gz9zd0u9v7eqsewyk4w5'
2020-07-27 21:32:51 -07:00
)
.then(result => {
2021-01-02 16:39:44 -08:00
assert.deepStrictEqual(data, result)
2020-07-27 21:32:51 -07:00
})
.then(done, done)
})
})
describe('#floor8', () => {
it('should floor a number to 8 decimals', () => {
const num = 1.234567891111
const result = bchjs.Util.floor8(num)
// console.log(result)
assert2.equal(result, 1.23456789)
})
it('should throw an error for non-number input', () => {
try {
const num = 'string'
bchjs.Util.floor8(num)
assert2.equal(true, false, 'Unexpected result')
} catch (err) {
// console.log(err)
assert2.include(err.message, 'input must be a number')
}
})
it('should not effect a number with less than 8 decimals', () => {
const num = 1.23
const result = bchjs.Util.floor8(num)
// console.log(result)
assert2.equal(result, 1.23)
})
})
2020-07-27 21:32:51 -07:00
})