2022-03-11 03:40:06 +01:00
|
|
|
import React from 'react';
|
2023-06-05 22:13:22 +02:00
|
|
|
import { act } from 'react-dom/test-utils';
|
|
|
|
import { ApprovalType } from '@metamask/controller-utils';
|
2022-03-11 03:40:06 +01:00
|
|
|
import { fireEvent, screen } from '@testing-library/react';
|
2023-06-05 22:13:22 +02:00
|
|
|
import {
|
|
|
|
resolvePendingApproval,
|
|
|
|
rejectPendingApproval,
|
|
|
|
} from '../../store/actions';
|
2022-03-11 03:40:06 +01:00
|
|
|
import configureStore from '../../store/store';
|
|
|
|
import { renderWithProvider } from '../../../test/jest/rendering';
|
|
|
|
import ConfirmAddSuggestedToken from '.';
|
|
|
|
|
|
|
|
const MOCK_SUGGESTED_ASSETS = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
asset: {
|
|
|
|
address: '0x8b175474e89094c44da98b954eedeac495271d0a',
|
|
|
|
symbol: 'NEW',
|
|
|
|
decimals: 18,
|
|
|
|
image: 'metamark.svg',
|
|
|
|
unlisted: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
asset: {
|
|
|
|
address: '0xC8c77482e45F1F44dE1745F52C74426C631bDD51',
|
|
|
|
symbol: '0XYX',
|
|
|
|
decimals: 18,
|
|
|
|
image: '0x.svg',
|
|
|
|
unlisted: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
const MOCK_PENDING_ASSET_APPROVALS = MOCK_SUGGESTED_ASSETS.map(
|
|
|
|
(requestData) => {
|
|
|
|
return {
|
|
|
|
type: ApprovalType.WatchAsset,
|
|
|
|
requestData,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-03-11 03:40:06 +01:00
|
|
|
const MOCK_TOKEN = {
|
|
|
|
address: '0x108cf70c7d384c552f42c07c41c0e1e46d77ea0d',
|
|
|
|
symbol: 'TEST',
|
|
|
|
decimals: '0',
|
|
|
|
};
|
|
|
|
|
|
|
|
jest.mock('../../store/actions', () => ({
|
2023-06-05 22:13:22 +02:00
|
|
|
resolvePendingApproval: jest.fn().mockReturnValue({ type: 'test' }),
|
|
|
|
rejectPendingApproval: jest.fn().mockReturnValue({ type: 'test' }),
|
2022-03-11 03:40:06 +01:00
|
|
|
}));
|
|
|
|
|
|
|
|
const renderComponent = (tokens = []) => {
|
|
|
|
const store = configureStore({
|
|
|
|
metamask: {
|
2023-06-05 22:13:22 +02:00
|
|
|
pendingApprovals: [...MOCK_PENDING_ASSET_APPROVALS],
|
2022-03-11 03:40:06 +01:00
|
|
|
tokens,
|
2023-05-02 17:53:20 +02:00
|
|
|
providerConfig: { chainId: '0x1' },
|
2022-03-11 03:40:06 +01:00
|
|
|
},
|
|
|
|
history: {
|
|
|
|
mostRecentOverviewPage: '/',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return renderWithProvider(<ConfirmAddSuggestedToken />, store);
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('ConfirmAddSuggestedToken Component', () => {
|
|
|
|
it('should render', () => {
|
|
|
|
renderComponent();
|
|
|
|
|
2022-08-08 21:01:38 +02:00
|
|
|
expect(screen.getByText('Add suggested tokens')).toBeInTheDocument();
|
2022-03-11 03:40:06 +01:00
|
|
|
expect(
|
|
|
|
screen.getByText('Would you like to import these tokens?'),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('Token')).toBeInTheDocument();
|
|
|
|
expect(screen.getByText('Balance')).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
|
|
|
|
expect(
|
2022-08-08 21:01:38 +02:00
|
|
|
screen.getByRole('button', { name: 'Add token' }),
|
2022-03-11 03:40:06 +01:00
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should render the list of suggested tokens', () => {
|
|
|
|
renderComponent();
|
|
|
|
|
|
|
|
for (const { asset } of MOCK_SUGGESTED_ASSETS) {
|
|
|
|
expect(screen.getByText(asset.symbol)).toBeInTheDocument();
|
|
|
|
}
|
|
|
|
expect(screen.getAllByRole('img')).toHaveLength(
|
|
|
|
MOCK_SUGGESTED_ASSETS.length,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
it('should dispatch resolvePendingApproval when clicking the "Add token" button', async () => {
|
2022-03-11 03:40:06 +01:00
|
|
|
renderComponent();
|
2022-08-08 21:01:38 +02:00
|
|
|
const addTokenBtn = screen.getByRole('button', { name: 'Add token' });
|
2022-03-11 03:40:06 +01:00
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
await act(async () => {
|
|
|
|
fireEvent.click(addTokenBtn);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(resolvePendingApproval).toHaveBeenCalledTimes(
|
|
|
|
MOCK_SUGGESTED_ASSETS.length,
|
|
|
|
);
|
|
|
|
|
|
|
|
MOCK_SUGGESTED_ASSETS.forEach(({ id }) => {
|
|
|
|
expect(resolvePendingApproval).toHaveBeenCalledWith(id, null);
|
|
|
|
});
|
2022-03-11 03:40:06 +01:00
|
|
|
});
|
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
it('should dispatch rejectPendingApproval when clicking the "Cancel" button', async () => {
|
2022-03-11 03:40:06 +01:00
|
|
|
renderComponent();
|
|
|
|
const cancelBtn = screen.getByRole('button', { name: 'Cancel' });
|
|
|
|
|
2023-06-05 22:13:22 +02:00
|
|
|
await act(async () => {
|
|
|
|
fireEvent.click(cancelBtn);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(rejectPendingApproval).toHaveBeenCalledTimes(
|
2022-03-11 03:40:06 +01:00
|
|
|
MOCK_SUGGESTED_ASSETS.length,
|
|
|
|
);
|
2023-06-05 22:13:22 +02:00
|
|
|
|
|
|
|
MOCK_SUGGESTED_ASSETS.forEach(({ id }) => {
|
|
|
|
expect(rejectPendingApproval).toHaveBeenCalledWith(
|
|
|
|
id,
|
|
|
|
expect.objectContaining({
|
|
|
|
code: 4001,
|
|
|
|
message: 'User rejected the request.',
|
|
|
|
stack: expect.any(String),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
2022-03-11 03:40:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the suggested token address matches an existing token address', () => {
|
|
|
|
it('should show "already listed" warning', () => {
|
|
|
|
const mockTokens = [
|
|
|
|
{
|
|
|
|
...MOCK_TOKEN,
|
|
|
|
address: MOCK_SUGGESTED_ASSETS[0].asset.address,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
renderComponent(mockTokens);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.getByText(
|
|
|
|
'This action will edit tokens that are already listed in your wallet, which can be used' +
|
|
|
|
' to phish you. Only approve if you are certain that you mean to change what these' +
|
|
|
|
' tokens represent. Learn more about',
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
expect(
|
|
|
|
screen.getByRole('link', { name: 'scams and security risks.' }),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the suggested token symbol matches an existing token symbol and has a different address', () => {
|
|
|
|
it('should show "reuses a symbol" warning', () => {
|
|
|
|
const mockTokens = [
|
|
|
|
{
|
|
|
|
...MOCK_TOKEN,
|
|
|
|
symbol: MOCK_SUGGESTED_ASSETS[0].asset.symbol,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
renderComponent(mockTokens);
|
|
|
|
|
|
|
|
expect(
|
|
|
|
screen.getByText(
|
|
|
|
'A token here reuses a symbol from another token you watch, this can be confusing or deceptive.',
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|