Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix plugin example tests with 4.0.1-rc.1 #6134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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';
Expand All @@ -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(
Expand All @@ -67,17 +71,16 @@ describe('CustomRpcMethodsPlugin Tests', () => {
expect(requestManagerSendSpy).toHaveBeenCalledWith({
method: 'eth_call',
params: [
{
expect.objectContaining({
input: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
data: '0x70a082310000000000000000000000008da5e39ec14b57fb9bcd9aa2b4500e909119795d',
to: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
},
}),
'latest',
],
});
});

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';
Expand All @@ -98,20 +101,22 @@ 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: [
{
expect.objectContaining({
input: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
data: '0xa9059cbb0000000000000000000000004f641def1e7845caab95ac717c80416082430d0d000000000000000000000000000000000000000000000000000000000000002a',
from: sender,
gasPrice: expectedGasPrice,
maxFeePerGas: undefined,
maxPriorityFeePerGas: undefined,
to: contractAddress,
},
}),
],
});

expect(balances).toStrictEqual({
sender: {
address: sender,
Expand Down
10 changes: 5 additions & 5 deletions tools/web3-plugin-example/test/unit/custom_rpc_methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,15 +36,15 @@ 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',
params: [],
});
});

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(
Expand Down