1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/components/app/wallet-overview/eth-overview.test.js
micaelae e77e20d8c4
Bridge tokens by redirecting to Portfolio from wallet overview page (#17952)
* Add Bridge button

* Add newline to the end of bridge svg

* Move Portfolio button

* Vertically center Portfolio button

* Use IconButton for Portfolio button

* Change portfolio button size

* Lowering coverage to get this in for release by a very small amount

* Add unit tests for Portfolio button

---------

Co-authored-by: georgewrmarshall <george.marshall@consensys.net>
2023-03-06 11:34:06 -08:00

214 lines
6.3 KiB
JavaScript

import React from 'react';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import { fireEvent, waitFor } from '@testing-library/react';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import { renderWithProvider } from '../../../../test/jest/rendering';
import { HardwareKeyringTypes } from '../../../../shared/constants/hardware-wallets';
import EthOverview from './eth-overview';
// Mock BUYABLE_CHAINS_MAP
jest.mock('../../../../shared/constants/network', () => ({
...jest.requireActual('../../../../shared/constants/network'),
BUYABLE_CHAINS_MAP: {
// MAINNET
'0x1': {
nativeCurrency: 'ETH',
network: 'ethereum',
},
// POLYGON
'0x89': {
nativeCurrency: 'MATIC',
network: 'polygon',
},
},
}));
let openTabSpy;
describe('EthOverview', () => {
const mockStore = {
metamask: {
provider: {
type: 'test',
chainId: CHAIN_IDS.MAINNET,
},
cachedBalances: {},
preferences: {
useNativeCurrencyAsPrimaryCurrency: true,
},
identities: {
'0x1': {
address: '0x1',
},
},
accounts: {
'0x1': {
address: '0x1',
balance: '0x1F4',
},
},
selectedAddress: '0x1',
keyrings: [
{
type: HardwareKeyringTypes.imported,
accounts: ['0x1', '0x2'],
},
{
type: HardwareKeyringTypes.ledger,
accounts: [],
},
],
contractExchangeRates: {},
},
};
const store = configureMockStore([thunk])(mockStore);
const ETH_OVERVIEW_BUY = 'eth-overview-buy';
const ETH_OVERVIEW_BRIDGE = 'eth-overview-bridge';
const ETH_OVERVIEW_PORTFOLIO = 'home__portfolio-site';
afterEach(() => {
store.clearActions();
});
describe('EthOverview', () => {
beforeAll(() => {
jest.clearAllMocks();
Object.defineProperty(global, 'platform', {
value: {
openTab: jest.fn(),
},
});
openTabSpy = jest.spyOn(global.platform, 'openTab');
});
beforeEach(() => {
openTabSpy.mockClear();
});
it('should always show the Bridge button', () => {
const { queryByTestId } = renderWithProvider(<EthOverview />, store);
const bridgeButton = queryByTestId(ETH_OVERVIEW_BRIDGE);
expect(bridgeButton).toBeInTheDocument();
});
it('should open the Bridge URI when clicking on Bridge button', async () => {
const { queryByTestId } = renderWithProvider(<EthOverview />, store);
const bridgeButton = queryByTestId(ETH_OVERVIEW_BRIDGE);
expect(bridgeButton).toBeInTheDocument();
expect(bridgeButton).not.toBeDisabled();
fireEvent.click(bridgeButton);
expect(openTabSpy).toHaveBeenCalledTimes(1);
await waitFor(() =>
expect(openTabSpy).toHaveBeenCalledWith({
url: expect.stringContaining(`/bridge?metamaskEntry=ext`),
}),
);
});
it('should always show the Portfolio button', () => {
const { queryByTestId } = renderWithProvider(<EthOverview />, store);
const portfolioButton = queryByTestId(ETH_OVERVIEW_PORTFOLIO);
expect(portfolioButton).toBeInTheDocument();
});
it('should open the Portfolio URI when clicking on Portfolio button', async () => {
const { queryByTestId } = renderWithProvider(<EthOverview />, store);
const portfolioButton = queryByTestId(ETH_OVERVIEW_PORTFOLIO);
expect(portfolioButton).toBeInTheDocument();
expect(portfolioButton).not.toBeDisabled();
fireEvent.click(portfolioButton);
expect(openTabSpy).toHaveBeenCalledTimes(1);
await waitFor(() =>
expect(openTabSpy).toHaveBeenCalledWith({
url: expect.stringContaining(`?metamaskEntry=ext`),
}),
);
});
it('should always show the Buy button regardless of current chain Id', () => {
const { queryByTestId } = renderWithProvider(<EthOverview />, store);
const buyButton = queryByTestId(ETH_OVERVIEW_BUY);
expect(buyButton).toBeInTheDocument();
});
it('should have the Buy native token button disabled if chain id is not part of supported buyable chains', () => {
const mockedStoreWithUnbuyableChainId = {
metamask: {
...mockStore.metamask,
provider: { type: 'test', chainId: CHAIN_IDS.FANTOM },
},
};
const mockedStore = configureMockStore([thunk])(
mockedStoreWithUnbuyableChainId,
);
const { queryByTestId } = renderWithProvider(
<EthOverview />,
mockedStore,
);
const buyButton = queryByTestId(ETH_OVERVIEW_BUY);
expect(buyButton).toBeInTheDocument();
expect(buyButton).toBeDisabled();
});
it('should have the Buy native token enabled if chain id is part of supported buyable chains', () => {
const mockedStoreWithUnbuyableChainId = {
metamask: {
...mockStore.metamask,
provider: { type: 'test', chainId: CHAIN_IDS.POLYGON },
},
};
const mockedStore = configureMockStore([thunk])(
mockedStoreWithUnbuyableChainId,
);
const { queryByTestId } = renderWithProvider(
<EthOverview />,
mockedStore,
);
const buyButton = queryByTestId(ETH_OVERVIEW_BUY);
expect(buyButton).toBeInTheDocument();
expect(buyButton).not.toBeDisabled();
});
it('should open the Buy native token URI when clicking on Buy button for a buyable chain ID', async () => {
const mockedStoreWithBuyableChainId = {
metamask: {
...mockStore.metamask,
provider: { type: 'test', chainId: CHAIN_IDS.POLYGON },
},
};
const mockedStore = configureMockStore([thunk])(
mockedStoreWithBuyableChainId,
);
const { queryByTestId } = renderWithProvider(
<EthOverview />,
mockedStore,
);
const buyButton = queryByTestId(ETH_OVERVIEW_BUY);
expect(buyButton).toBeInTheDocument();
expect(buyButton).not.toBeDisabled();
fireEvent.click(buyButton);
expect(openTabSpy).toHaveBeenCalledTimes(1);
await waitFor(() =>
expect(openTabSpy).toHaveBeenCalledWith({
url: expect.stringContaining(`/buy?metamaskEntry=ext_buy_button`),
}),
);
});
});
});