1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/ui/pages/send/send.test.js
Dan J Miller c63714c4f2 Show users a warning when they are sending directly to a token contract (#13588)
* Fix warning dialog when sending tokens to a known token contract address

Fixing after rebase

Covering missed cases

Rebased and ran yarn setup

Rebased

Fix checkContractAddress condition

Lint fix

Applied requested changes

Fix unit tests

Applying requested changes

Applied requested changes

Refactor and update

Lint fix

Use V2 of ActionableMessage component

Adding Learn More Link

Updating warning copy

Addressing review feedback

Fix up copy changes

Simplify validation of pasted addresses

Improve detection of whether this is a token contract

Refactor to leave updateRecipient unchanged, and to prevent the double calling of update recipient

Update tests

fix

* Fix unit tests

* Fix e2e tests

* Ensure next button is disabled while recipient type is loading

* Add optional chaining and a fallback to getRecipientWarningAcknowledgement

* Fix lint

* Don't reset recipient warning on asset change, because we should show recipient warnings regardless of asset

* Update unit tests

* Update unit tests

Co-authored-by: Filip Sekulic <filip.sekulic@consensys.net>
2022-07-13 19:46:30 -02:30

193 lines
5.5 KiB
JavaScript

import React from 'react';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { useLocation } from 'react-router-dom';
import { SEND_STAGES } from '../../ducks/send';
import { ensInitialState } from '../../ducks/ens';
import { renderWithProvider } from '../../../test/jest';
import { RINKEBY_CHAIN_ID } from '../../../shared/constants/network';
import { GAS_ESTIMATE_TYPES } from '../../../shared/constants/gas';
import { INITIAL_SEND_STATE_FOR_EXISTING_DRAFT } from '../../../test/jest/mocks';
import Send from './send';
const middleware = [thunk];
jest.mock('react-router-dom', () => {
const original = jest.requireActual('react-router-dom');
return {
...original,
useLocation: jest.fn(() => ({ search: '' })),
useHistory: () => ({
push: jest.fn(),
}),
};
});
jest.mock(
'ethjs-ens',
() =>
class MocKENS {
async ensLookup() {
return '';
}
},
);
const baseStore = {
send: INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
ENS: ensInitialState,
gas: {
customData: { limit: null, price: null },
},
history: { mostRecentOverviewPage: 'activity' },
metamask: {
gasEstimateType: GAS_ESTIMATE_TYPES.LEGACY,
gasFeeEstimates: {
low: '0',
medium: '1',
fast: '2',
},
selectedAddress: '0x0',
keyrings: [
{
type: 'HD Key Tree',
accounts: ['0x0'],
},
],
networkDetails: {
EIPS: {},
},
tokens: [],
preferences: {
useNativeCurrencyAsPrimaryCurrency: false,
},
currentCurrency: 'USD',
provider: {
chainId: RINKEBY_CHAIN_ID,
},
nativeCurrency: 'ETH',
featureFlags: {
sendHexData: false,
},
addressBook: {
[RINKEBY_CHAIN_ID]: [],
},
cachedBalances: {
[RINKEBY_CHAIN_ID]: {},
},
accounts: {
'0x0': { balance: '0x0', address: '0x0' },
},
identities: { '0x0': { address: '0x0' } },
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,
},
},
},
appState: {
sendInputCurrencySwitched: false,
},
};
describe('Send Page', () => {
describe('Send Flow Initialization', () => {
it('should initialize the ENS slice on render', () => {
const store = configureMockStore(middleware)(baseStore);
renderWithProvider(<Send />, store);
const actions = store.getActions();
expect(actions).toStrictEqual(
expect.arrayContaining([
expect.objectContaining({
type: 'ENS/enableEnsLookup',
}),
]),
);
});
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({
type: 'ENS/enableEnsLookup',
}),
expect.objectContaining({
type: 'UI_MODAL_OPEN',
payload: { name: 'QR_SCANNER' },
}),
]),
);
useLocation.mockImplementation(() => ({ search: '' }));
});
});
describe('Send Flow', () => {
it('should render the header with Send to displayed', () => {
const store = configureMockStore(middleware)(baseStore);
const { getByText } = renderWithProvider(<Send />, store);
expect(getByText('Send to')).toBeTruthy();
});
it('should render the EnsInput field', () => {
const store = configureMockStore(middleware)(baseStore);
const { getByPlaceholderText } = renderWithProvider(<Send />, store);
expect(
getByPlaceholderText('Search, public address (0x), or ENS'),
).toBeTruthy();
});
it('should not render the footer', () => {
const store = configureMockStore(middleware)(baseStore);
const { queryByText } = renderWithProvider(<Send />, store);
expect(queryByText('Next')).toBeNull();
});
});
describe('Send and Edit Flow (draft)', () => {
it('should render the header with Send displayed', () => {
const store = configureMockStore(middleware)({
...baseStore,
send: { ...baseStore.send, stage: SEND_STAGES.DRAFT },
});
const { getByText } = renderWithProvider(<Send />, store);
expect(getByText('Send')).toBeTruthy();
});
it('should render the EnsInput field', () => {
const store = configureMockStore(middleware)(baseStore);
const { getByPlaceholderText } = renderWithProvider(<Send />, store);
expect(
getByPlaceholderText('Search, public address (0x), or ENS'),
).toBeTruthy();
});
it('should render the footer', () => {
const store = configureMockStore(middleware)({
...baseStore,
send: { ...baseStore.send, stage: SEND_STAGES.DRAFT },
});
const { getByText } = renderWithProvider(<Send />, store);
expect(getByText('Next')).toBeTruthy();
});
});
});