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.
169 lines
5.4 KiB
169 lines
5.4 KiB
4 years ago
|
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';
|
||
4 years ago
|
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
|
|
||
4 years ago
|
beforeAll(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
|
afterAll(function () {
|
||
|
sinon.restore();
|
||
|
});
|
||
|
|
||
4 years ago
|
describe('when account has insufficient balance to cover gas', function () {
|
||
4 years ago
|
beforeAll(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;
|
||
|
});
|
||
|
});
|
||
4 years ago
|
afterAll(function () {
|
||
|
useSelector.restore();
|
||
|
});
|
||
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
|
);
|
||
4 years ago
|
expect(result.current[0]).toStrictEqual(false);
|
||
4 years ago
|
});
|
||
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
|
);
|
||
4 years ago
|
expect(typeof result.current[1]).toStrictEqual('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
|
||
4 years ago
|
expect(dispatchAction[dispatchAction.length - 1][0].type).toStrictEqual(
|
||
4 years ago
|
actionConstants.SIDEBAR_OPEN,
|
||
|
);
|
||
|
|
||
4 years ago
|
expect(
|
||
4 years ago
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
||
|
.id,
|
||
4 years ago
|
).toStrictEqual(transactionId);
|
||
4 years ago
|
|
||
|
// call onSubmit myself
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
||
|
'0x5208',
|
||
|
'0x1',
|
||
|
);
|
||
|
|
||
4 years ago
|
expect(
|
||
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
|
).toStrictEqual(true);
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
5 years ago
|
|
||
|
describe('when account has sufficient balance to cover gas', function () {
|
||
4 years ago
|
beforeAll(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;
|
||
|
});
|
||
|
});
|
||
4 years ago
|
|
||
|
afterAll(function () {
|
||
|
useSelector.restore();
|
||
|
});
|
||
|
|
||
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
|
);
|
||
4 years ago
|
expect(result.current[0]).toStrictEqual(true);
|
||
4 years ago
|
});
|
||
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
|
);
|
||
4 years ago
|
expect(typeof result.current[1]).toStrictEqual('function');
|
||
4 years ago
|
result.current[1]({
|
||
|
preventDefault: () => undefined,
|
||
|
stopPropagation: () => undefined,
|
||
4 years ago
|
});
|
||
4 years ago
|
const dispatchAction = dispatch.args;
|
||
|
|
||
4 years ago
|
expect(dispatchAction[dispatchAction.length - 1][0].type).toStrictEqual(
|
||
4 years ago
|
actionConstants.SIDEBAR_OPEN,
|
||
|
);
|
||
4 years ago
|
expect(
|
||
4 years ago
|
dispatchAction[dispatchAction.length - 1][0].value.props.transaction
|
||
|
.id,
|
||
4 years ago
|
).toStrictEqual(transactionId);
|
||
4 years ago
|
|
||
|
dispatchAction[dispatchAction.length - 1][0].value.props.onSubmit(
|
||
|
'0x5208',
|
||
|
'0x1',
|
||
|
);
|
||
|
|
||
4 years ago
|
expect(
|
||
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
|
).toStrictEqual(true);
|
||
4 years ago
|
});
|
||
|
});
|
||
|
});
|
||
|
});
|