Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/__tests__/useBCH.test.js
Show First 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | it('calculates fee correctly for 2 P2PKH outputs', () => { | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const utxosMock = [{}, {}]; | const utxosMock = [{}, {}]; | ||||
// For 1.01 sat/byte fee | // For 1.01 sat/byte fee | ||||
let expectedTxFee = 378; | let expectedTxFee = 378; | ||||
if (currency.defaultFee === 3.01) { | if (currency.defaultFee === 3.01) { | ||||
expectedTxFee = 1126; | expectedTxFee = 1126; | ||||
} else if (currency.defaultFee === 5.01) { | } else if (currency.defaultFee === 5.01) { | ||||
expectedTxFee = 1874; | expectedTxFee = 1874; | ||||
} else if ((currency.defaultFee = 83.3)) { | } else if (currency.defaultFee === 83.3) { | ||||
expectedTxFee = 31155; | expectedTxFee = 31155; | ||||
} | } | ||||
expect(calcFee(BCH, utxosMock)).toBe(expectedTxFee); | expect(calcFee(BCH, utxosMock)).toBe(expectedTxFee); | ||||
}); | }); | ||||
it('calculates fee correctly for 1 P2PKH output', () => { | |||||
const { calcFee } = useBCH(); | |||||
const BCH = new BCHJS(); | |||||
const utxosMock = [{}]; | |||||
// For 1.01 sat/byte fee | |||||
let expectedTxFee = 229; | |||||
if (currency.defaultFee === 3.01) { | |||||
expectedTxFee = 681; | |||||
} else if (currency.defaultFee === 5.01) { | |||||
expectedTxFee = 1133; | |||||
} else if (currency.defaultFee === 83.3) { | |||||
expectedTxFee = 18826; | |||||
} | |||||
expect(calcFee(BCH, utxosMock)).toBe(expectedTxFee); | |||||
}); | |||||
it('gets SLP and BCH balances and utxos from hydrated utxo details', async () => { | it('gets SLP and BCH balances and utxos from hydrated utxo details', async () => { | ||||
const { getSlpBalancesAndUtxos } = useBCH(); | const { getSlpBalancesAndUtxos } = useBCH(); | ||||
const result = await getSlpBalancesAndUtxos( | const result = await getSlpBalancesAndUtxos( | ||||
mockReturnGetHydratedUtxoDetails, | mockReturnGetHydratedUtxoDetails, | ||||
); | ); | ||||
expect(result).toStrictEqual(mockReturnGetSlpBalancesAndUtxos); | expect(result).toStrictEqual(mockReturnGetSlpBalancesAndUtxos); | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | it('sends BCH correctly with callback', async () => { | ||||
await sendBch(BCH, wallet, utxos, { addresses, values }, callback), | await sendBch(BCH, wallet, utxos, { addresses, values }, callback), | ||||
).toBe(`${currency.blockExplorerUrl}/tx/${expectedTxId}`); | ).toBe(`${currency.blockExplorerUrl}/tx/${expectedTxId}`); | ||||
expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | ||||
expectedHexByFee, | expectedHexByFee, | ||||
); | ); | ||||
expect(callback).toHaveBeenCalledWith(expectedTxId); | expect(callback).toHaveBeenCalledWith(expectedTxId); | ||||
}); | }); | ||||
it('sends BCH with less BCH available on balance', async () => { | it(`Throws error if called trying to send one satoshi ${currency.ticker} more than available in utxo set`, async () => { | ||||
const { sendBch } = useBCH(); | const { sendBch } = useBCH(); | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { | const { expectedTxId, utxos, wallet, addresses } = sendBCHMock; | ||||
expectedTxId, | let expectedTxFee = 229; | ||||
expectedHex, | if (currency.defaultFee === 3.01) { | ||||
utxos, | expectedTxFee = 681; | ||||
wallet, | } else if (currency.defaultFee === 5.01) { | ||||
addresses, | expectedTxFee = 1133; | ||||
} = sendBCHMock; | } else if (currency.defaultFee === 83.3) { | ||||
expectedTxFee = 18826; | |||||
} | |||||
BCH.RawTransactions.sendRawTransaction = jest | BCH.RawTransactions.sendRawTransaction = jest | ||||
.fn() | .fn() | ||||
.mockResolvedValue(expectedTxId); | .mockResolvedValue(expectedTxId); | ||||
const oneBaseUnitMoreThanBalance = new BigNumber(utxos[0].value) | |||||
.minus(expectedTxFee) | |||||
.plus(1) | |||||
.div(10 ** currency.cashDecimals) | |||||
.toString(); | |||||
const failedSendBch = sendBch(BCH, wallet, utxos, { | const failedSendBch = sendBch(BCH, wallet, utxos, { | ||||
addresses, | addresses, | ||||
values: [1], | values: [oneBaseUnitMoreThanBalance], | ||||
}); | }); | ||||
expect(failedSendBch).rejects.toThrow(new Error('Insufficient funds')); | expect(failedSendBch).rejects.toThrow(new Error('Insufficient funds')); | ||||
const nullValuesSendBch = await sendBch(BCH, wallet, utxos, { | const nullValuesSendBch = await sendBch(BCH, wallet, utxos, { | ||||
addresses, | addresses, | ||||
values: null, | values: null, | ||||
}); | }); | ||||
expect(nullValuesSendBch).toBe(null); | expect(nullValuesSendBch).toBe(null); | ||||
}); | }); | ||||
▲ Show 20 Lines • Show All 82 Lines • Show Last 20 Lines |