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

Commit 7a51c33

Browse files
author
Alex
authored
add test coverage to web3-core (#7091)
* add test coverage to web3-core * make types compitable * update * update * update types * update types * update * update tests
1 parent 9547643 commit 7a51c33

12 files changed

+611
-31
lines changed

packages/web3-core/src/web3_request_manager.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ export class Web3RequestManager<
429429
) {
430430
return this._buildResponse(payload, response, error);
431431
}
432-
433432
if (jsonRpc.isBatchRequest(payload) && !Array.isArray(response)) {
434433
throw new ResponseError(response, 'Got normal response for a batch request.');
435434
}
@@ -438,15 +437,6 @@ export class Web3RequestManager<
438437
throw new ResponseError(response, 'Got batch response for a normal request.');
439438
}
440439

441-
if (
442-
(jsonRpc.isResponseWithError(response) || jsonRpc.isResponseWithResult(response)) &&
443-
!jsonRpc.isBatchRequest(payload)
444-
) {
445-
if (response.id && payload.id !== response.id) {
446-
throw new InvalidResponseError<ErrorType>(response);
447-
}
448-
}
449-
450440
throw new ResponseError(response, 'Invalid response');
451441
}
452442

packages/web3-core/src/web3_subscriptions.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import {
3030
Web3APISpec,
3131
} from 'web3-types';
3232
import { jsonRpc } from 'web3-utils';
33-
import { SubscriptionError } from 'web3-errors';
3433

3534
// eslint-disable-next-line import/no-cycle
3635
import { Web3SubscriptionManager } from './web3_subscription_manager.js';
@@ -82,16 +81,6 @@ export abstract class Web3Subscription<
8281
super();
8382
const { requestManager } = options as { requestManager: Web3RequestManager<API> };
8483
const { subscriptionManager } = options as { subscriptionManager: Web3SubscriptionManager };
85-
if (requestManager && subscriptionManager) {
86-
throw new SubscriptionError(
87-
'Only requestManager or subscriptionManager should be provided at Subscription constructor',
88-
);
89-
}
90-
if (!requestManager && !subscriptionManager) {
91-
throw new SubscriptionError(
92-
'Either requestManager or subscriptionManager should be provided at Subscription constructor',
93-
);
94-
}
9584
if (requestManager) {
9685
// eslint-disable-next-line deprecation/deprecation
9786
this._subscriptionManager = new Web3SubscriptionManager(requestManager, {}, true);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
import {
19+
BaseTransaction,
20+
TxValuesArray,
21+
AccessListEIP2930ValuesArray,
22+
FeeMarketEIP1559ValuesArray,
23+
JsonTx,
24+
} from 'web3-eth-accounts';
25+
26+
export class CustomTransactionType extends BaseTransaction<unknown> {
27+
// eslint-disable-next-line class-methods-use-this
28+
public getUpfrontCost(): bigint {
29+
throw new Error('Method not implemented.');
30+
}
31+
// eslint-disable-next-line class-methods-use-this
32+
public raw(): TxValuesArray | AccessListEIP2930ValuesArray | FeeMarketEIP1559ValuesArray {
33+
throw new Error('Method not implemented.');
34+
}
35+
// eslint-disable-next-line class-methods-use-this
36+
public serialize(): Uint8Array {
37+
throw new Error('Method not implemented.');
38+
}
39+
public getMessageToSign(hashMessage: false): Uint8Array | Uint8Array[];
40+
public getMessageToSign(hashMessage?: true | undefined): Uint8Array;
41+
// eslint-disable-next-line class-methods-use-this
42+
public getMessageToSign(): Uint8Array | Uint8Array[] {
43+
throw new Error('Method not implemented.');
44+
}
45+
// eslint-disable-next-line class-methods-use-this
46+
public hash(): Uint8Array {
47+
throw new Error('Method not implemented.');
48+
}
49+
// eslint-disable-next-line class-methods-use-this
50+
public getMessageToVerifySignature(): Uint8Array {
51+
throw new Error('Method not implemented.');
52+
}
53+
// eslint-disable-next-line class-methods-use-this
54+
public getSenderPublicKey(): Uint8Array {
55+
throw new Error('Method not implemented.');
56+
}
57+
// eslint-disable-next-line class-methods-use-this
58+
public toJSON(): JsonTx {
59+
throw new Error('Method not implemented.');
60+
}
61+
// eslint-disable-next-line class-methods-use-this
62+
protected _processSignature(): unknown {
63+
throw new Error('Method not implemented.');
64+
}
65+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility, class-methods-use-this
66+
public errorStr(): string {
67+
throw new Error('Method not implemented.');
68+
}
69+
// eslint-disable-next-line class-methods-use-this
70+
protected _errorMsg(): string {
71+
throw new Error('Method not implemented.');
72+
}
73+
}

packages/web3-core/test/unit/fixtures/example_subscription.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GNU Lesser General Public License for more details.
1414
You should have received a copy of the GNU Lesser General Public License
1515
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
17-
17+
import { DataFormat } from 'web3-types';
1818
import { Web3Subscription } from '../../../src';
1919

2020
export class ExampleSubscription extends Web3Subscription<
@@ -26,4 +26,7 @@ export class ExampleSubscription extends Web3Subscription<
2626
protected _buildSubscriptionParams() {
2727
return ['newHeads'];
2828
}
29+
public getReturnFormat(): DataFormat {
30+
return this.returnFormat;
31+
}
2932
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
This file is part of web3.js.
3+
4+
web3.js is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Lesser General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
web3.js is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public License
15+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
import { Web3Subscription } from '../../../src';
18+
19+
// subscription class that exposes buildSubscriptionParams
20+
export class BuildSubscription extends Web3Subscription<
21+
{ data: string },
22+
{ param1: string },
23+
{ eth_subscribe: (newHeads: string) => void }
24+
> {
25+
public buildSubscriptionParams() {
26+
this._buildSubscriptionParams();
27+
}
28+
}

packages/web3-core/test/unit/formatters.test.ts

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

1818
import * as utils from 'web3-utils';
19-
import { BlockTags } from 'web3-types';
19+
import { BlockTags, TransactionInput, Filter } from 'web3-types';
2020
import { Iban } from 'web3-eth-iban';
21+
import { FormatterError } from 'web3-errors';
2122
import {
2223
inputAddressFormatter,
2324
inputBlockNumberFormatter,
@@ -268,6 +269,121 @@ describe('formatters', () => {
268269
);
269270
});
270271

272+
describe('inputCallFormatter', () => {
273+
let txInput: any;
274+
275+
beforeEach(() => {
276+
jest.spyOn(utils, 'isAddress').mockReturnValue(true);
277+
txInput = {
278+
to: '0xabcd',
279+
};
280+
});
281+
282+
it('should format "to" address if provided', () => {
283+
expect(formatters.inputCallFormatter({ ...txInput, to: '0xABCD' })).toEqual(
284+
expect.objectContaining({ to: '0xabcd' }),
285+
);
286+
});
287+
288+
it('should format "from" if defaultAddress is provided', () => {
289+
expect(formatters.inputCallFormatter({ ...txInput, to: '0xABCD' }, '0xABCDE')).toEqual(
290+
expect.objectContaining({ from: '0xabcde', to: '0xabcd' }),
291+
);
292+
});
293+
});
294+
295+
describe('inputTransactionFormatter', () => {
296+
let txInput: any;
297+
298+
beforeEach(() => {
299+
jest.spyOn(utils, 'isAddress').mockReturnValue(true);
300+
txInput = {
301+
to: '0xabcd',
302+
};
303+
});
304+
it('should format and populate "from"', () => {
305+
expect(
306+
formatters.inputTransactionFormatter({ ...txInput, to: '0xabcd', from: '0xABCDE' }),
307+
).toEqual(expect.objectContaining({ from: '0xabcde', to: '0xabcd' }));
308+
});
309+
310+
it('should throw an error when from is undefined', () => {
311+
expect(() => formatters.inputTransactionFormatter({ ...txInput })).toThrow(
312+
new FormatterError('The send transactions "from" field must be defined!'),
313+
);
314+
});
315+
});
316+
317+
describe('outputTransactionFormatter', () => {
318+
it('should correctly format blockNumber from hex to number', () => {
319+
const txInput: TransactionInput = {
320+
to: '0x1234567890abcdef',
321+
from: '0xabcdef1234567890',
322+
gas: '0x123456',
323+
gasPrice: '0x987654321',
324+
nonce: '0x1',
325+
value: '0x9876543210',
326+
blockNumber: '0x123',
327+
transactionIndex: '0x1',
328+
maxFeePerGas: '0x87654321',
329+
maxPriorityFeePerGas: '0x7654321',
330+
type: '0x1',
331+
};
332+
333+
const formattedTxOutput = formatters.outputTransactionFormatter(txInput);
334+
// should return the mocked values;
335+
expect(formattedTxOutput.blockNumber).toBe(123);
336+
expect(formattedTxOutput.to).toBe('toChecksumAddress');
337+
expect(formattedTxOutput.from).toBe('toChecksumAddress');
338+
expect(formattedTxOutput.gas).toBe(123);
339+
expect(formattedTxOutput.nonce).toBe(123);
340+
expect(formattedTxOutput.transactionIndex).toBe(123);
341+
expect(formattedTxOutput.value).toBe(12345);
342+
expect(formattedTxOutput.maxFeePerGas).toBe(12345);
343+
expect(formattedTxOutput.maxPriorityFeePerGas).toBe(12345);
344+
expect(formattedTxOutput.type).toBe(123);
345+
});
346+
347+
it('should make "to" property undefined', () => {
348+
const txInput = { gas: '0x', nonce: '1', value: '0x' };
349+
const formattedTxOutput = formatters.outputTransactionFormatter(txInput);
350+
351+
expect(formattedTxOutput.to).toBeUndefined();
352+
});
353+
});
354+
355+
describe('inputLogFormatter', () => {
356+
beforeAll(() => {
357+
const actualUtils = jest.requireActual('web3-utils');
358+
jest.spyOn(utils, 'mergeDeep').mockImplementation(actualUtils.mergeDeep);
359+
});
360+
it('should correctly format a filter with all fields provided', () => {
361+
const filter: Filter = {
362+
fromBlock: '0x1',
363+
toBlock: '0x2',
364+
address: '0x1234567890abcdef1234567890abcdef12345678',
365+
topics: ['0x123', ['0x456', '0x789']],
366+
};
367+
368+
const formattedFilter = formatters.inputLogFormatter(filter);
369+
370+
expect(formattedFilter.fromBlock).toBe('0x1');
371+
expect(formattedFilter.toBlock).toBe('0x2');
372+
expect(formattedFilter.address).toBe('0x1234567890abcdef1234567890abcdef12345678');
373+
expect(formattedFilter.topics).toEqual(['0x123', ['0x456', '0x789']]);
374+
});
375+
it('should correctly format a filter with no fromBlock', () => {
376+
const filter: Filter = {
377+
address: ['0x123', '0x222'],
378+
};
379+
380+
const formattedFilter = formatters.inputLogFormatter(filter);
381+
382+
expect(formattedFilter.fromBlock).toBe('latest');
383+
expect(formattedFilter.address).toEqual(['0x123', '0x222']);
384+
});
385+
});
386+
271387
describe('outputLogFormatter', () => {
272388
it('should set log id from "blockHash", "transactionHash" and "logIndex"', () => {
273389
const result = outputLogFormatter({

0 commit comments

Comments
 (0)