Skip to content

Commit 9ecac02

Browse files
Merge pull request #121 from Bitcoin-com/ct-rawtransactions-unit
Refactored RawTransactions Unit Tests
2 parents 7f70b6d + 53ed761 commit 9ecac02

21 files changed

Lines changed: 381 additions & 210 deletions

lib/RawTransactions.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ export class RawTransactions {
2222

2323
// Array of hexes
2424
} else if (Array.isArray(hex)) {
25-
const options: AxiosRequestConfig = {
26-
method: "POST",
27-
url: `${this.restURL}rawtransactions/decodeRawTransaction`,
28-
data: {
25+
// Dev note: must use axios.post for unit test stubbing.
26+
const response: AxiosResponse = await axios.post(
27+
`${this.restURL}rawtransactions/decodeRawTransaction`,
28+
{
2929
hexes: hex
3030
}
31-
}
32-
const response: AxiosResponse = await axios(options)
31+
)
3332

3433
return response.data
3534
}
@@ -52,14 +51,13 @@ export class RawTransactions {
5251

5352
return response.data
5453
} else if (Array.isArray(script)) {
55-
const options: AxiosRequestConfig = {
56-
method: "POST",
57-
url: `${this.restURL}rawtransactions/decodeScript`,
58-
data: {
54+
// Dev note: must use axios.post for unit test stubbing.
55+
const response: AxiosResponse = await axios.post(
56+
`${this.restURL}rawtransactions/decodeScript`,
57+
{
5958
hexes: script
6059
}
61-
}
62-
const response: AxiosResponse = await axios(options)
60+
)
6361

6462
return response.data
6563
}
@@ -90,15 +88,14 @@ export class RawTransactions {
9088

9189
return response.data
9290
} else if (Array.isArray(txid)) {
93-
const options: AxiosRequestConfig = {
94-
method: "POST",
95-
url: `${this.restURL}rawtransactions/getRawTransaction`,
96-
data: {
91+
// Dev note: must use axios.post for unit test stubbing.
92+
const response: AxiosResponse = await axios.post(
93+
`${this.restURL}rawtransactions/getRawTransaction`,
94+
{
9795
txids: txid,
9896
verbose: verbose
9997
}
100-
}
101-
const response: AxiosResponse = await axios(options)
98+
)
10299

103100
return response.data
104101
}
@@ -133,14 +130,14 @@ export class RawTransactions {
133130

134131
// Array input
135132
} else if (Array.isArray(hex)) {
136-
const options: AxiosRequestConfig = {
137-
method: "POST",
138-
url: `${this.restURL}rawtransactions/sendRawTransaction`,
139-
data: {
133+
134+
// Dev note: must use axios.post for unit test stubbing.
135+
const response: AxiosResponse = await axios.post(
136+
`${this.restURL}rawtransactions/sendRawTransaction`,
137+
{
140138
hexes: hex
141139
}
142-
}
143-
const response: AxiosResponse = await axios(options)
140+
)
144141

145142
return response.data
146143
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"test": "tsc && nyc mocha test/unit --timeout 60000 --exit",
1111
"test:no-build": "nyc mocha test/unit --timeout 60000 --exit",
1212
"test:integration": "TEST=integration nyc --reporter=text mocha --timeout 30000 test/integration",
13+
"test:integration:local": "TEST=integration SERVER=local nyc --reporter=text mocha --timeout 30000 test/integration --exit",
14+
"test:integration:stage": "TEST=integration SERVER=stage nyc --reporter=text mocha --timeout 30000 test/integration --exit",
1315
"coverage": "nyc report --reporter=text-lcov | coveralls",
1416
"build": "tsc"
1517
},

test/integration/address.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ const chai = require("chai")
1111
const assert = chai.assert
1212

1313
const BITBOX = require("../../lib/BITBOX").BITBOX
14-
const bitbox = new BITBOX()
15-
//const axios = require("axios")
14+
let bitbox = new BITBOX()
15+
16+
if (process.env.SERVER === "local")
17+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
18+
if (process.env.SERVER === "stage")
19+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1620

1721
// Inspect utility used for debugging.
1822
const util = require("util")

test/integration/block.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ const chai = require("chai")
77
const assert = chai.assert
88

99
const BITBOX = require("../../lib/BITBOX").BITBOX
10-
const bitbox = new BITBOX()
10+
let bitbox = new BITBOX()
11+
12+
if (process.env.SERVER === "local")
13+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
14+
if (process.env.SERVER === "stage")
15+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1116

1217
// Inspect utility used for debugging.
1318
const util = require("util")

test/integration/blockchain.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ const chai = require("chai")
1111
const assert = chai.assert
1212

1313
const BITBOX = require("../../lib/BITBOX").BITBOX
14-
const bitbox = new BITBOX()
14+
let bitbox = new BITBOX()
15+
16+
if (process.env.SERVER === "local")
17+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
18+
if (process.env.SERVER === "stage")
19+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1520

1621
// Inspect utility used for debugging.
1722
const util = require("util")

test/integration/price.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const assert = require("assert")
22
const BITBOX = require("../../lib/BITBOX").BITBOX
3-
const bitbox = new BITBOX()
3+
let bitbox = new BITBOX()
4+
5+
if (process.env.SERVER === "local")
6+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
7+
if (process.env.SERVER === "stage")
8+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
49

510
describe("#price", () => {
611
describe("#current", () => {

test/integration/rawtransaction.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const chai = require("chai")
99
const assert = chai.assert
1010

1111
const BITBOX = require("../../lib/BITBOX").BITBOX
12-
const bitbox = new BITBOX()
12+
let bitbox = new BITBOX()
13+
14+
if (process.env.SERVER === "local")
15+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
16+
if (process.env.SERVER === "stage")
17+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1318

1419
// Inspect utility used for debugging.
1520
const util = require("util")

test/integration/transaction.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ const chai = require("chai")
77
const assert = chai.assert
88

99
const BITBOX = require("../../lib/BITBOX").BITBOX
10-
const bitbox = new BITBOX()
10+
let bitbox = new BITBOX()
11+
12+
if (process.env.SERVER === "local")
13+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
14+
if (process.env.SERVER === "stage")
15+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1116

1217
// Inspect utility used for debugging.
1318
const util = require("util")

test/integration/util.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ const chai = require("chai")
77
const assert = chai.assert
88

99
const BITBOX = require("../../lib/BITBOX").BITBOX
10-
const bitbox = new BITBOX()
10+
let bitbox = new BITBOX()
11+
12+
if (process.env.SERVER === "local")
13+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
14+
if (process.env.SERVER === "stage")
15+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1116

1217
// Inspect utility used for debugging.
1318
const util = require("util")

test/integration/z9-rate-limits.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ const chai = require("chai")
88
const assert = chai.assert
99

1010
const BITBOX = require("../../lib/BITBOX").BITBOX
11-
const bitbox = new BITBOX()
11+
let bitbox = new BITBOX()
12+
13+
if (process.env.SERVER === "local")
14+
bitbox = new BITBOX({ restURL: "http://localhost:3000/v2/" })
15+
if (process.env.SERVER === "stage")
16+
bitbox = new BITBOX({ restURL: "https://rest.btctest.net/v2/" })
1217

1318
// Inspect utility used for debugging.
1419
const util = require("util")

0 commit comments

Comments
 (0)