@@ -4,11 +4,14 @@ import { BITBOX } from "../../lib/BITBOX"
44import { Util } from "../../lib/Util"
55import { resturl } from "../../lib/BITBOX"
66import * 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
1012const bitbox : BITBOX = new BITBOX ( )
1113const assert : Chai . AssertStatic = chai . assert
14+ const mockData = require ( "./mocks/util-mock" )
1215
1316util . inspect . defaultOptions = {
1417 showHidden : true ,
@@ -17,6 +20,10 @@ util.inspect.defaultOptions = {
1720}
1821
1922describe ( "#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} )
0 commit comments