1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00
Alaa Hadad 5998ae21aa
Buy crypto by redirecting to onramp experience on pdapp instead of deposit popover (#17689)
* feat: disable deposit popover and replace it with global redirect to onramp on pdapp

* feat: remove legacy code for hardcoded onramp providers

* fix: remove unused visuals and components related to legacy code of deposit popover

* fix: remove unused messages

* feat: use a custom hook for all onramps related methods and variables

* fix: modify the custom hook implementation to include test networks

* fix: remove deprecated file buy-url

* fix: remove references for deleted deposit logos

* fix: network-controller failing unit test

* fix: snapshot loading-swaps-quotes-stories-metadata.test.js.snap

* fix: storybook tests

* fix: remove unused constatns related to buyable onramp chains

* fix: remove unused variables and fix eslint

* adding unit test for useRamps custom hook

* feat: add comment on the proper usage of useRamps within confirm-page-container component

* fix: add unit tests for buy button in token-overview page

* fix: add unit test for open the buy crypto URL for a buyable chain ID in token page

* feat: add unit test coverage for eth-overview page

* fix: update locales
2023-03-01 12:45:27 +04:00

160 lines
4.4 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',
},
},
}));
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';
afterEach(() => {
store.clearActions();
});
describe('EthOverview', () => {
beforeAll(() => {
jest.clearAllMocks();
Object.defineProperty(global, 'platform', {
value: {
openTab: jest.fn(),
},
});
});
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 openTabSpy = jest.spyOn(global.platform, 'openTab');
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`),
}),
);
});
});
});