2021-06-23 23:35:25 +02:00
|
|
|
import React from 'react';
|
|
|
|
import configureMockStore from 'redux-mock-store';
|
|
|
|
import thunk from 'redux-thunk';
|
|
|
|
import { useLocation } from 'react-router-dom';
|
2022-07-18 19:01:10 +02:00
|
|
|
import { SEND_STAGES, startNewDraftTransaction } from '../../ducks/send';
|
2022-11-21 16:19:11 +01:00
|
|
|
import { domainInitialState } from '../../ducks/domains';
|
2022-09-14 16:55:31 +02:00
|
|
|
import { CHAIN_IDS } from '../../../shared/constants/network';
|
2022-11-28 17:41:42 +01:00
|
|
|
import {
|
|
|
|
renderWithProvider,
|
|
|
|
setBackgroundConnection,
|
|
|
|
} from '../../../test/jest';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { GasEstimateTypes } from '../../../shared/constants/gas';
|
2023-03-21 15:43:22 +01:00
|
|
|
import { KeyringType } from '../../../shared/constants/keyring';
|
2022-07-01 15:58:35 +02:00
|
|
|
import { INITIAL_SEND_STATE_FOR_EXISTING_DRAFT } from '../../../test/jest/mocks';
|
2021-06-23 23:35:25 +02:00
|
|
|
import Send from './send';
|
|
|
|
|
|
|
|
const middleware = [thunk];
|
|
|
|
|
2022-07-18 19:01:10 +02:00
|
|
|
jest.mock('../../ducks/send/send', () => {
|
|
|
|
const original = jest.requireActual('../../ducks/send/send');
|
|
|
|
return {
|
|
|
|
...original,
|
|
|
|
// We don't really need to start a draft transaction, and the mock store
|
|
|
|
// does not update as a result of action calls so instead we just ensure
|
|
|
|
// that the action WOULD be called.
|
|
|
|
startNewDraftTransaction: jest.fn(() => ({
|
|
|
|
type: 'TEST_START_NEW_DRAFT',
|
|
|
|
payload: null,
|
|
|
|
})),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
jest.mock('react-router-dom', () => {
|
|
|
|
const original = jest.requireActual('react-router-dom');
|
|
|
|
return {
|
|
|
|
...original,
|
|
|
|
useLocation: jest.fn(() => ({ search: '' })),
|
|
|
|
useHistory: () => ({
|
|
|
|
push: jest.fn(),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-11-28 17:41:42 +01:00
|
|
|
setBackgroundConnection({
|
|
|
|
getGasFeeTimeEstimate: jest.fn(),
|
|
|
|
getGasFeeEstimatesAndStartPolling: jest.fn(),
|
|
|
|
promisifiedBackground: jest.fn(),
|
|
|
|
});
|
|
|
|
|
2023-01-24 15:10:36 +01:00
|
|
|
jest.mock('@ethersproject/providers', () => {
|
|
|
|
const originalModule = jest.requireActual('@ethersproject/providers');
|
2022-07-12 16:30:31 +02:00
|
|
|
return {
|
|
|
|
...originalModule,
|
2023-01-24 15:10:36 +01:00
|
|
|
Web3Provider: jest.fn().mockImplementation(() => {
|
|
|
|
return {};
|
|
|
|
}),
|
2022-07-12 16:30:31 +02:00
|
|
|
};
|
|
|
|
});
|
2021-06-23 23:35:25 +02:00
|
|
|
const baseStore = {
|
2022-07-01 15:58:35 +02:00
|
|
|
send: INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
|
2022-11-21 16:19:11 +01:00
|
|
|
DNS: domainInitialState,
|
2021-06-23 23:35:25 +02:00
|
|
|
gas: {
|
|
|
|
customData: { limit: null, price: null },
|
|
|
|
},
|
|
|
|
history: { mostRecentOverviewPage: 'activity' },
|
|
|
|
metamask: {
|
2022-11-28 17:41:42 +01:00
|
|
|
unapprovedTxs: {
|
|
|
|
1: {
|
|
|
|
id: 1,
|
|
|
|
txParams: {
|
|
|
|
value: 'oldTxValue',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType: GasEstimateTypes.legacy,
|
2021-07-16 18:06:32 +02:00
|
|
|
gasFeeEstimates: {
|
|
|
|
low: '0',
|
|
|
|
medium: '1',
|
|
|
|
fast: '2',
|
|
|
|
},
|
2021-08-03 00:52:18 +02:00
|
|
|
selectedAddress: '0x0',
|
|
|
|
keyrings: [
|
|
|
|
{
|
2023-03-21 15:43:22 +01:00
|
|
|
type: KeyringType.hdKeyTree,
|
2021-08-03 00:52:18 +02:00
|
|
|
accounts: ['0x0'],
|
|
|
|
},
|
|
|
|
],
|
2021-07-16 18:06:32 +02:00
|
|
|
networkDetails: {
|
|
|
|
EIPS: {},
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
tokens: [],
|
|
|
|
preferences: {
|
|
|
|
useNativeCurrencyAsPrimaryCurrency: false,
|
|
|
|
},
|
|
|
|
currentCurrency: 'USD',
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: {
|
2022-09-29 05:26:01 +02:00
|
|
|
chainId: CHAIN_IDS.GOERLI,
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
|
|
|
nativeCurrency: 'ETH',
|
|
|
|
featureFlags: {
|
|
|
|
sendHexData: false,
|
|
|
|
},
|
|
|
|
addressBook: {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: [],
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2023-02-09 18:45:52 +01:00
|
|
|
currentNetworkTxList: [],
|
2021-06-23 23:35:25 +02:00
|
|
|
cachedBalances: {
|
2022-09-29 05:26:01 +02:00
|
|
|
[CHAIN_IDS.GOERLI]: {},
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
|
|
|
accounts: {
|
|
|
|
'0x0': { balance: '0x0', address: '0x0' },
|
|
|
|
},
|
2021-08-03 00:52:18 +02:00
|
|
|
identities: { '0x0': { address: '0x0' } },
|
2022-07-14 00:15:38 +02:00
|
|
|
tokenAddress: '0x32e6c34cd57087abbd59b5a4aecc4cb495924356',
|
|
|
|
tokenList: {
|
|
|
|
'0x32e6c34cd57087abbd59b5a4aecc4cb495924356': {
|
|
|
|
name: 'BitBase',
|
|
|
|
symbol: 'BTBS',
|
|
|
|
decimals: 18,
|
|
|
|
address: '0x32E6C34Cd57087aBBD59B5A4AECC4cB495924356',
|
|
|
|
iconUrl: 'BTBS.svg',
|
|
|
|
occurrences: null,
|
|
|
|
},
|
|
|
|
'0x3fa400483487a489ec9b1db29c4129063eec4654': {
|
|
|
|
name: 'Cryptokek.com',
|
|
|
|
symbol: 'KEK',
|
|
|
|
decimals: 18,
|
|
|
|
address: '0x3fa400483487A489EC9b1dB29C4129063EEC4654',
|
|
|
|
iconUrl: 'cryptokek.svg',
|
|
|
|
occurrences: null,
|
|
|
|
},
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-01-20 16:42:13 +01:00
|
|
|
appState: {
|
|
|
|
sendInputCurrencySwitched: false,
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('Send Page', () => {
|
|
|
|
describe('Send Flow Initialization', () => {
|
2022-07-01 15:58:35 +02:00
|
|
|
it('should initialize the ENS slice on render', () => {
|
2021-06-23 23:35:25 +02:00
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
renderWithProvider(<Send />, store);
|
|
|
|
const actions = store.getActions();
|
|
|
|
expect(actions).toStrictEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2022-11-21 16:19:11 +01:00
|
|
|
type: 'DNS/enableDomainLookup',
|
2021-06-23 23:35:25 +02:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should showQrScanner when location.search is ?scan=true', () => {
|
|
|
|
useLocation.mockImplementation(() => ({ search: '?scan=true' }));
|
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
renderWithProvider(<Send />, store);
|
|
|
|
const actions = store.getActions();
|
|
|
|
expect(actions).toStrictEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2022-11-21 16:19:11 +01:00
|
|
|
type: 'DNS/enableDomainLookup',
|
2021-06-23 23:35:25 +02:00
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
|
|
|
type: 'UI_MODAL_OPEN',
|
|
|
|
payload: { name: 'QR_SCANNER' },
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
useLocation.mockImplementation(() => ({ search: '' }));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-20 17:44:41 +02:00
|
|
|
describe('Send Flow', () => {
|
|
|
|
it('should render the header with Send to displayed', () => {
|
2021-06-23 23:35:25 +02:00
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
const { getByText } = renderWithProvider(<Send />, store);
|
2021-08-20 17:44:41 +02:00
|
|
|
expect(getByText('Send to')).toBeTruthy();
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
|
2022-11-21 16:19:11 +01:00
|
|
|
it('should render the DomainInput field', () => {
|
2021-06-23 23:35:25 +02:00
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
const { getByPlaceholderText } = renderWithProvider(<Send />, store);
|
|
|
|
expect(
|
2023-06-08 19:09:39 +02:00
|
|
|
getByPlaceholderText('Enter public address (0x) or ENS name'),
|
2021-06-23 23:35:25 +02:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not render the footer', () => {
|
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
const { queryByText } = renderWithProvider(<Send />, store);
|
|
|
|
expect(queryByText('Next')).toBeNull();
|
|
|
|
});
|
2022-07-18 19:01:10 +02:00
|
|
|
|
|
|
|
it('should render correctly even when a draftTransaction does not exist', () => {
|
|
|
|
const modifiedStore = {
|
|
|
|
...baseStore,
|
|
|
|
send: {
|
|
|
|
...baseStore.send,
|
|
|
|
currentTransactionUUID: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const store = configureMockStore(middleware)(modifiedStore);
|
|
|
|
const { getByPlaceholderText } = renderWithProvider(<Send />, store);
|
|
|
|
// Ensure that the send flow renders on the add recipient screen when
|
|
|
|
// there is no draft transaction.
|
|
|
|
expect(
|
2023-06-08 19:09:39 +02:00
|
|
|
getByPlaceholderText('Enter public address (0x) or ENS name'),
|
2022-07-18 19:01:10 +02:00
|
|
|
).toBeTruthy();
|
|
|
|
// Ensure we start a new draft transaction when its missing.
|
|
|
|
expect(startNewDraftTransaction).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
|
2021-08-20 17:44:41 +02:00
|
|
|
describe('Send and Edit Flow (draft)', () => {
|
2021-06-23 23:35:25 +02:00
|
|
|
it('should render the header with Send displayed', () => {
|
|
|
|
const store = configureMockStore(middleware)({
|
|
|
|
...baseStore,
|
|
|
|
send: { ...baseStore.send, stage: SEND_STAGES.DRAFT },
|
2022-11-28 17:41:42 +01:00
|
|
|
confirmTransaction: {
|
|
|
|
txData: {
|
|
|
|
id: 3111025347726181,
|
|
|
|
time: 1620723786838,
|
|
|
|
status: 'unapproved',
|
|
|
|
metamaskNetworkId: '5',
|
|
|
|
chainId: '0x5',
|
|
|
|
loadingDefaults: false,
|
|
|
|
txParams: {
|
|
|
|
from: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4',
|
|
|
|
to: '0xaD6D458402F60fD3Bd25163575031ACDce07538D',
|
|
|
|
value: '0x0',
|
|
|
|
data: '0x095ea7b30000000000000000000000009bc5baf874d2da8d216ae9f137804184ee5afef40000000000000000000000000000000000000000000000000000000000011170',
|
|
|
|
gas: '0xea60',
|
|
|
|
gasPrice: '0x4a817c800',
|
|
|
|
},
|
|
|
|
type: 'transfer',
|
|
|
|
origin: 'https://metamask.github.io',
|
|
|
|
transactionCategory: 'approve',
|
|
|
|
},
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
const { getByText } = renderWithProvider(<Send />, store);
|
|
|
|
expect(getByText('Send')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-11-21 16:19:11 +01:00
|
|
|
it('should render the DomainInput field', () => {
|
2021-06-23 23:35:25 +02:00
|
|
|
const store = configureMockStore(middleware)(baseStore);
|
|
|
|
const { getByPlaceholderText } = renderWithProvider(<Send />, store);
|
|
|
|
expect(
|
2023-06-08 19:09:39 +02:00
|
|
|
getByPlaceholderText('Enter public address (0x) or ENS name'),
|
2021-06-23 23:35:25 +02:00
|
|
|
).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render the footer', () => {
|
|
|
|
const store = configureMockStore(middleware)({
|
|
|
|
...baseStore,
|
|
|
|
send: { ...baseStore.send, stage: SEND_STAGES.DRAFT },
|
2022-11-28 17:41:42 +01:00
|
|
|
confirmTransaction: {
|
|
|
|
txData: {
|
|
|
|
id: 3111025347726181,
|
|
|
|
time: 1620723786838,
|
|
|
|
status: 'unapproved',
|
|
|
|
metamaskNetworkId: '5',
|
|
|
|
chainId: '0x5',
|
|
|
|
loadingDefaults: false,
|
|
|
|
txParams: {
|
|
|
|
from: '0x64a845a5b02460acf8a3d84503b0d68d028b4bb4',
|
|
|
|
to: '0xaD6D458402F60fD3Bd25163575031ACDce07538D',
|
|
|
|
value: '0x0',
|
|
|
|
data: '0x095ea7b30000000000000000000000009bc5baf874d2da8d216ae9f137804184ee5afef40000000000000000000000000000000000000000000000000000000000011170',
|
|
|
|
gas: '0xea60',
|
|
|
|
gasPrice: '0x4a817c800',
|
|
|
|
},
|
|
|
|
type: 'transfer',
|
|
|
|
origin: 'https://metamask.github.io',
|
|
|
|
transactionCategory: 'approve',
|
|
|
|
},
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
const { getByText } = renderWithProvider(<Send />, store);
|
|
|
|
expect(getByText('Next')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|