minimal-slp-wallet
Version:
A minimalist Bitcoin Cash (BCH) wallet npm library, for use in a web apps.
141 lines (109 loc) • 3.59 kB
JavaScript
/*
Unit tests for the utxos.js library
*/
import chai from 'chai'
import sinon from 'sinon'
import cloneDeep from 'lodash.clonedeep'
import BCHJS from '@psf/bch-js'
import UTXOs from '../../lib/utxos.js'
import AdapterRouter from '../../lib/adapters/router.js'
import { tokenUtxos01 } from './mocks/utxo-mocks.js'
const { assert } = chai
let uut
let mockData
describe('#UTXOs', () => {
let sandbox
beforeEach(() => {
const config = {
restURL: 'https://api.fullstack.cash/v5/'
}
const bchjs = new BCHJS(config)
config.bchjs = bchjs
config.ar = new AdapterRouter(config)
uut = new UTXOs(config)
mockData = {
tokenUtxos01: cloneDeep(tokenUtxos01)
}
sandbox = sinon.createSandbox()
})
afterEach(() => sandbox.restore())
describe('#constructor', () => {
it('should throw an error if instance of bch-js is not passed', () => {
try {
uut = new UTXOs()
assert.fail('Unexpected code path')
} catch (err) {
assert.include(
err.message,
'Must pass instance of bch-js when instantiating AdapterRouter.'
)
}
})
it('should throw an error if instance of Adapter Router is not passed.', () => {
try {
uut = new UTXOs({ bchjs: {} })
assert.fail('Unexpected code path')
} catch (err) {
assert.include(err.message, 'Must pass instance of Adapter Router.')
}
})
})
describe('#initUtxoStore', () => {
it('should initialize and return the utxoStore', async () => {
const addr = 'bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
// Mock network calls.
sandbox.stub(uut.ar, 'getUtxos').resolves(mockData.tokenUtxos01)
const utxos = await uut.initUtxoStore(addr)
// console.log(`utxos: ${JSON.stringify(utxos, null, 2)}`)
assert.property(utxos, 'bchUtxos')
assert.property(utxos, 'nullUtxos')
assert.property(utxos, 'slpUtxos')
assert.property(utxos, 'address')
})
it('should handle errors', async () => {
try {
const addr = 'bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
// Force an error
sandbox.stub(uut.ar, 'getUtxos').rejects(new Error('test error'))
await uut.initUtxoStore(addr)
assert.fail('unexpected result')
} catch (err) {
assert.include(err.message, 'test error')
}
})
it('should handle network errors', async () => {
try {
const addr = 'bitcoincash:qzs02v05l7qs5s24srqju498qu55dwuj0cx5ehjm2c'
// Force an error
sandbox
.stub(uut.ar, 'getUtxos')
.resolves({ status: 422, message: 'test error' })
await uut.initUtxoStore(addr)
assert.fail('unexpected result')
} catch (err) {
// console.log(err)
assert.include(err.message, 'test error')
}
})
})
describe('#getSpendableTokenUtxos', () => {
it('should return all the spendable UTXOs', () => {
uut.utxoStore = mockData.tokenUtxos01
const tokenUtxos = uut.getSpendableTokenUtxos()
// console.log(`tokenUtxos: ${JSON.stringify(tokenUtxos, null, 2)}`)
for (let i = 0; i < tokenUtxos.length; i++) {
assert.equal(tokenUtxos[i].tokenType, 1)
}
})
it('should handle an error', () => {
try {
uut.utxoStore = {}
uut.getSpendableTokenUtxos()
assert.fail('Unexpected result')
} catch (err) {
// console.log(err)
assert.include(err.message, 'Cannot read')
}
})
})
})