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 { showModal } from '../store/actions';
|
|
|
|
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util';
|
|
|
|
import * as actionConstants from '../store/actionConstants';
|
2021-06-08 17:25:48 +02:00
|
|
|
import { GAS_LIMITS } from '../../shared/constants/gas';
|
2021-03-16 22:00:08 +01:00
|
|
|
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-04-15 20:01:46 +02:00
|
|
|
expect(result.current[0]).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-04-15 20:01:46 +02:00
|
|
|
expect(typeof result.current[1]).toStrictEqual('function');
|
2020-11-03 00:41:28 +01:00
|
|
|
result.current[1]({
|
|
|
|
preventDefault: () => undefined,
|
|
|
|
stopPropagation: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-03-12 20:26:07 +01:00
|
|
|
const dispatchAction = dispatch.args;
|
|
|
|
|
|
|
|
// calls customize-gas sidebar
|
|
|
|
// also check type= customize-gas
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(dispatchAction[dispatchAction.length - 1][0].type).toStrictEqual(
|
2021-03-12 20:26:07 +01:00
|
|
|
actionConstants.SIDEBAR_OPEN,
|
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2021-03-12 20:26:07 +01:00
|
|
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
|
|
|
.id,
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(transactionId);
|
2021-03-12 20:26:07 +01:00
|
|
|
|
|
|
|
// call onSubmit myself
|
|
|
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
2021-06-08 17:25:48 +02:00
|
|
|
GAS_LIMITS.SIMPLE,
|
2021-03-12 20:26:07 +01:00
|
|
|
'0x1',
|
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2020-05-27 21:45:20 +02:00
|
|
|
dispatch.calledWith(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
transactionId,
|
2021-06-08 17:25:48 +02:00
|
|
|
newGasFee: GAS_LIMITS.SIMPLE,
|
2021-03-12 20:26:07 +01:00
|
|
|
defaultNewGasPrice: '0x1',
|
2021-06-08 17:25:48 +02:00
|
|
|
gasLimit: GAS_LIMITS.SIMPLE,
|
2020-07-14 17:20:41 +02:00
|
|
|
}),
|
2020-05-27 21:45:20 +02:00
|
|
|
),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(true);
|
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-04-15 20:01:46 +02:00
|
|
|
expect(result.current[0]).toStrictEqual(true);
|
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-04-15 20:01:46 +02:00
|
|
|
expect(typeof result.current[1]).toStrictEqual('function');
|
2020-11-03 00:41:28 +01:00
|
|
|
result.current[1]({
|
|
|
|
preventDefault: () => undefined,
|
|
|
|
stopPropagation: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2021-03-12 20:26:07 +01:00
|
|
|
const dispatchAction = dispatch.args;
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(dispatchAction[dispatchAction.length - 1][0].type).toStrictEqual(
|
2021-03-12 20:26:07 +01:00
|
|
|
actionConstants.SIDEBAR_OPEN,
|
|
|
|
);
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2021-03-12 20:26:07 +01:00
|
|
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
|
|
|
.id,
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(transactionId);
|
2021-03-12 20:26:07 +01:00
|
|
|
|
|
|
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
2021-06-08 17:25:48 +02:00
|
|
|
GAS_LIMITS.SIMPLE,
|
2021-03-12 20:26:07 +01:00
|
|
|
'0x1',
|
|
|
|
);
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(
|
2020-05-26 22:49:11 +02:00
|
|
|
dispatch.calledWith(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
transactionId,
|
2021-06-08 17:25:48 +02:00
|
|
|
newGasFee: GAS_LIMITS.SIMPLE,
|
2021-03-12 20:26:07 +01:00
|
|
|
defaultNewGasPrice: '0x1',
|
2021-06-08 17:25:48 +02:00
|
|
|
gasLimit: GAS_LIMITS.SIMPLE,
|
2020-07-14 17:20:41 +02:00
|
|
|
}),
|
2020-05-26 22:49:11 +02:00
|
|
|
),
|
2021-04-15 20:01:46 +02:00
|
|
|
).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|