2021-02-04 19:15:23 +01:00
|
|
|
import * as reactRedux from 'react-redux';
|
2021-07-31 03:29:21 +02:00
|
|
|
import { renderHook, act } from '@testing-library/react-hooks';
|
2021-02-04 19:15:23 +01:00
|
|
|
import sinon from 'sinon';
|
2021-04-28 21:53:59 +02:00
|
|
|
import transactions from '../../test/data/transaction-data.json';
|
2021-05-18 18:36:05 +02:00
|
|
|
import { getIsMainnet } from '../selectors';
|
2021-03-16 22:00:08 +01:00
|
|
|
import * as methodDataHook from './useMethodData';
|
|
|
|
import * as metricEventHook from './useMetricEvent';
|
|
|
|
import { useRetryTransaction } from './useRetryTransaction';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
jest.mock('./useGasFeeEstimates', () => ({
|
|
|
|
useGasFeeEstimates: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
describe('useRetryTransaction', () => {
|
|
|
|
describe('when transaction meets retry enabled criteria', () => {
|
2021-05-18 18:36:05 +02:00
|
|
|
let useSelector;
|
2021-02-04 19:15:23 +01:00
|
|
|
const dispatch = sinon.spy(() => Promise.resolve({ blockTime: 0 }));
|
|
|
|
const trackEvent = sinon.spy();
|
2020-11-03 00:41:28 +01:00
|
|
|
const event = {
|
|
|
|
preventDefault: () => undefined,
|
|
|
|
stopPropagation: () => undefined,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-04-15 20:01:46 +02:00
|
|
|
beforeAll(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
sinon.stub(reactRedux, 'useDispatch').returns(dispatch);
|
|
|
|
sinon.stub(methodDataHook, 'useMethodData').returns({});
|
|
|
|
sinon.stub(metricEventHook, 'useMetricEvent').returns(trackEvent);
|
2021-05-18 18:36:05 +02:00
|
|
|
useSelector = sinon.stub(reactRedux, 'useSelector');
|
|
|
|
useSelector.callsFake((selector) => {
|
|
|
|
if (selector === getIsMainnet) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
});
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
afterEach(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
dispatch.resetHistory();
|
|
|
|
trackEvent.resetHistory();
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
2020-05-26 22:49:11 +02:00
|
|
|
const retryEnabledTransaction = {
|
|
|
|
...transactions[0],
|
|
|
|
transactions: [
|
|
|
|
{
|
|
|
|
submittedTime: new Date() - 5001,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
hasRetried: false,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-04-15 20:01:46 +02:00
|
|
|
it('retryTransaction function should track metrics', () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useRetryTransaction(retryEnabledTransaction, true),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-12 21:35:54 +02:00
|
|
|
const { retryTransaction } = result.current;
|
2021-07-31 03:29:21 +02:00
|
|
|
act(() => {
|
|
|
|
retryTransaction(event);
|
|
|
|
});
|
2021-04-15 20:01:46 +02:00
|
|
|
expect(trackEvent.calledOnce).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-07-31 03:29:21 +02:00
|
|
|
it('retryTransaction function should show retry popover', async () => {
|
2020-11-03 00:41:28 +01:00
|
|
|
const { result } = renderHook(() =>
|
|
|
|
useRetryTransaction(retryEnabledTransaction, true),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2021-07-12 21:35:54 +02:00
|
|
|
const { retryTransaction } = result.current;
|
2021-07-31 03:29:21 +02:00
|
|
|
await act(async () => {
|
|
|
|
await retryTransaction(event);
|
|
|
|
});
|
|
|
|
expect(result.current.showRetryEditGasPopover).toStrictEqual(true);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|