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

177 lines
3.9 KiB
JavaScript
Raw Normal View History

import assert from 'assert'
import chai from 'chai'
import axios from 'axios'
import BCHJS from '../../src/bch-js.js'
import sinon from 'sinon'
const { assert: assert2 } = chai
2020-07-27 21:32:51 -07:00
const bchjs = new BCHJS()
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
}
2025-11-16 13:47:24 -08:00
const resolved = new Promise(resolve => resolve({ 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)
})
})
describe('#floor2', () => {
it('should round a number to 2 decimals', () => {
const num = 1.234567891111
const result = bchjs.Util.floor2(num)
// console.log(result)
assert2.equal(result, 1.23)
})
it('should throw an error for non-number input', () => {
try {
const num = 'string'
bchjs.Util.floor2(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.2
const result = bchjs.Util.floor2(num)
// console.log(result)
assert2.equal(result, 1.2)
})
})
describe('#chunk20', () => {
const thirtyFiveElements = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29,
30,
31,
32,
33,
34
]
it('should split 35 elements into 2 arrays', () => {
const result = bchjs.Util.chunk20(thirtyFiveElements)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert2.isArray(result)
assert2.isArray(result[0])
assert2.equal(result.length, 2)
assert2.equal(result[0].length, 20)
})
it('should return accurately with an array of less than 20 elements', () => {
const elems = [0, 1, 2, 3, 4, 5]
const result = bchjs.Util.chunk20(elems)
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
assert2.isArray(result)
assert2.isArray(result[0])
assert2.equal(result.length, 1)
assert2.equal(result[0].length, 6)
})
it('should throw an error for non-array input', () => {
try {
bchjs.Util.chunk20('string')
assert2.equal(true, false, 'Unexpected result')
} catch (err) {
// console.log(err)
assert2.include(err.message, 'input must be an array')
}
})
})
2020-07-27 21:32:51 -07:00
})