-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ck] consistent send tx object in kit (#1191)
- Loading branch information
Showing
5 changed files
with
167 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { TransactionObject, Tx } from 'web3/eth/types' | ||
import PromiEvent from 'web3/promiEvent' | ||
import { TransactionReceipt } from 'web3/types' | ||
import { newKit } from './kit' | ||
import { promiEventSpy } from './test-utils/PromiEventStub' | ||
|
||
interface TransactionObjectStub<T> extends TransactionObject<T> { | ||
sendMock: jest.Mock<PromiEvent<any>, [Tx | undefined]> | ||
estimateGasMock: jest.Mock<Promise<number>, []> | ||
resolveHash(hash: string): void | ||
resolveReceipt(receipt: TransactionReceipt): void | ||
rejectHash(error: any): void | ||
rejectReceipt(receipt: TransactionReceipt, error: any): void | ||
} | ||
|
||
export function txoStub<T>(): TransactionObjectStub<T> { | ||
const estimateGasMock = jest.fn() | ||
const peStub = promiEventSpy() | ||
const sendMock = jest.fn().mockReturnValue(peStub) | ||
|
||
const pe: TransactionObjectStub<T> = { | ||
arguments: [], | ||
call: () => { | ||
throw new Error('not implemented') | ||
}, | ||
encodeABI: () => { | ||
throw new Error('not implemented') | ||
}, | ||
estimateGas: estimateGasMock, | ||
send: sendMock, | ||
sendMock, | ||
estimateGasMock, | ||
resolveHash: peStub.resolveHash, | ||
rejectHash: peStub.rejectHash, | ||
resolveReceipt: peStub.resolveReceipt, | ||
rejectReceipt: peStub.resolveReceipt, | ||
} | ||
return pe | ||
} | ||
|
||
describe('kit.sendTransactionObject()', () => { | ||
const kit = newKit('http://') | ||
|
||
test('should send transaction on simple case', async () => { | ||
const txo = txoStub() | ||
txo.estimateGasMock.mockResolvedValue(1000) | ||
const txRes = await kit.sendTransactionObject(txo) | ||
|
||
txo.resolveHash('HASH') | ||
txo.resolveReceipt('Receipt' as any) | ||
|
||
await expect(txRes.getHash()).resolves.toBe('HASH') | ||
await expect(txRes.waitReceipt()).resolves.toBe('Receipt') | ||
}) | ||
|
||
test('should not estimateGas if gas is provided', async () => { | ||
const txo = txoStub() | ||
await kit.sendTransactionObject(txo, { gas: 555 }) | ||
expect(txo.estimateGasMock).not.toBeCalled() | ||
}) | ||
|
||
test('should use inflation factor on gas', async () => { | ||
const txo = txoStub() | ||
txo.estimateGasMock.mockResolvedValue(1000) | ||
kit.gasInflactionFactor = 2 | ||
await kit.sendTransactionObject(txo) | ||
expect(txo.send).toBeCalledWith( | ||
expect.objectContaining({ | ||
gas: 1000 * 2, | ||
}) | ||
) | ||
}) | ||
|
||
test('should forward txoptions to txo.send()', async () => { | ||
const txo = txoStub() | ||
await kit.sendTransactionObject(txo, { gas: 555, gasCurrency: 'XXX', from: '0xAAFFF' }) | ||
expect(txo.send).toBeCalledWith({ | ||
gasPrice: '0', | ||
gas: 555, | ||
gasCurrency: 'XXX', | ||
from: '0xAAFFF', | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.