Skip to content

Commit ab8331e

Browse files
authored
lib name (#9)
1 parent dde053d commit ab8331e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

packages/client/src/core/util/utils.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LIB_VERSION } from '../../version';
12
import { checkErrorForInvalidRequestArgs, fetchRPCRequest } from './utils';
23
import { standardErrors } from ':core/error';
34

@@ -20,6 +21,9 @@ const invalidParamsError = (args) =>
2021
data: args,
2122
});
2223

24+
const mockUUID = '123e4567-e89b-12d3-a456-426614174000';
25+
jest.spyOn(crypto, 'randomUUID').mockReturnValue(mockUUID);
26+
2327
describe('Utils', () => {
2428
describe('fetchRPCRequest', () => {
2529
function mockFetchResponse(response: unknown) {
@@ -28,6 +32,33 @@ describe('Utils', () => {
2832
});
2933
}
3034

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+
3162
it('should throw if the response has an error', async () => {
3263
mockFetchResponse({
3364
id: 1,

packages/client/src/core/util/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ export async function fetchRPCRequest(request: RequestArguments, rpcUrl: string)
1313
method: 'POST',
1414
body: JSON.stringify(requestBody),
1515
mode: 'cors',
16-
headers: { 'Content-Type': 'application/json', 'X-Cbw-Sdk-Version': LIB_VERSION },
16+
headers: {
17+
'Content-Type': 'application/json',
18+
'X-Cbw-Sdk-Version': LIB_VERSION,
19+
'X-Cbw-Sdk-Platform': '@mobile-wallet-protocol/client',
20+
},
1721
});
1822
const { result, error } = await res.json();
1923
if (error) throw error;

0 commit comments

Comments
 (0)