1
+ import { LIB_VERSION } from '../../version' ;
1
2
import { checkErrorForInvalidRequestArgs , fetchRPCRequest } from './utils' ;
2
3
import { standardErrors } from ':core/error' ;
3
4
@@ -20,6 +21,9 @@ const invalidParamsError = (args) =>
20
21
data : args ,
21
22
} ) ;
22
23
24
+ const mockUUID = '123e4567-e89b-12d3-a456-426614174000' ;
25
+ jest . spyOn ( crypto , 'randomUUID' ) . mockReturnValue ( mockUUID ) ;
26
+
23
27
describe ( 'Utils' , ( ) => {
24
28
describe ( 'fetchRPCRequest' , ( ) => {
25
29
function mockFetchResponse ( response : unknown ) {
@@ -28,6 +32,33 @@ describe('Utils', () => {
28
32
} ) ;
29
33
}
30
34
35
+ it ( 'should make a POST request with correct parameters' , async ( ) => {
36
+ const mockResponse = { json : jest . fn ( ) . mockResolvedValue ( { result : '0x1' } ) } ;
37
+ mockFetchResponse ( { id : 1 , result : mockResponse , error : null } ) ;
38
+
39
+ const mockRpcUrl = 'https://example.com/rpc' ;
40
+ const mockRequestArguments = {
41
+ method : 'eth_getBalance' ,
42
+ params : [ '0x742d35Cc6634C0532925a3b844Bc454e4438f44e' , 'latest' ] ,
43
+ } ;
44
+ await fetchRPCRequest ( mockRequestArguments , mockRpcUrl ) ;
45
+
46
+ expect ( fetch ) . toHaveBeenCalledWith ( mockRpcUrl , {
47
+ method : 'POST' ,
48
+ body : JSON . stringify ( {
49
+ ...mockRequestArguments ,
50
+ jsonrpc : '2.0' ,
51
+ id : mockUUID ,
52
+ } ) ,
53
+ mode : 'cors' ,
54
+ headers : {
55
+ 'Content-Type' : 'application/json' ,
56
+ 'X-Cbw-Sdk-Version' : LIB_VERSION ,
57
+ 'X-Cbw-Sdk-Platform' : '@mobile-wallet-protocol/client' ,
58
+ } ,
59
+ } ) ;
60
+ } ) ;
61
+
31
62
it ( 'should throw if the response has an error' , async ( ) => {
32
63
mockFetchResponse ( {
33
64
id : 1 ,
0 commit comments