2021-02-04 19:15:23 +01:00
|
|
|
import assert from 'assert';
|
|
|
|
import * as reactRedux from 'react-redux';
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import transactions from '../../../../test/data/transaction-data.json';
|
|
|
|
import { getConversionRate, getSelectedAccount } from '../../selectors';
|
|
|
|
import { useCancelTransaction } from '../useCancelTransaction';
|
|
|
|
import { showModal } from '../../store/actions';
|
|
|
|
import { increaseLastGasPrice } from '../../helpers/utils/confirm-tx.util';
|
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
|
|
|
|
|
|
|
before(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
|
|
|
|
2020-07-20 19:02:49 +02:00
|
|
|
describe('when account has insufficient balance to cover gas', function () {
|
2020-05-26 22:49:11 +02:00
|
|
|
before(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;
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
);
|
|
|
|
assert.strictEqual(result.current[0], false);
|
|
|
|
});
|
2020-05-27 21:45:20 +02:00
|
|
|
it(`should return a function that 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
|
|
|
);
|
|
|
|
assert.strictEqual(typeof result.current[1], 'function');
|
2020-11-03 00:41:28 +01:00
|
|
|
result.current[1]({
|
|
|
|
preventDefault: () => undefined,
|
|
|
|
stopPropagation: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
2020-05-27 21:45:20 +02:00
|
|
|
dispatch.calledWith(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
transactionId,
|
|
|
|
originalGasPrice,
|
2020-07-14 17:20:41 +02:00
|
|
|
}),
|
2020-05-27 21:45:20 +02:00
|
|
|
),
|
2020-07-14 17:20:41 +02:00
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
after(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
useSelector.restore();
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
describe('when account has sufficient balance to cover gas', function () {
|
|
|
|
before(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;
|
|
|
|
});
|
|
|
|
});
|
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
|
|
|
);
|
|
|
|
assert.strictEqual(result.current[0], true);
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
it(`should return a function that 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
|
|
|
);
|
|
|
|
assert.strictEqual(typeof result.current[1], 'function');
|
2020-11-03 00:41:28 +01:00
|
|
|
result.current[1]({
|
|
|
|
preventDefault: () => undefined,
|
|
|
|
stopPropagation: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
2020-05-26 22:49:11 +02:00
|
|
|
dispatch.calledWith(
|
|
|
|
showModal({
|
|
|
|
name: 'CANCEL_TRANSACTION',
|
|
|
|
transactionId,
|
|
|
|
originalGasPrice,
|
2020-07-14 17:20:41 +02:00
|
|
|
}),
|
2020-05-26 22:49:11 +02:00
|
|
|
),
|
2020-07-14 17:20:41 +02:00
|
|
|
true,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
after(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
useSelector.restore();
|
|
|
|
});
|
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
after(function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
});
|