From 6c16beafa2760b866e30ee3bf0a54e33d25eff9d Mon Sep 17 00:00:00 2001 From: Oleksii Kosynskyi Date: Thu, 12 Oct 2023 12:08:40 -0400 Subject: [PATCH] add unit tests --- .../src/tx/transactionFactory.ts | 2 +- .../test/unit/tx/registerNewTx.test.ts | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 packages/web3-eth-accounts/test/unit/tx/registerNewTx.test.ts diff --git a/packages/web3-eth-accounts/src/tx/transactionFactory.ts b/packages/web3-eth-accounts/src/tx/transactionFactory.ts index 718d7152850..e4da9fcfe6e 100644 --- a/packages/web3-eth-accounts/src/tx/transactionFactory.ts +++ b/packages/web3-eth-accounts/src/tx/transactionFactory.ts @@ -108,7 +108,7 @@ export class TransactionFactory { case 2: return FeeMarketEIP1559Transaction.fromSerializedTx(data, txOptions); default: { - const ExtraTransaction = extraTxTypes.get(data[0]); + const ExtraTransaction = extraTxTypes.get(Number(data[0])); if (ExtraTransaction?.fromSerializedTx) { return ExtraTransaction.fromSerializedTx( data, diff --git a/packages/web3-eth-accounts/test/unit/tx/registerNewTx.test.ts b/packages/web3-eth-accounts/test/unit/tx/registerNewTx.test.ts new file mode 100644 index 00000000000..f694729af00 --- /dev/null +++ b/packages/web3-eth-accounts/test/unit/tx/registerNewTx.test.ts @@ -0,0 +1,51 @@ +/* +This file is part of web3.js. + +web3.js is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +web3.js is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with web3.js. If not, see . +*/ + +import { TransactionFactory } from '../../../src/tx/transactionFactory'; +import { BaseTransaction } from '../../../src/tx/baseTransaction'; +import { TxData, TxOptions } from '../../../src/tx'; + +describe('Register new TX', () => { + it('validateCannotExceedMaxInteger()', () => { + const TYPE = 20; + // @ts-ignore + class SomeNewTxType extends BaseTransaction { + constructor(txData: TxData, opts: TxOptions = {}) { + super(txData, opts); + // @ts-ignore + this._type = 20; + } + public static fromTxData() { + return 'new fromTxData'; + } + public static fromSerializedTx() { + return 'new fromSerializedData'; + } + } + TransactionFactory.registerTransactionType(TYPE, SomeNewTxType); + const txData = { + from: '0x', + to: '0x', + value: '0x1', + type: TYPE, + }; + expect(TransactionFactory.fromTxData(txData)).toBe('new fromTxData'); + expect(TransactionFactory.fromSerializedData(new Uint8Array([TYPE, 10]))).toBe( + 'new fromSerializedData', + ); + }); +});