From 94200780e277d0e042c7bb276de154a1301402cc Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Wed, 31 May 2023 17:35:30 +0200 Subject: [PATCH 1/2] fix plugin example tests and tests titles --- .../unit/contract_method_wrappers.test.ts | 23 +++++++++++-------- .../test/unit/custom_rpc_methods.test.ts | 10 ++++---- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts b/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts index 0351a695e53..9a45fdf2b86 100644 --- a/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts +++ b/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts @@ -26,8 +26,8 @@ declare module '../web3_export_helper' { } } -describe('CustomRpcMethodsPlugin Tests', () => { - it('should register CustomRpcMethodsPlugin plugin', () => { +describe('ContractMethodWrappersPlugin', () => { + it('should register the plugin', () => { const web3 = new Web3('http://127.0.0.1:8545'); web3.registerPlugin( new ContractMethodWrappersPlugin( @@ -38,7 +38,7 @@ describe('CustomRpcMethodsPlugin Tests', () => { expect(web3.contractMethodWrappersPlugin).toBeDefined(); }); - describe('CustomRpcMethodsPlugin methods tests', () => { + describe('methods', () => { const contractAddress = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; const sender = '0x8da5e39ec14b57fb9bcd9aa2b4500e909119795d'; const recipient = '0x4f641def1e7845caab95ac717c80416082430d0d'; @@ -47,17 +47,21 @@ describe('CustomRpcMethodsPlugin Tests', () => { '0x0000000000000000000000000000000000000000000000000000000000000280'; const expectedRecipientBalance = '0x0000000000000000000000000000000000000000000000000000000000000120'; - const requestManagerSendSpy = jest.fn(); + let requestManagerSendSpy: jest.Mock; let web3: Web3; beforeAll(() => { web3 = new Web3('http://127.0.0.1:8545'); web3.registerPlugin(new ContractMethodWrappersPlugin(ERC20TokenAbi, contractAddress)); + }); + + beforeEach(() => { + requestManagerSendSpy = jest.fn(); web3.contractMethodWrappersPlugin._contract.requestManager.send = requestManagerSendSpy; }); - it('should call contractMethodWrappersPlugin.getFormattedBalance with expected RPC object', async () => { + it('should call `getFormattedBalance` with expected RPC object', async () => { requestManagerSendSpy.mockResolvedValueOnce(expectedSenderBalance); await web3.contractMethodWrappersPlugin.getFormattedBalance( @@ -69,7 +73,6 @@ describe('CustomRpcMethodsPlugin Tests', () => { params: [ { input: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d', - data: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d', to: '0xdAC17F958D2ee523a2206206994597C13D831ec7', }, 'latest', @@ -77,7 +80,7 @@ describe('CustomRpcMethodsPlugin Tests', () => { }); }); - it('should call CustomRpcMethodsPlugin.customRpcMethodWithParameters with expected RPC object', async () => { + it('should call `transferAndGetBalances` with expected RPC object', async () => { const expectedGasPrice = '0x1ca14bd70'; const expectedTransactionHash = '0xc41b9a4f654c44552e135f770945916f57c069b80326f9a5f843e613491ab6b1'; @@ -98,12 +101,13 @@ describe('CustomRpcMethodsPlugin Tests', () => { recipient, amount, ); - expect(requestManagerSendSpy).toHaveBeenCalledWith({ + + // The first call will be to `eth_gasPrice` and the second is to `eth_blockNumber`. And the third one will be to `eth_sendTransaction`: + expect(requestManagerSendSpy).toHaveBeenNthCalledWith(3, { method: 'eth_sendTransaction', params: [ { input: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a', - data: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a', from: sender, gasPrice: expectedGasPrice, maxFeePerGas: undefined, @@ -112,6 +116,7 @@ describe('CustomRpcMethodsPlugin Tests', () => { }, ], }); + expect(balances).toStrictEqual({ sender: { address: sender, diff --git a/tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts b/tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts index 3103d26ada6..b65a23dad70 100644 --- a/tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts +++ b/tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts @@ -18,14 +18,14 @@ import { Web3Context } from 'web3-core'; import { CustomRpcMethodsPlugin } from '../../src/custom_rpc_methods'; -describe('CustomRpcMethodsPlugin Tests', () => { - it('should register CustomRpcMethodsPlugin plugin', () => { +describe('CustomRpcMethodsPlugin', () => { + it('should register the plugin', () => { const web3Context = new Web3Context('http://127.0.0.1:8545'); web3Context.registerPlugin(new CustomRpcMethodsPlugin()); expect(web3Context.customRpcMethods).toBeDefined(); }); - describe('CustomRpcMethodsPlugin methods tests', () => { + describe('methods', () => { const requestManagerSendSpy = jest.fn(); let web3Context: Web3Context; @@ -36,7 +36,7 @@ describe('CustomRpcMethodsPlugin Tests', () => { web3Context.requestManager.send = requestManagerSendSpy; }); - it('should call CustomRpcMethodsPlugin.customRpcMethod with expected RPC object', async () => { + it('should call `customRpcMethod` with expected RPC object', async () => { await web3Context.customRpcMethods.customRpcMethod(); expect(requestManagerSendSpy).toHaveBeenCalledWith({ method: 'custom_rpc_method', @@ -44,7 +44,7 @@ describe('CustomRpcMethodsPlugin Tests', () => { }); }); - it('should call CustomRpcMethodsPlugin.customRpcMethodWithParameters with expected RPC object', async () => { + it('should call `customRpcMethodWithParameters` with expected RPC object', async () => { const parameter1 = 'myString'; const parameter2 = 42; await web3Context.customRpcMethods.customRpcMethodWithParameters( From 7dca63dec1577ecb5e96da0b64fdbffe747e3b77 Mon Sep 17 00:00:00 2001 From: Muhammad-Altabba <24407834+Muhammad-Altabba@users.noreply.github.com> Date: Wed, 31 May 2023 23:52:09 +0200 Subject: [PATCH 2/2] modify unit test for the plugin example --- .../test/unit/contract_method_wrappers.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts b/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts index 9a45fdf2b86..62d8e6f7666 100644 --- a/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts +++ b/tools/web3-plugin-example/test/unit/contract_method_wrappers.test.ts @@ -71,10 +71,10 @@ describe('ContractMethodWrappersPlugin', () => { expect(requestManagerSendSpy).toHaveBeenCalledWith({ method: 'eth_call', params: [ - { + expect.objectContaining({ input: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d', to: '0xdAC17F958D2ee523a2206206994597C13D831ec7', - }, + }), 'latest', ], }); @@ -106,14 +106,14 @@ describe('ContractMethodWrappersPlugin', () => { expect(requestManagerSendSpy).toHaveBeenNthCalledWith(3, { method: 'eth_sendTransaction', params: [ - { + expect.objectContaining({ input: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a', from: sender, gasPrice: expectedGasPrice, maxFeePerGas: undefined, maxPriorityFeePerGas: undefined, to: contractAddress, - }, + }), ], });