1- import { harmony } from './harmony' ;
1+ import fetch from 'jest-fetch-mock' ;
2+ import { harmony , checkCalledMethod } from './harmony' ;
23// tslint:disable-next-line: no-implicit-dependencies
34import { Transaction , TxStatus } from '@harmony-js/transaction' ;
45// tslint:disable-next-line: no-implicit-dependencies
56import { isHash , numberToHex } from '@harmony-js/utils' ;
67import txnJsons from '../fixtures/transactions.json' ;
78import demoAccounts from '../fixtures/testAccount.json' ;
9+ import { RPCMethod } from '@harmony-js/network' ;
810
911const receiver = demoAccounts . Accounts [ 3 ] ;
1012jest . useRealTimers ( ) ;
1113
1214describe ( 'test Transaction using SDK' , ( ) => {
15+ beforeEach ( ( ) => {
16+ fetch . resetMocks ( ) ;
17+ } ) ;
1318 let signed : Transaction ;
1419 let sent : Transaction ;
1520 let txId : string ;
@@ -38,18 +43,56 @@ describe('test Transaction using SDK', () => {
3843 } ;
3944
4045 const txn = harmony . transactions . newTx ( txnObject ) ;
41- signed = await harmony . wallet . signTransaction ( txn ) ;
46+ signed = await harmony . wallet . signTransaction ( txn , undefined , undefined , false ) ;
4247
4348 expect ( signed . isSigned ( ) ) . toEqual ( true ) ;
4449 } ) ;
4550 it ( 'should send transaction' , async ( ) => {
51+ fetch . mockResponseOnce (
52+ JSON . stringify ( {
53+ "jsonrpc" : "2.0" , "id" : 1 ,
54+ "result" : "0x323A2B2C81d8948E5109FC32f9d0e4e6d178d14cC732C8E0a7Af74E81C7653eA"
55+ } ) ,
56+ ) ;
4657 const [ sentTxn , id ] = await signed . sendTransaction ( ) ;
4758 expect ( sentTxn . isPending ( ) ) . toEqual ( true ) ;
4859 expect ( harmony . utils . isHash ( id ) ) . toEqual ( true ) ;
60+ expect ( checkCalledMethod ( 0 , RPCMethod . SendRawTransaction ) ) . toEqual ( true ) ;
4961 txId = id ;
5062 sent = sentTxn ;
5163 } ) ;
5264 it ( 'should confirm a transaction' , async ( ) => {
65+ fetch . mockResponses (
66+ [
67+ JSON . stringify ( { "jsonrpc" : "2.0" , "id" : 1 , "result" : "0x1" } ) ,
68+ { status : 200 } ,
69+ ] ,
70+ [
71+ JSON . stringify ( { "jsonrpc" : "2.0" , "id" : 1 , "result" : "0x2" } ) ,
72+ { status : 200 } ,
73+ ] ,
74+ [
75+ JSON . stringify ( {
76+ "jsonrpc" : "2.0" ,
77+ "id" : 1 ,
78+ "result" : {
79+ "contractAddress" : null ,
80+ "blockNumber" : harmony . utils . numberToHex ( 2 ) ,
81+ "from" : harmony . wallet . accounts [ 0 ] ,
82+ "gasUsed" : harmony . utils . numberToHex ( 5 ) ,
83+ "cumulativeGasUsed" : harmony . utils . numberToHex ( 5 ) ,
84+ "logs" : [ ] ,
85+ "logsBloom" : harmony . utils . numberToHex ( 5 ) ,
86+ "shardID" : 0 ,
87+ "to" : demoAccounts . Accounts [ 3 ] . Address ,
88+ "transactionHash" : "0x8c26EFdb6e4cAC6F8BeACE59F52fd95beD4Bfbfa8fF30F4a7cEd511fE5f869d9" ,
89+ "transactionIndex" : harmony . utils . numberToHex ( 10 ) ,
90+ "blockHash" : "0xFECCCCBFd5AC71902BcfE65dDB0b88EEbbD15AD6cDAE7A9FAEb773bF827320fd" ,
91+ }
92+ } ) ,
93+ { status : 200 } ,
94+ ]
95+ )
5396 const toConfirm = await sent . confirm ( txId , 20 , 1000 ) ;
5497 expect ( toConfirm . receipt !== undefined ) . toEqual ( true ) ;
5598 expect ( checkTransactionReceipt ( toConfirm . receipt ) ) . toEqual ( true ) ;
@@ -58,6 +101,9 @@ describe('test Transaction using SDK', () => {
58101 } else if ( toConfirm . isRejected ( ) ) {
59102 expect ( toConfirm . txStatus ) . toEqual ( TxStatus . REJECTED ) ;
60103 }
104+ expect ( checkCalledMethod ( 0 , RPCMethod . BlockNumber ) ) . toEqual ( true ) ;
105+ expect ( checkCalledMethod ( 1 , RPCMethod . BlockNumber ) ) . toEqual ( true ) ;
106+ expect ( checkCalledMethod ( 2 , RPCMethod . GetTransactionReceipt ) ) . toEqual ( true ) ;
61107 } ) ;
62108 it ( 'should test transaction observed events' , async ( ) => {
63109 const txnObject = {
@@ -84,10 +130,53 @@ describe('test Transaction using SDK', () => {
84130 . on ( 'error' , ( error ) => {
85131 expect ( error ) . toBeTruthy ( ) ;
86132 } ) ;
87- const txnSigned = await harmony . wallet . signTransaction ( txn ) ;
133+ const txnSigned = await harmony . wallet . signTransaction ( txn , undefined , undefined , false ) ;
134+ fetch . mockResponseOnce (
135+ JSON . stringify ( {
136+ "jsonrpc" : "2.0" , "id" : 1 ,
137+ "result" : "0x323A2B2C81d8948E5109FC32f9d0e4e6d178d14cC732C8E0a7Af74E81C7653eA"
138+ } ) ,
139+ ) ;
88140 const [ txnSent , id ] = await txnSigned . sendTransaction ( ) ;
89141 expect ( txnSent . txStatus ) . toEqual ( TxStatus . PENDING ) ;
142+ expect ( checkCalledMethod ( 0 , RPCMethod . SendRawTransaction ) ) . toEqual ( true ) ;
143+ console . log ( 'Here' ) ;
144+ fetch . mockResponses (
145+ [
146+ JSON . stringify ( { "jsonrpc" : "2.0" , "id" : 1 , "result" : "0x1" } ) ,
147+ { status : 200 } ,
148+ ] ,
149+ [
150+ JSON . stringify ( { "jsonrpc" : "2.0" , "id" : 1 , "result" : "0x2" } ) ,
151+ { status : 200 } ,
152+ ] ,
153+ [
154+ JSON . stringify ( {
155+ "jsonrpc" : "2.0" ,
156+ "id" : 1 ,
157+ "result" : {
158+ "contractAddress" : null ,
159+ "blockNumber" : harmony . utils . numberToHex ( 2 ) ,
160+ "from" : harmony . wallet . accounts [ 0 ] ,
161+ "gasUsed" : harmony . utils . numberToHex ( 5 ) ,
162+ "cumulativeGasUsed" : harmony . utils . numberToHex ( 5 ) ,
163+ "logs" : [ ] ,
164+ "logsBloom" : harmony . utils . numberToHex ( 5 ) ,
165+ "shardID" : 0 ,
166+ "to" : demoAccounts . Accounts [ 3 ] . Address ,
167+ "transactionHash" : "0x8c26EFdb6e4cAC6F8BeACE59F52fd95beD4Bfbfa8fF30F4a7cEd511fE5f869d9" ,
168+ "transactionIndex" : harmony . utils . numberToHex ( 10 ) ,
169+ "blockHash" : "0xFECCCCBFd5AC71902BcfE65dDB0b88EEbbD15AD6cDAE7A9FAEb773bF827320fd" ,
170+ "status" : "0x1" ,
171+ }
172+ } ) ,
173+ { status : 200 } ,
174+ ]
175+ ) ;
90176 await txnSigned . confirm ( id ) ;
177+ expect ( checkCalledMethod ( 1 , RPCMethod . BlockNumber ) ) . toEqual ( true ) ;
178+ expect ( checkCalledMethod ( 2 , RPCMethod . BlockNumber ) ) . toEqual ( true ) ;
179+ expect ( checkCalledMethod ( 3 , RPCMethod . GetTransactionReceipt ) ) . toEqual ( true ) ;
91180 } ) ;
92181} ) ;
93182
0 commit comments