Skip to content

Commit dce8ee4

Browse files
committed
refactored util lib and unit tests with mocks
1 parent 9e9550a commit dce8ee4

3 files changed

Lines changed: 65 additions & 41 deletions

File tree

lib/Util.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ export class Util {
3232

3333
// Array of blocks.
3434
} else if (Array.isArray(address)) {
35-
const options: AxiosRequestConfig = {
36-
method: "POST",
37-
url: `${this.restURL}util/validateAddress`,
38-
data: {
35+
// Dev note: must use axios.post for unit test stubbing.
36+
const response: AxiosResponse = await axios.post(
37+
`${this.restURL}util/validateAddress`,
38+
{
3939
addresses: address
4040
}
41-
}
42-
const response: AxiosResponse = await axios(options)
41+
)
4342

4443
return response.data
4544
}

test/unit/Util.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { BITBOX } from "../../lib/BITBOX"
44
import { Util } from "../../lib/Util"
55
import { resturl } from "../../lib/BITBOX"
66
import * as util from "util"
7-
import { AddressValidateResult } from "bitcoin-com-rest";
7+
import { AddressValidateResult } from "bitcoin-com-rest"
8+
import axios from "axios"
9+
import * as sinon from "sinon"
810

911
// conts
1012
const bitbox: BITBOX = new BITBOX()
1113
const assert: Chai.AssertStatic = chai.assert
14+
const mockData = require("./mocks/util-mock")
1215

1316
util.inspect.defaultOptions = {
1417
showHidden: true,
@@ -17,6 +20,10 @@ util.inspect.defaultOptions = {
1720
}
1821

1922
describe("#Util", (): void => {
23+
let sandbox: any
24+
beforeEach(() => (sandbox = sinon.sandbox.create()))
25+
afterEach(() => sandbox.restore())
26+
2027
describe("#UtilConstructor", (): void => {
2128
it("should create instance of Util", (): void => {
2229
const util: Util = new Util()
@@ -31,29 +38,32 @@ describe("#Util", (): void => {
3138

3239
describe(`#validateAddress`, (): void => {
3340
it(`should return false for testnet addr on mainnet`, async () => {
34-
const address: string = `bchtest:qqqk4y6lsl5da64sg5qc3xezmplyu5kmpyz2ysaa5y`
35-
36-
const result: AddressValidateResult | AddressValidateResult[] = await bitbox.Util.validateAddress(address)
41+
// Mock the call to rest to prevent live network calls.
42+
const resolved = new Promise(r => r({ data: mockData.invalid }))
43+
sandbox.stub(axios, "get").returns(resolved)
3744

38-
assert.hasAllKeys(result, ["isvalid"])
39-
if (!Array.isArray(result)) {
40-
assert.equal(result.isvalid, false)
41-
}
42-
})
45+
const address: string = `bchtest:qqqk4y6lsl5da64sg5qc3xezmplyu5kmpyz2ysaa5y`
4346

44-
it(`should return false for bad address`, async () => {
45-
const address: string = `bitcoincash:qp4k8fjtgunhdr7yq30ha4peu`
46-
const result: AddressValidateResult | AddressValidateResult[] = await bitbox.Util.validateAddress(address)
47+
const result:
48+
| AddressValidateResult
49+
| AddressValidateResult[] = await bitbox.Util.validateAddress(address)
50+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
4751

4852
assert.hasAllKeys(result, ["isvalid"])
49-
if (!Array.isArray(result)) {
50-
assert.equal(result.isvalid, false)
51-
}
53+
if (!Array.isArray(result)) assert.equal(result.isvalid, false)
5254
})
5355

5456
it(`should validate valid address`, async () => {
57+
// Mock the call to rest to prevent live network calls.
58+
const resolved = new Promise(r => r({ data: mockData.valid }))
59+
sandbox.stub(axios, "get").returns(resolved)
60+
5561
const address: string = `bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`
56-
const result: AddressValidateResult | AddressValidateResult[] = await bitbox.Util.validateAddress(address)
62+
63+
const result:
64+
| AddressValidateResult
65+
| AddressValidateResult[] = await bitbox.Util.validateAddress(address)
66+
//console.log(`result: ${JSON.stringify(result,null,2)}`)
5767

5868
assert.hasAllKeys(result, [
5969
"isvalid",
@@ -63,18 +73,23 @@ describe("#Util", (): void => {
6373
"iswatchonly",
6474
"isscript"
6575
])
66-
if (!Array.isArray(result)) {
67-
assert.equal(result.isvalid, true)
68-
}
76+
if (!Array.isArray(result)) assert.equal(result.isvalid, true)
6977
})
7078

7179
it(`should validate an array of addresses`, async () => {
80+
// Mock the call to rest to prevent live network calls.
81+
const testData = [mockData.valid, mockData.valid]
82+
const resolved = new Promise(r => r({ data: testData }))
83+
sandbox.stub(axios, "post").returns(resolved)
84+
7285
const address: string[] = [
7386
`bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`,
7487
`bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`
7588
]
7689

77-
const result: AddressValidateResult | AddressValidateResult[] = await bitbox.Util.validateAddress(address)
90+
const result:
91+
| AddressValidateResult
92+
| AddressValidateResult[] = await bitbox.Util.validateAddress(address)
7893

7994
assert.isArray(result)
8095
if (Array.isArray(result)) {
@@ -89,32 +104,24 @@ describe("#Util", (): void => {
89104
}
90105
})
91106

92-
it(`should throw an error for improper single input`, async () => {
107+
it(`should pass error from server to user`, async () => {
93108
try {
109+
// Mock out data for unit test, to prevent live network call.
110+
sandbox
111+
.stub(axios, "get")
112+
.throws("error", "Input must be a string or array of strings.")
113+
94114
const address: any = 15432
95115

96116
await bitbox.Util.validateAddress(address)
97117
assert.equal(true, false, "Unexpected result!")
98118
} catch (err) {
119+
//console.log(`err: ${util.inspect(err)}`)
99120
assert.include(
100121
err.message,
101122
`Input must be a string or array of strings.`
102123
)
103124
}
104125
})
105-
106-
it(`should throw error on array size rate limit`, async () => {
107-
try {
108-
const dataMock: string = `bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z`
109-
const data: string[] = []
110-
for (let i: number = 0; i < 25; i++) data.push(dataMock)
111-
112-
const result: AddressValidateResult | AddressValidateResult[] = await bitbox.Util.validateAddress(data)
113-
assert.equal(true, false, "Unexpected result!")
114-
} catch (err) {
115-
assert.hasAnyKeys(err, ["error"])
116-
assert.include(err.error, "Array too large")
117-
}
118-
})
119126
})
120127
})

test/unit/mocks/util-mock.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Mock data used for unit testing.
3+
*/
4+
5+
module.exports = {
6+
invalid: {
7+
isvalid: false
8+
},
9+
10+
valid: {
11+
isvalid: true,
12+
address: "bitcoincash:qp4k8fjtgunhdr7yq30ha4peuwupzan2vcnwrmpy0z",
13+
scriptPubKey: "76a9146b63a64b4727768fc4045f7ed439e3b811766a6688ac",
14+
ismine: false,
15+
iswatchonly: false,
16+
isscript: false
17+
}
18+
}

0 commit comments

Comments
 (0)