From 4d86d3b38b511deb081e61bbe661698d0da0ce40 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Fri, 17 Apr 2020 11:27:50 -0700 Subject: [PATCH] Added unit tests for new electrumx endpoints --- test/unit/electrumx.js | 146 +++++++++++++++++++++++++++ test/unit/fixtures/electrumx-mock.js | 113 ++++++++++++++++++++- 2 files changed, 257 insertions(+), 2 deletions(-) diff --git a/test/unit/electrumx.js b/test/unit/electrumx.js index 716cb65..50fdd3e 100644 --- a/test/unit/electrumx.js +++ b/test/unit/electrumx.js @@ -49,5 +49,151 @@ describe(`#ElectrumX`, () => { assert.property(result.utxos[0], "tx_pos") assert.property(result.utxos[0], "value") }) + + it(`should POST utxo details for an array of addresses`, async () => { + // Stub the network call. + sandbox.stub(axios, "post").resolves({ data: mockData.utxos }) + + const addr = [ + "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", + "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + ] + + const result = await bchjs.Electrumx.utxo(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "success") + assert.equal(result.success, true) + + assert.property(result, "utxos") + assert.isArray(result.utxos) + + assert.property(result.utxos[0], "utxos") + assert.isArray(result.utxos[0].utxos) + assert.property(result.utxos[0], "address") + + assert.property(result.utxos[0].utxos[0], "height") + assert.property(result.utxos[0].utxos[0], "tx_hash") + assert.property(result.utxos[0].utxos[0], "tx_pos") + assert.property(result.utxos[0].utxos[0], "value") + }) + }) + + describe(`#balance`, () => { + it(`should throw an error for improper input`, async () => { + try { + const addr = 12345 + + await bchjs.Electrumx.balance(addr) + assert.equal(true, false, "Unexpected result!") + } catch (err) { + // console.log(`err: `, err) + assert.include( + err.message, + `Input address must be a string or array of strings` + ) + } + }) + + it(`should GET balance for a single address`, async () => { + // Stub the network call. + sandbox.stub(axios, "get").resolves({ data: mockData.balance }) + + const addr = "bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9" + + const result = await bchjs.Electrumx.balance(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "success") + assert.equal(result.success, true) + + assert.property(result, "balance") + assert.property(result.balance, "confirmed") + assert.property(result.balance, "unconfirmed") + }) + + it(`should POST balance for an array of addresses`, async () => { + // Stub the network call. + sandbox.stub(axios, "post").resolves({ data: mockData.balances }) + + const addr = [ + "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", + "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + ] + + const result = await bchjs.Electrumx.balance(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "success") + assert.equal(result.success, true) + + assert.property(result, "balances") + assert.isArray(result.balances) + + assert.property(result.balances[0], "address") + assert.property(result.balances[0], "balance") + assert.property(result.balances[0].balance, "confirmed") + assert.property(result.balances[0].balance, "unconfirmed") + }) + }) + + describe(`#transactions`, () => { + it(`should throw an error for improper input`, async () => { + try { + const addr = 12345 + + await bchjs.Electrumx.transactions(addr) + assert.equal(true, false, "Unexpected result!") + } catch (err) { + // console.log(`err: `, err) + assert.include( + err.message, + `Input address must be a string or array of strings` + ) + } + }) + + it(`should GET transactions for a single address`, async () => { + // Stub the network call. + sandbox.stub(axios, "get").resolves({ data: mockData.transaction }) + + const addr = "bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9" + + const result = await bchjs.Electrumx.transactions(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "success") + assert.equal(result.success, true) + + assert.property(result, "transactions") + assert.isArray(result.transactions) + assert.property(result.transactions[0], "height") + assert.property(result.transactions[0], "tx_hash") + }) + + it(`should POST transaction history for an array of addresses`, async () => { + // Stub the network call. + sandbox.stub(axios, "post").resolves({ data: mockData.transactions }) + + const addr = [ + "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf", + "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + ] + + const result = await bchjs.Electrumx.transactions(addr) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) + + assert.property(result, "success") + assert.equal(result.success, true) + + assert.property(result, "transactions") + assert.isArray(result.transactions) + + assert.property(result.transactions[0], "address") + assert.property(result.transactions[0], "transactions") + assert.isArray(result.transactions[0].transactions) + assert.property(result.transactions[0].transactions[0], "height") + assert.property(result.transactions[0].transactions[0], "tx_hash") + }) }) }) diff --git a/test/unit/fixtures/electrumx-mock.js b/test/unit/fixtures/electrumx-mock.js index 6ed32fc..8e951c7 100644 --- a/test/unit/fixtures/electrumx-mock.js +++ b/test/unit/fixtures/electrumx-mock.js @@ -15,6 +15,115 @@ const utxo = { ] } -module.exports = { - utxo +const utxos = { + success: true, + utxos: [ + { + utxos: [ + { + height: 604392, + tx_hash: + "7774e449c5a3065144cefbc4c0c21e6b69c987f095856778ef9f45ddd8ae1a41", + tx_pos: 0, + value: 1000 + }, + { + height: 630834, + tx_hash: + "4fe60a51e0d8f5134bfd8e5f872d6e502d7f01b28a6afebb27f4438a4f638d53", + tx_pos: 0, + value: 6000 + } + ], + address: "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" + }, + { + utxos: [], + address: "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + } + ] +} + +const balance = { + success: true, + balance: { + confirmed: 1000, + unconfirmed: 0 + } +} + +const balances = { + success: true, + balances: [ + { + balance: { + confirmed: 7000, + unconfirmed: 0 + }, + address: "bitcoincash:qrdka2205f4hyukutc2g0s6lykperc8nsu5u2ddpqf" + }, + { + balance: { + confirmed: 0, + unconfirmed: 0 + }, + address: "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + } + ] +} + +const transaction = { + success: true, + transactions: [ + { + height: 560430, + tx_hash: + "3e1f3e882be9c03897eeb197224bf87f312be556a89f4308fabeeeabcf9bc851" + }, + { + height: 560534, + tx_hash: + "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" + } + ] +} + +const transactions = { + success: true, + transactions: [ + { + transactions: [ + { + height: 631219, + tx_hash: + "ae2daa01c8172545b5edd205ea438706bcb74e63d4084a26b9ff2a46d46dc97f" + } + ], + address: "bitcoincash:qrl2nlsaayk6ekxn80pq0ks32dya8xfclyktem2mqj" + }, + { + transactions: [ + { + height: 560430, + tx_hash: + "3e1f3e882be9c03897eeb197224bf87f312be556a89f4308fabeeeabcf9bc851" + }, + { + height: 560534, + tx_hash: + "4ebbeaac51ce141e262964e3a0ce11b96ca72c0dffe9b4127ce80135f503a280" + } + ], + address: "bitcoincash:qpdh9s677ya8tnx7zdhfrn8qfyvy22wj4qa7nwqa5v" + } + ] +} + +module.exports = { + utxo, + utxos, + balance, + balances, + transaction, + transactions }