Skip to content

Commit 7ed5d35

Browse files
Merge pull request #122 from Bitcoin-com/ct-transactions-unit
Mocked unit tests for Transactions lib
2 parents 9ecac02 + 9e9550a commit 7ed5d35

3 files changed

Lines changed: 86 additions & 26 deletions

File tree

lib/Transaction.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ export class Transaction {
2121

2222
// Array of addresses
2323
} else if (Array.isArray(txid)) {
24-
const options: AxiosRequestConfig = {
25-
method: "POST",
26-
url: `${this.restURL}transaction/details`,
27-
data: {
24+
// Dev note: must use axios.post for unit test stubbing.
25+
const response: AxiosResponse = await axios.post(
26+
`${this.restURL}transaction/details`,
27+
{
2828
txids: txid
2929
}
30-
}
31-
const response: AxiosResponse = await axios(options)
30+
)
3231

3332
return response.data
3433
}

test/unit/Transaction.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ import * as chai from "chai"
33
import { BITBOX } from "../../lib/BITBOX"
44
import { Transaction } from "../../lib/Transaction"
55
import { resturl } from "../../lib/BITBOX"
6-
import { TxnDetailsResult } from "bitcoin-com-rest";
6+
import { TxnDetailsResult } from "bitcoin-com-rest"
7+
import axios from "axios"
8+
import * as sinon from "sinon"
79

810
// consts
911
const bitbox: BITBOX = new BITBOX()
1012
const assert: Chai.AssertStatic = chai.assert
13+
const mockData = require("./mocks/transactions-mock")
1114

1215
describe("#Transaction", (): void => {
16+
let sandbox: any
17+
beforeEach(() => (sandbox = sinon.sandbox.create()))
18+
afterEach(() => sandbox.restore())
19+
1320
describe("#TransactionConstructor", (): void => {
1421
it("should create instance of Transaction", (): void => {
1522
const transaction: Transaction = new Transaction()
@@ -23,10 +30,19 @@ describe("#Transaction", (): void => {
2330
})
2431

2532
describe(`#details`, (): void => {
26-
it(`should GET details for a given txid`, async () => {
33+
it(`should GET details for a given txid`, async (): Promise<any> => {
34+
// Mock the call to rest to prevent live network calls.
35+
const resolved = new Promise(r => r({ data: mockData.details }))
36+
sandbox.stub(axios, "get").returns(resolved)
37+
2738
const txid: string =
2839
"fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33"
29-
const result: TxnDetailsResult | TxnDetailsResult[] = await bitbox.Transaction.details(txid)
40+
41+
const result:
42+
| TxnDetailsResult
43+
| TxnDetailsResult[] = await bitbox.Transaction.details(txid)
44+
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
45+
3046
assert.hasAllKeys(result, [
3147
"txid",
3248
"version",
@@ -45,39 +61,39 @@ describe("#Transaction", (): void => {
4561
})
4662

4763
it(`should GET details for an array of txids`, async () => {
64+
// Mock the call to rest to prevent live network calls.
65+
const testData = [mockData.details, mockData.details]
66+
const resolved = new Promise(r => r({ data: testData }))
67+
sandbox.stub(axios, "post").returns(resolved)
68+
4869
const txids: string[] = [
4970
"fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33",
5071
"fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33"
5172
]
52-
const result: TxnDetailsResult | TxnDetailsResult[] = await bitbox.Transaction.details(txids)
73+
const result:
74+
| TxnDetailsResult
75+
| TxnDetailsResult[] = await bitbox.Transaction.details(txids)
5376
assert.isArray(result)
5477
})
5578

56-
it(`should throw an error for improper single input`, async () => {
79+
it(`should pass error from server to user`, async (): Promise<any> => {
5780
try {
81+
// Mock out data for unit test, to prevent live network call.
82+
sandbox
83+
.stub(axios, "get")
84+
.throws("error", "Input txid must be a string or array of strings.")
85+
5886
const txid: any = 12345
87+
5988
await bitbox.Transaction.details(txid)
6089
assert.equal(true, false, "Unexpected result!")
6190
} catch (err) {
91+
//console.log(`err: ${util.inspect(err)}`)
6292
assert.include(
6393
err.message,
64-
`Input txid must be a string or array of strings`
94+
`Input txid must be a string or array of strings.`
6595
)
6696
}
6797
})
68-
69-
it(`should throw error on array size rate limit`, async () => {
70-
try {
71-
const dataMock: string =
72-
"fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33"
73-
const data: string[] = []
74-
for (let i: number = 0; i < 25; i++) data.push(dataMock)
75-
await bitbox.Transaction.details(data)
76-
assert.equal(false, false, "Unexpected result!")
77-
} catch (err) {
78-
assert.hasAnyKeys(err, ["error"])
79-
assert.include(err.error, "Array too large")
80-
}
81-
})
8298
})
8399
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Mock data used for unit testing.
3+
*/
4+
5+
module.exports = {
6+
details: {
7+
txid: "fe28050b93faea61fa88c4c630f0e1f0a1c24d0082dd0e10d369e13212128f33",
8+
version: 1,
9+
locktime: 0,
10+
vin: [
11+
{
12+
coinbase: "04ffff001d02fd04",
13+
sequence: 4294967295,
14+
n: 0
15+
}
16+
],
17+
vout: [
18+
{
19+
value: "50.00000000",
20+
n: 0,
21+
scriptPubKey: {
22+
hex:
23+
"4104f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446aac",
24+
asm:
25+
"04f5eeb2b10c944c6b9fbcfff94c35bdeecd93df977882babc7f3a2cf7f5c81d3b09a68db7f0e04f21de5d4230e75e6dbe7ad16eefe0d4325a62067dc6f369446a OP_CHECKSIG",
26+
addresses: ["1BW18n7MfpU35q4MTBSk8pse3XzQF8XvzT"],
27+
type: "pubkeyhash",
28+
cashAddrs: ["bitcoincash:qpej6mkrwca4tvy2snq4crhrf88v4ljspysx0ueetk"]
29+
},
30+
spentTxId: null,
31+
spentIndex: null,
32+
spentHeight: null
33+
}
34+
],
35+
blockhash:
36+
"00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09",
37+
blockheight: 1000,
38+
confirmations: 585610,
39+
time: 1232346882,
40+
blocktime: 1232346882,
41+
isCoinBase: true,
42+
valueOut: 50,
43+
size: 135
44+
}
45+
}

0 commit comments

Comments
 (0)