Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/__tests__/useBCH.test.js
/* eslint-disable no-native-reassign */ | /* eslint-disable no-native-reassign */ | ||||
import useBCH from '../useBCH'; | import useBCH from '../useBCH'; | ||||
import mockReturnGetUtxos from '../__mocks__/mockReturnGetUtxos'; | |||||
import mockReturnGetHydratedUtxoDetails from '../__mocks__/mockReturnGetHydratedUtxoDetails'; | import mockReturnGetHydratedUtxoDetails from '../__mocks__/mockReturnGetHydratedUtxoDetails'; | ||||
import mockReturnGetSlpBalancesAndUtxos from '../__mocks__/mockReturnGetSlpBalancesAndUtxos'; | import mockReturnGetSlpBalancesAndUtxos from '../__mocks__/mockReturnGetSlpBalancesAndUtxos'; | ||||
import sendBCHMock from '../__mocks__/sendBCH'; | import sendBCHMock from '../__mocks__/sendBCH'; | ||||
import BCHJS from '@psf/bch-js'; // TODO: should be removed when external lib not needed anymore | import BCHJS from '@psf/bch-js'; // TODO: should be removed when external lib not needed anymore | ||||
import { currency } from '../../components/Common/Ticker'; | import { currency } from '../../components/Common/Ticker'; | ||||
import sendBCH from '../__mocks__/sendBCH'; | |||||
import BigNumber from 'bignumber.js'; | import BigNumber from 'bignumber.js'; | ||||
describe('useBCH hook', () => { | describe('useBCH hook', () => { | ||||
it('gets Rest Api Url on testnet', () => { | it('gets Rest Api Url on testnet', () => { | ||||
process = { | process = { | ||||
env: { | env: { | ||||
REACT_APP_NETWORK: `testnet`, | REACT_APP_NETWORK: `testnet`, | ||||
REACT_APP_BCHA_APIS: | REACT_APP_BCHA_APIS: | ||||
▲ Show 20 Lines • Show All 88 Lines • ▼ Show 20 Lines | it('sends BCH correctly with callback', async () => { | ||||
), | ), | ||||
).toBe(`${currency.blockExplorerUrl}/tx/${expectedTxId}`); | ).toBe(`${currency.blockExplorerUrl}/tx/${expectedTxId}`); | ||||
expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | ||||
expectedHex, | expectedHex, | ||||
); | ); | ||||
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 base unit ${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, | |||||
expectedHex, | const expectedTxFeeInSats = 229; | ||||
utxos, | |||||
wallet, | |||||
addresses, | |||||
} = sendBCHMock; | |||||
BCH.RawTransactions.sendRawTransaction = jest | BCH.RawTransactions.sendRawTransaction = jest | ||||
.fn() | .fn() | ||||
.mockResolvedValue(expectedTxId); | .mockResolvedValue(expectedTxId); | ||||
const oneBaseUnitMoreThanBalance = new BigNumber(utxos[0].value) | |||||
.minus(expectedTxFeeInSats) | |||||
.plus(1) | |||||
.div(10 ** currency.cashDecimals) | |||||
.toString(); | |||||
const failedSendBch = sendBch( | const failedSendBch = sendBch( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
{ | { | ||||
addresses, | addresses, | ||||
values: [1], | values: [oneBaseUnitMoreThanBalance], | ||||
}, | }, | ||||
1.01, | 1.01, | ||||
); | ); | ||||
expect(failedSendBch).rejects.toThrow(new Error('Insufficient funds')); | expect(failedSendBch).rejects.toThrow(new Error('Insufficient funds')); | ||||
const nullValuesSendBch = await sendBch( | const nullValuesSendBch = await sendBch( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
utxos, | utxos, | ||||
▲ Show 20 Lines • Show All 126 Lines • Show Last 20 Lines |