mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
6e5c2f03bf
* addding the legacy tokenlist, tuning token detection OFF by default, adding new message while importing tokens updating the controller version and calling detectNewToken on network change fixing rebase error Run yarn lavamoat:auto for updating policies updating lavamoat Deleted node modules and run again lavamoat auto fixing rebase issues updating lavamoat policies updating lavamoat after rebasing policies updating custom token warning and blocking detectedtoken link when tpken detection is off for supported networks to update the token in fetchTosync updating the contract map object Revert build-system lavamoat policy changes Move token list selection logic from components to getTokenList selector updating the tokenList Update lavamoat Fix error updating lavamoat lint fix fix unit test fail fix unit test fail lint fix fixing rebase locale error rebase fix Revert build-system policy changes temp addressing review comments * rebase fix
180 lines
5.5 KiB
JavaScript
180 lines
5.5 KiB
JavaScript
import React from 'react';
|
|
import { fireEvent } from '@testing-library/react';
|
|
import { renderWithProvider } from '../../../test/lib/render-helpers';
|
|
import configureStore from '../../store/store';
|
|
import {
|
|
setPendingTokens,
|
|
clearPendingTokens,
|
|
getTokenStandardAndDetails,
|
|
} from '../../store/actions';
|
|
import ImportToken from './import-token.container';
|
|
|
|
jest.mock('../../store/actions', () => ({
|
|
getTokenStandardAndDetails: jest
|
|
.fn()
|
|
.mockImplementation(() => Promise.resolve({ standard: 'ERC20' })),
|
|
setPendingTokens: jest
|
|
.fn()
|
|
.mockImplementation(() => ({ type: 'SET_PENDING_TOKENS' })),
|
|
clearPendingTokens: jest
|
|
.fn()
|
|
.mockImplementation(() => ({ type: 'CLEAR_PENDING_TOKENS' })),
|
|
}));
|
|
|
|
describe('Import Token', () => {
|
|
const historyStub = jest.fn();
|
|
const props = {
|
|
history: {
|
|
push: historyStub,
|
|
},
|
|
showSearchTab: true,
|
|
tokenList: {},
|
|
};
|
|
|
|
const render = () => {
|
|
const baseStore = {
|
|
metamask: {
|
|
tokens: [],
|
|
provider: { chainId: '0x1' },
|
|
frequentRpcListDetail: [],
|
|
identities: {},
|
|
selectedAddress: '0x1231231',
|
|
useTokenDetection: true,
|
|
},
|
|
history: {
|
|
mostRecentOverviewPage: '/',
|
|
},
|
|
};
|
|
|
|
const store = configureStore(baseStore);
|
|
|
|
return renderWithProvider(<ImportToken {...props} />, store);
|
|
};
|
|
|
|
describe('Import Token', () => {
|
|
it('add custom token button is disabled when no fields are populated', () => {
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
const submit = getByText('Add custom token');
|
|
|
|
expect(submit).toBeDisabled();
|
|
});
|
|
|
|
it('edits token address', () => {
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
|
|
const tokenAddress = '0x617b3f8050a0BD94b6b1da02B4384eE5B4DF13F4';
|
|
const event = { target: { value: tokenAddress } };
|
|
fireEvent.change(document.getElementById('custom-address'), event);
|
|
|
|
expect(document.getElementById('custom-address').value).toStrictEqual(
|
|
tokenAddress,
|
|
);
|
|
});
|
|
|
|
it('edits token symbol', () => {
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
|
|
const tokenSymbol = 'META';
|
|
const event = { target: { value: tokenSymbol } };
|
|
fireEvent.change(document.getElementById('custom-symbol'), event);
|
|
|
|
expect(document.getElementById('custom-symbol').value).toStrictEqual(
|
|
tokenSymbol,
|
|
);
|
|
});
|
|
|
|
it('edits token decimal precision', () => {
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
|
|
const tokenPrecision = '2';
|
|
const event = { target: { value: tokenPrecision } };
|
|
fireEvent.change(document.getElementById('custom-decimals'), event);
|
|
|
|
expect(document.getElementById('custom-decimals').value).toStrictEqual(
|
|
tokenPrecision,
|
|
);
|
|
});
|
|
|
|
it('adds custom tokens successfully', async () => {
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
|
|
const submit = getByText('Add custom token');
|
|
expect(submit).toBeDisabled();
|
|
|
|
const tokenAddress = '0x617b3f8050a0BD94b6b1da02B4384eE5B4DF13F4';
|
|
fireEvent.change(document.getElementById('custom-address'), {
|
|
target: { value: tokenAddress },
|
|
});
|
|
expect(submit).not.toBeDisabled();
|
|
|
|
const tokenSymbol = 'META';
|
|
fireEvent.change(document.getElementById('custom-symbol'), {
|
|
target: { value: tokenSymbol },
|
|
});
|
|
|
|
const tokenPrecision = '2';
|
|
await fireEvent.change(document.getElementById('custom-decimals'), {
|
|
target: { value: tokenPrecision },
|
|
});
|
|
|
|
expect(submit).not.toBeDisabled();
|
|
fireEvent.click(submit);
|
|
expect(setPendingTokens).toHaveBeenCalledWith({
|
|
customToken: {
|
|
address: tokenAddress,
|
|
decimals: Number(tokenPrecision),
|
|
standard: 'ERC20',
|
|
symbol: tokenSymbol,
|
|
},
|
|
selectedTokens: {},
|
|
tokenAddressList: [],
|
|
});
|
|
expect(historyStub).toHaveBeenCalledWith('/confirm-import-token');
|
|
});
|
|
|
|
it('cancels out of import token flow', () => {
|
|
const { getByRole } = render();
|
|
const closeButton = getByRole('button', { name: 'close' });
|
|
fireEvent.click(closeButton);
|
|
|
|
expect(clearPendingTokens).toHaveBeenCalled();
|
|
expect(historyStub).toHaveBeenCalledWith('/');
|
|
});
|
|
|
|
it('sets and error when a token is an NFT', async () => {
|
|
process.env.COLLECTIBLES_V1 = true;
|
|
getTokenStandardAndDetails.mockImplementation(() =>
|
|
Promise.resolve({ standard: 'ERC721' }),
|
|
);
|
|
|
|
const { getByText } = render();
|
|
const customTokenButton = getByText('Custom token');
|
|
fireEvent.click(customTokenButton);
|
|
|
|
const submit = getByText('Add custom token');
|
|
expect(submit).toBeDisabled();
|
|
|
|
const tokenAddress = '0x617b3f8050a0BD94b6b1da02B4384eE5B4DF13F4';
|
|
await fireEvent.change(document.getElementById('custom-address'), {
|
|
target: { value: tokenAddress },
|
|
});
|
|
|
|
expect(submit).toBeDisabled();
|
|
|
|
// The last part of this error message won't be found by getByText because it is wrapped as a link.
|
|
const errorMessage = getByText('This token is an NFT. Add on the');
|
|
expect(errorMessage).toBeInTheDocument();
|
|
});
|
|
});
|
|
});
|