2021-11-23 19:18:44 +01:00
|
|
|
import React from 'react';
|
2022-01-07 20:18:02 +01:00
|
|
|
import { fireEvent } from '@testing-library/react';
|
|
|
|
import { renderWithProvider } from '../../../../test/jest';
|
|
|
|
import { submittedPendingTransactionsSelector } from '../../../selectors/transactions';
|
|
|
|
import { useGasFeeContext } from '../../../contexts/gasFee';
|
2021-11-23 19:18:44 +01:00
|
|
|
import configureStore from '../../../store/store';
|
|
|
|
import TransactionAlerts from './transaction-alerts';
|
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
jest.mock('../../../selectors/transactions', () => {
|
|
|
|
return {
|
|
|
|
...jest.requireActual('../../../selectors/transactions'),
|
|
|
|
submittedPendingTransactionsSelector: jest.fn(),
|
|
|
|
};
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
jest.mock('../../../contexts/gasFee');
|
|
|
|
|
|
|
|
function render({
|
|
|
|
componentProps = {},
|
|
|
|
useGasFeeContextValue = {},
|
|
|
|
submittedPendingTransactionsSelectorValue = null,
|
|
|
|
}) {
|
|
|
|
useGasFeeContext.mockReturnValue(useGasFeeContextValue);
|
|
|
|
submittedPendingTransactionsSelector.mockReturnValue(
|
|
|
|
submittedPendingTransactionsSelectorValue,
|
2021-11-23 19:18:44 +01:00
|
|
|
);
|
2022-01-07 20:18:02 +01:00
|
|
|
const store = configureStore({});
|
|
|
|
return renderWithProvider(<TransactionAlerts {...componentProps} />, store);
|
|
|
|
}
|
2021-11-23 19:18:44 +01:00
|
|
|
|
|
|
|
describe('TransactionAlerts', () => {
|
2022-12-08 19:37:06 +01:00
|
|
|
describe('when supportsEIP1559 from useGasFeeContext is truthy', () => {
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if hasSimulationError from useGasFeeContext is true', () => {
|
|
|
|
it('informs the user that a simulation of the transaction failed', () => {
|
|
|
|
const { getByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
hasSimulationError: true,
|
|
|
|
},
|
|
|
|
});
|
2021-11-29 17:08:24 +01:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
expect(
|
|
|
|
getByText(
|
|
|
|
'We were not able to estimate gas. There might be an error in the contract and this transaction may fail.',
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
2021-11-29 17:08:24 +01:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if the user has not acknowledged the failure', () => {
|
|
|
|
it('offers the user an option to bypass the warning', () => {
|
|
|
|
const { getByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
hasSimulationError: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(getByText('I want to proceed anyway')).toBeInTheDocument();
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
it('calls setUserAcknowledgedGasMissing if the user bypasses the warning', () => {
|
|
|
|
const setUserAcknowledgedGasMissing = jest.fn();
|
|
|
|
const { getByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
hasSimulationError: true,
|
|
|
|
},
|
|
|
|
componentProps: { setUserAcknowledgedGasMissing },
|
|
|
|
});
|
|
|
|
fireEvent.click(getByText('I want to proceed anyway'));
|
|
|
|
expect(setUserAcknowledgedGasMissing).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if the user has already acknowledged the failure', () => {
|
|
|
|
it('does not offer the user an option to bypass the warning', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
hasSimulationError: true,
|
|
|
|
},
|
|
|
|
componentProps: { userAcknowledgedGasMissing: true },
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText('I want to proceed anyway'),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
});
|
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if hasSimulationError from useGasFeeContext is falsy', () => {
|
|
|
|
it('does not inform the user that a simulation of the transaction failed', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText(
|
|
|
|
'We were not able to estimate gas. There might be an error in the contract and this transaction may fail.',
|
|
|
|
),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
});
|
|
|
|
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if the length of pendingTransactions is 1', () => {
|
|
|
|
it('informs the user that they have a pending transaction', () => {
|
|
|
|
const { getByText } = render({
|
2022-12-08 19:37:06 +01:00
|
|
|
useGasFeeContextValue: { supportsEIP1559: true },
|
2022-01-07 20:18:02 +01:00
|
|
|
submittedPendingTransactionsSelectorValue: [{ some: 'transaction' }],
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
getByText('You have (1) pending transaction.'),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if the length of pendingTransactions is more than 1', () => {
|
|
|
|
it('informs the user that they have pending transactions', () => {
|
|
|
|
const { getByText } = render({
|
2022-12-08 19:37:06 +01:00
|
|
|
useGasFeeContextValue: { supportsEIP1559: true },
|
2022-01-07 20:18:02 +01:00
|
|
|
submittedPendingTransactionsSelectorValue: [
|
|
|
|
{ some: 'transaction' },
|
|
|
|
{ some: 'transaction' },
|
|
|
|
],
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
getByText('You have (2) pending transactions.'),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if the length of pendingTransactions is 0', () => {
|
|
|
|
it('does not inform the user that they have pending transactions', () => {
|
|
|
|
const { queryByText } = render({
|
2022-12-08 19:37:06 +01:00
|
|
|
useGasFeeContextValue: { supportsEIP1559: true },
|
2022-01-07 20:18:02 +01:00
|
|
|
submittedPendingTransactionsSelectorValue: [],
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText('You have (0) pending transactions.'),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if balanceError from useGasFeeContext is falsy', () => {
|
|
|
|
it('does not inform the user that they have insufficient funds', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
balanceError: false,
|
2021-11-23 19:18:44 +01:00
|
|
|
},
|
2022-01-07 20:18:02 +01:00
|
|
|
});
|
|
|
|
expect(queryByText('Insufficient funds.')).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if estimateUsed from useGasFeeContext is "low"', () => {
|
|
|
|
it('informs the user that the current transaction is queued', () => {
|
|
|
|
const { getByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
estimateUsed: 'low',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
2022-01-22 01:23:02 +01:00
|
|
|
getByText('Future transactions will queue after this one.'),
|
2022-01-07 20:18:02 +01:00
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if estimateUsed from useGasFeeContext is not "low"', () => {
|
|
|
|
it('does not inform the user that the current transaction is queued', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
estimateUsed: 'something_else',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
2022-01-22 01:23:02 +01:00
|
|
|
queryByText('Future transactions will queue after this one.'),
|
2022-01-07 20:18:02 +01:00
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if isNetworkBusy from useGasFeeContext is truthy', () => {
|
|
|
|
it('informs the user that the network is busy', () => {
|
|
|
|
const { getByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
isNetworkBusy: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
getByText(
|
|
|
|
'Network is busy. Gas prices are high and estimates are less accurate.',
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if isNetworkBusy from useGasFeeContext is falsy', () => {
|
|
|
|
it('does not inform the user that the network is busy', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: true,
|
2022-01-07 20:18:02 +01:00
|
|
|
isNetworkBusy: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText(
|
|
|
|
'Network is busy. Gas prices are high and estimates are less accurate.',
|
|
|
|
),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
});
|
|
|
|
});
|
2021-11-29 17:08:24 +01:00
|
|
|
|
2022-12-08 19:37:06 +01:00
|
|
|
describe('when supportsEIP1559 from useGasFeeContext is falsy', () => {
|
2022-01-07 20:18:02 +01:00
|
|
|
describe('if hasSimulationError from useGasFeeContext is true', () => {
|
|
|
|
it('does not inform the user that a simulation of the transaction failed', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: false,
|
2022-01-07 20:18:02 +01:00
|
|
|
hasSimulationError: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
queryByText(
|
|
|
|
'We were not able to estimate gas. There might be an error in the contract and this transaction may fail.',
|
|
|
|
),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if the length of pendingTransactions is at least 1', () => {
|
2022-12-08 19:37:06 +01:00
|
|
|
it('does not informs the user that they have a pending transaction', () => {
|
2022-01-07 20:18:02 +01:00
|
|
|
const { queryByText } = render({
|
2022-12-08 19:37:06 +01:00
|
|
|
useGasFeeContextValue: { supportsEIP1559: false },
|
2022-01-07 20:18:02 +01:00
|
|
|
submittedPendingTransactionsSelectorValue: [{ some: 'transaction' }],
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText('You have (1) pending transaction.'),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if balanceError from useGasFeeContext is true', () => {
|
|
|
|
it('informs the user that they have insufficient funds', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: false,
|
2022-01-07 20:18:02 +01:00
|
|
|
balanceError: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(queryByText('Insufficient funds.')).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if estimateUsed from useGasFeeContext is "low"', () => {
|
|
|
|
it('informs the user that the current transaction is queued', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: false,
|
2022-01-07 20:18:02 +01:00
|
|
|
estimateUsed: 'low',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText(
|
|
|
|
'Future transactions will queue after this one. This price was last seen was some time ago.',
|
|
|
|
),
|
|
|
|
).not.toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('if isNetworkBusy from useGasFeeContext is truthy', () => {
|
|
|
|
it('does not inform the user that the network is busy', () => {
|
|
|
|
const { queryByText } = render({
|
|
|
|
useGasFeeContextValue: {
|
2022-12-08 19:37:06 +01:00
|
|
|
supportsEIP1559: false,
|
2022-01-07 20:18:02 +01:00
|
|
|
isNetworkBusy: true,
|
2021-11-29 17:08:24 +01:00
|
|
|
},
|
2022-01-07 20:18:02 +01:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
queryByText(
|
|
|
|
'Network is busy. Gas prices are high and estimates are less accurate.',
|
|
|
|
),
|
|
|
|
).not.toBeInTheDocument();
|
2021-11-29 17:08:24 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-11-23 19:18:44 +01:00
|
|
|
});
|