@@ -3,13 +3,20 @@ import * as chai from "chai"
33import { BITBOX } from "../../lib/BITBOX"
44import { Transaction } from "../../lib/Transaction"
55import { 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
911const bitbox : BITBOX = new BITBOX ( )
1012const assert : Chai . AssertStatic = chai . assert
13+ const mockData = require ( "./mocks/transactions-mock" )
1114
1215describe ( "#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} )
0 commit comments