2021-02-04 19:15:23 +01:00
|
|
|
import * as reactRedux from 'react-redux';
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
import sinon from 'sinon';
|
2021-04-28 21:53:59 +02:00
|
|
|
import transactions from '../../test/data/transaction-data.json';
|
2021-03-16 22:00:08 +01:00
|
|
|
import { getConversionRate, getSelectedAccount } from '../selectors';
|
|
|
|
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util';
|
|
|
|
import { useCancelTransaction } from './useCancelTransaction';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
describe('useCancelTransaction', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let useSelector;
|
|
|
|
const dispatch = sinon.spy();
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.stub(reactRedux, 'useDispatch').returns(dispatch);
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
afterEach(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch.resetHistory();
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterAll(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
describe('when account has insufficient balance to cover gas', function () {
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
2020-05-26 22:49:11 +02:00
|
|
|
useSelector.callsFake((selector) => {
|
|
|
|
if (selector === getConversionRate) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return 280.46;
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getSelectedAccount) {
|
|
|
|
return {
|
|
|
|
balance: '0x3',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
afterAll(function () {
|
|
|
|
useSelector.restore();
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
transactions.forEach((transactionGroup) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const originalGasPrice =
|
2021-02-04 19:15:23 +01:00
|
|
|
transactionGroup.primaryTransaction.txParams?.gasPrice;
|
2020-11-03 00:41:28 +01:00
|
|
|
const gasPrice =
|
2021-02-04 19:15:23 +01:00
|
|
|
originalGasPrice && increaseLastGasPrice(originalGasPrice);
|
|
|
|
const transactionId = transactionGroup.initialTransaction.id;
|
2020-05-26 22:49:11 +02:00
|
|
|
it(`should indicate account has insufficient funds to cover ${gasPrice} gas price`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useCancelTransaction(transactionGroup),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-31 15:01:29 +02:00
|
|
|
expect(result.current.hasEnoughCancelGas).toStrictEqual(false);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-03-12 20:26:07 +01:00
|
|
|
it(`should return a function that opens the gas sidebar onsubmit kicks off cancellation for id ${transactionId}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useCancelTransaction(transactionGroup),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-31 15:01:29 +02:00
|
|
|
expect(typeof result.current.cancelTransaction).toStrictEqual(
|
2021-07-12 21:35:54 +02:00
|
|
|
'function',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
describe('when account has sufficient balance to cover gas', function () {
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
2020-05-26 22:49:11 +02:00
|
|
|
useSelector.callsFake((selector) => {
|
|
|
|
if (selector === getConversionRate) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return 280.46;
|
2020-05-26 22:49:11 +02:00
|
|
|
} else if (selector === getSelectedAccount) {
|
|
|
|
return {
|
|
|
|
balance: '0x9C2007651B2500000',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
afterAll(function () {
|
|
|
|
useSelector.restore();
|
|
|
|
});
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
transactions.forEach((transactionGroup) => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const originalGasPrice =
|
2021-02-04 19:15:23 +01:00
|
|
|
transactionGroup.primaryTransaction.txParams?.gasPrice;
|
2020-11-03 00:41:28 +01:00
|
|
|
const gasPrice =
|
2021-02-04 19:15:23 +01:00
|
|
|
originalGasPrice && increaseLastGasPrice(originalGasPrice);
|
|
|
|
const transactionId = transactionGroup.initialTransaction.id;
|
2020-05-26 22:49:11 +02:00
|
|
|
it(`should indicate account has funds to cover ${gasPrice} gas price`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useCancelTransaction(transactionGroup),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-31 15:01:29 +02:00
|
|
|
expect(result.current.hasEnoughCancelGas).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-07-31 03:29:21 +02:00
|
|
|
it(`should return a function that opens the gas popover onsubmit kicks off cancellation for id ${transactionId}`, function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useCancelTransaction(transactionGroup),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-31 15:01:29 +02:00
|
|
|
expect(typeof result.current.cancelTransaction).toStrictEqual(
|
2021-07-12 21:35:54 +02:00
|
|
|
'function',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|