Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit d60c285

Browse files
Fix plugin example tests with 4.0.1-rc.1 (#6134)
* fix plugin example tests and tests titles * modify unit test for the plugin example
1 parent 88ac791 commit d60c285

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ declare module '../web3_export_helper' {
2626
}
2727
}
2828

29-
describe('CustomRpcMethodsPlugin Tests', () => {
30-
it('should register CustomRpcMethodsPlugin plugin', () => {
29+
describe('ContractMethodWrappersPlugin', () => {
30+
it('should register the plugin', () => {
3131
const web3 = new Web3('http://127.0.0.1:8545');
3232
web3.registerPlugin(
3333
new ContractMethodWrappersPlugin(
@@ -38,7 +38,7 @@ describe('CustomRpcMethodsPlugin Tests', () => {
3838
expect(web3.contractMethodWrappersPlugin).toBeDefined();
3939
});
4040

41-
describe('CustomRpcMethodsPlugin methods tests', () => {
41+
describe('methods', () => {
4242
const contractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7';
4343
const sender = '0x8da5e39ec14b57fb9bcd9aa2b4500e909119795d';
4444
const recipient = '0x4f641def1e7845caab95ac717c80416082430d0d';
@@ -47,17 +47,21 @@ describe('CustomRpcMethodsPlugin Tests', () => {
4747
'0x0000000000000000000000000000000000000000000000000000000000000280';
4848
const expectedRecipientBalance =
4949
'0x0000000000000000000000000000000000000000000000000000000000000120';
50-
const requestManagerSendSpy = jest.fn();
50+
let requestManagerSendSpy: jest.Mock;
5151

5252
let web3: Web3;
5353

5454
beforeAll(() => {
5555
web3 = new Web3('http://127.0.0.1:8545');
5656
web3.registerPlugin(new ContractMethodWrappersPlugin(ERC20TokenAbi, contractAddress));
57+
});
58+
59+
beforeEach(() => {
60+
requestManagerSendSpy = jest.fn();
5761
web3.contractMethodWrappersPlugin._contract.requestManager.send = requestManagerSendSpy;
5862
});
5963

60-
it('should call contractMethodWrappersPlugin.getFormattedBalance with expected RPC object', async () => {
64+
it('should call `getFormattedBalance` with expected RPC object', async () => {
6165
requestManagerSendSpy.mockResolvedValueOnce(expectedSenderBalance);
6266

6367
await web3.contractMethodWrappersPlugin.getFormattedBalance(
@@ -67,17 +71,16 @@ describe('CustomRpcMethodsPlugin Tests', () => {
6771
expect(requestManagerSendSpy).toHaveBeenCalledWith({
6872
method: 'eth_call',
6973
params: [
70-
{
74+
expect.objectContaining({
7175
input: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
72-
data: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
7376
to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
74-
},
77+
}),
7578
'latest',
7679
],
7780
});
7881
});
7982

80-
it('should call CustomRpcMethodsPlugin.customRpcMethodWithParameters with expected RPC object', async () => {
83+
it('should call `transferAndGetBalances` with expected RPC object', async () => {
8184
const expectedGasPrice = '0x1ca14bd70';
8285
const expectedTransactionHash =
8386
'0xc41b9a4f654c44552e135f770945916f57c069b80326f9a5f843e613491ab6b1';
@@ -98,20 +101,22 @@ describe('CustomRpcMethodsPlugin Tests', () => {
98101
recipient,
99102
amount,
100103
);
101-
expect(requestManagerSendSpy).toHaveBeenCalledWith({
104+
105+
// The first call will be to `eth_gasPrice` and the second is to `eth_blockNumber`. And the third one will be to `eth_sendTransaction`:
106+
expect(requestManagerSendSpy).toHaveBeenNthCalledWith(3, {
102107
method: 'eth_sendTransaction',
103108
params: [
104-
{
109+
expect.objectContaining({
105110
input: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
106-
data: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
107111
from: sender,
108112
gasPrice: expectedGasPrice,
109113
maxFeePerGas: undefined,
110114
maxPriorityFeePerGas: undefined,
111115
to: contractAddress,
112-
},
116+
}),
113117
],
114118
});
119+
115120
expect(balances).toStrictEqual({
116121
sender: {
117122
address: sender,

tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import { Web3Context } from 'web3-core';
1818

1919
import { CustomRpcMethodsPlugin } from '../../src/custom_rpc_methods';
2020

21-
describe('CustomRpcMethodsPlugin Tests', () => {
22-
it('should register CustomRpcMethodsPlugin plugin', () => {
21+
describe('CustomRpcMethodsPlugin', () => {
22+
it('should register the plugin', () => {
2323
const web3Context = new Web3Context('http://127.0.0.1:8545');
2424
web3Context.registerPlugin(new CustomRpcMethodsPlugin());
2525
expect(web3Context.customRpcMethods).toBeDefined();
2626
});
2727

28-
describe('CustomRpcMethodsPlugin methods tests', () => {
28+
describe('methods', () => {
2929
const requestManagerSendSpy = jest.fn();
3030

3131
let web3Context: Web3Context;
@@ -36,15 +36,15 @@ describe('CustomRpcMethodsPlugin Tests', () => {
3636
web3Context.requestManager.send = requestManagerSendSpy;
3737
});
3838

39-
it('should call CustomRpcMethodsPlugin.customRpcMethod with expected RPC object', async () => {
39+
it('should call `customRpcMethod` with expected RPC object', async () => {
4040
await web3Context.customRpcMethods.customRpcMethod();
4141
expect(requestManagerSendSpy).toHaveBeenCalledWith({
4242
method: 'custom_rpc_method',
4343
params: [],
4444
});
4545
});
4646

47-
it('should call CustomRpcMethodsPlugin.customRpcMethodWithParameters with expected RPC object', async () => {
47+
it('should call `customRpcMethodWithParameters` with expected RPC object', async () => {
4848
const parameter1 = 'myString';
4949
const parameter2 = 42;
5050
await web3Context.customRpcMethods.customRpcMethodWithParameters(

0 commit comments

Comments
 (0)