You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
174 lines
5.5 KiB
174 lines
5.5 KiB
4 years ago
|
import assert from 'assert';
|
||
|
import * as reactRedux from 'react-redux';
|
||
|
import { renderHook } from '@testing-library/react-hooks';
|
||
|
import sinon from 'sinon';
|
||
4 years ago
|
import transactions from '../../../test/data/transaction-data.json';
|
||
|
import { getConversionRate, getSelectedAccount } from '../selectors';
|
||
|
import { showModal } from '../store/actions';
|
||
|
import { increaseLastGasPrice } from '../helpers/utils/confirm-tx.util';
|
||
|
import * as actionConstants from '../store/actionConstants';
|
||
|
import { useCancelTransaction } from './useCancelTransaction';
|
||
5 years ago
|
|
||
|
describe('useCancelTransaction', function () {
|
||
4 years ago
|
let useSelector;
|
||
|
const dispatch = sinon.spy();
|
||
5 years ago
|
|
||
|
before(function () {
|
||
4 years ago
|
sinon.stub(reactRedux, 'useDispatch').returns(dispatch);
|
||
|
});
|
||
5 years ago
|
|
||
|
afterEach(function () {
|
||
4 years ago
|
dispatch.resetHistory();
|
||
|
});
|
||
5 years ago
|
|
||
4 years ago
|
describe('when account has insufficient balance to cover gas', function () {
|
||
5 years ago
|
before(function () {
|
||
4 years ago
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
||
5 years ago
|
useSelector.callsFake((selector) => {
|
||
|
if (selector === getConversionRate) {
|
||
4 years ago
|
return 280.46;
|
||
5 years ago
|
} else if (selector === getSelectedAccount) {
|
||
|
return {
|
||
|
balance: '0x3',
|
||
4 years ago
|
};
|
||
5 years ago
|
}
|
||
4 years ago
|
return undefined;
|
||
|
});
|
||
|
});
|
||
5 years ago
|
transactions.forEach((transactionGroup) => {
|
||
4 years ago
|
const originalGasPrice =
|
||
4 years ago
|
transactionGroup.primaryTransaction.txParams?.gasPrice;
|
||
4 years ago
|
const gasPrice =
|
||
4 years ago
|
originalGasPrice && increaseLastGasPrice(originalGasPrice);
|
||
|
const transactionId = transactionGroup.initialTransaction.id;
|
||
5 years ago
|
it(`should indicate account has insufficient funds to cover ${gasPrice} gas price`, function () {
|
||
4 years ago
|
const { result } = renderHook(() =>
|
||
|
useCancelTransaction(transactionGroup),
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(result.current[0], false);
|
||
|
});
|
||
4 years ago
|
it(`should return a function that opens the gas sidebar onsubmit kicks off cancellation for id ${transactionId}`, function () {
|
||
4 years ago
|
const { result } = renderHook(() =>
|
||
|
useCancelTransaction(transactionGroup),
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(typeof result.current[1], 'function');
|
||
4 years ago
|
result.current[1]({
|
||
|
preventDefault: () => undefined,
|
||
|
stopPropagation: () => undefined,
|
||
4 years ago
|
});
|
||
4 years ago
|
const dispatchAction = dispatch.args;
|
||
|
|
||
|
// calls customize-gas sidebar
|
||
|
// also check type= customize-gas
|
||
|
assert.strictEqual(
|
||
|
dispatchAction[dispatchAction.length - 1][0].type,
|
||
|
actionConstants.SIDEBAR_OPEN,
|
||
|
);
|
||
|
|
||
|
assert.strictEqual(
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
||
|
.id,
|
||
|
transactionId,
|
||
|
);
|
||
|
|
||
|
// call onSubmit myself
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
||
|
'0x5208',
|
||
|
'0x1',
|
||
|
);
|
||
|
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
dispatch.calledWith(
|
||
|
showModal({
|
||
|
name: 'CANCEL_TRANSACTION',
|
||
|
transactionId,
|
||
4 years ago
|
newGasFee: '0x5208',
|
||
|
defaultNewGasPrice: '0x1',
|
||
|
gasLimit: '0x5208',
|
||
4 years ago
|
}),
|
||
5 years ago
|
),
|
||
4 years ago
|
true,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
after(function () {
|
||
4 years ago
|
useSelector.restore();
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('when account has sufficient balance to cover gas', function () {
|
||
|
before(function () {
|
||
4 years ago
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
||
5 years ago
|
useSelector.callsFake((selector) => {
|
||
|
if (selector === getConversionRate) {
|
||
4 years ago
|
return 280.46;
|
||
5 years ago
|
} else if (selector === getSelectedAccount) {
|
||
|
return {
|
||
|
balance: '0x9C2007651B2500000',
|
||
4 years ago
|
};
|
||
5 years ago
|
}
|
||
4 years ago
|
return undefined;
|
||
|
});
|
||
|
});
|
||
5 years ago
|
transactions.forEach((transactionGroup) => {
|
||
4 years ago
|
const originalGasPrice =
|
||
4 years ago
|
transactionGroup.primaryTransaction.txParams?.gasPrice;
|
||
4 years ago
|
const gasPrice =
|
||
4 years ago
|
originalGasPrice && increaseLastGasPrice(originalGasPrice);
|
||
|
const transactionId = transactionGroup.initialTransaction.id;
|
||
5 years ago
|
it(`should indicate account has funds to cover ${gasPrice} gas price`, function () {
|
||
4 years ago
|
const { result } = renderHook(() =>
|
||
|
useCancelTransaction(transactionGroup),
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(result.current[0], true);
|
||
|
});
|
||
4 years ago
|
it(`should return a function that opens the gas sidebar onsubmit kicks off cancellation for id ${transactionId}`, function () {
|
||
4 years ago
|
const { result } = renderHook(() =>
|
||
|
useCancelTransaction(transactionGroup),
|
||
4 years ago
|
);
|
||
|
assert.strictEqual(typeof result.current[1], 'function');
|
||
4 years ago
|
result.current[1]({
|
||
|
preventDefault: () => undefined,
|
||
|
stopPropagation: () => undefined,
|
||
4 years ago
|
});
|
||
4 years ago
|
const dispatchAction = dispatch.args;
|
||
|
|
||
|
assert.strictEqual(
|
||
|
dispatchAction[dispatchAction.length - 1][0].type,
|
||
|
actionConstants.SIDEBAR_OPEN,
|
||
|
);
|
||
|
assert.strictEqual(
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
||
|
.id,
|
||
|
transactionId,
|
||
|
);
|
||
|
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
||
|
'0x5208',
|
||
|
'0x1',
|
||
|
);
|
||
|
|
||
4 years ago
|
assert.strictEqual(
|
||
5 years ago
|
dispatch.calledWith(
|
||
|
showModal({
|
||
|
name: 'CANCEL_TRANSACTION',
|
||
|
transactionId,
|
||
4 years ago
|
newGasFee: '0x5208',
|
||
|
defaultNewGasPrice: '0x1',
|
||
|
gasLimit: '0x5208',
|
||
4 years ago
|
}),
|
||
5 years ago
|
),
|
||
4 years ago
|
true,
|
||
4 years ago
|
);
|
||
|
});
|
||
|
});
|
||
5 years ago
|
after(function () {
|
||
4 years ago
|
useSelector.restore();
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
after(function () {
|
||
4 years ago
|
sinon.restore();
|
||
|
});
|
||
|
});
|