diff --git a/app/components/UI/Stake/components/StakeButton/StakeButton.test.tsx b/app/components/UI/Stake/components/StakeButton/StakeButton.test.tsx index 2d4374e5c4b..2aada3df5b4 100644 --- a/app/components/UI/Stake/components/StakeButton/StakeButton.test.tsx +++ b/app/components/UI/Stake/components/StakeButton/StakeButton.test.tsx @@ -8,6 +8,8 @@ import { MOCK_STAKED_ETH_ASSET } from '../../__mocks__/mockData'; import { useMetrics } from '../../../../hooks/useMetrics'; import { MetricsEventBuilder } from '../../../../../core/Analytics/MetricsEventBuilder'; import { mockNetworkState } from '../../../../../util/test/network'; +import AppConstants from '../../../../../core/AppConstants'; +import useStakingEligibility from '../../hooks/useStakingEligibility'; const mockNavigate = jest.fn(); @@ -40,6 +42,7 @@ jest.mock('../../../../hooks/useMetrics'); jest.mock('../../../../../core/Engine', () => ({ context: { NetworkController: { + setActiveNetwork: jest.fn(() => Promise.resolve()), getNetworkClientById: () => ({ configuration: { chainId: '0x1', @@ -55,14 +58,21 @@ jest.mock('../../../../../core/Engine', () => ({ jest.mock('../../hooks/useStakingEligibility', () => ({ __esModule: true, - default: () => ({ + default: jest.fn(() => ({ isEligible: true, - loading: false, - error: null, - refreshPooledStakingEligibility: jest - .fn() - .mockResolvedValueOnce({ isEligible: true }), - }), + isLoadingEligibility: false, + refreshPooledStakingEligibility: jest.fn().mockResolvedValue({ + isEligible: true, + }), + error: false, + })), +})); + +jest.mock('../../hooks/useStakingChain', () => ({ + __esModule: true, + default: jest.fn(() => ({ + isStakingSupportedChain: true, + })), })); const STATE_MOCK = { @@ -77,9 +87,9 @@ const STATE_MOCK = { }, }; -const renderComponent = () => +const renderComponent = (state = STATE_MOCK) => renderWithProvider(, { - state: STATE_MOCK, + state, }); describe('StakeButton', () => { @@ -92,11 +102,62 @@ describe('StakeButton', () => { expect(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)).toBeDefined(); }); + it('navigates to Web view when stake button is pressed and user is not eligible', async () => { + (useStakingEligibility as jest.Mock).mockReturnValue({ + isEligible: false, + isLoadingEligibility: false, + refreshPooledStakingEligibility: jest + .fn() + .mockResolvedValue({ isEligible: false }), + error: false, + }); + const { getByTestId } = renderComponent(); + + fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); + await waitFor(() => { + expect(mockNavigate).toHaveBeenCalledWith(Routes.BROWSER.HOME, { + params: { + newTabUrl: `${AppConstants.STAKE.URL}?metamaskEntry=mobile`, + timestamp: expect.any(Number), + }, + screen: Routes.BROWSER.VIEW, + }); + }); + }); + it('navigates to Stake Input screen when stake button is pressed and user is eligible', async () => { + (useStakingEligibility as jest.Mock).mockReturnValue({ + isEligible: true, + isLoadingEligibility: false, + refreshPooledStakingEligibility: jest + .fn() + .mockResolvedValue({ isEligible: true }), + error: false, + }); const { getByTestId } = renderComponent(); fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); + await waitFor(() => { + expect(mockNavigate).toHaveBeenCalledWith('StakeScreens', { + screen: Routes.STAKING.STAKE, + }); + }); + }); + it('navigates to Stake Input screen when on unsupported network', async () => { + const UNSUPPORTED_NETWORK_STATE = { + engine: { + backgroundState: { + NetworkController: { + ...mockNetworkState({ + chainId: '0x89', // Polygon + }), + }, + }, + }, + }; + const { getByTestId } = renderComponent(UNSUPPORTED_NETWORK_STATE); + fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); await waitFor(() => { expect(mockNavigate).toHaveBeenCalledWith('StakeScreens', { screen: Routes.STAKING.STAKE, diff --git a/app/components/UI/Tokens/index.test.tsx b/app/components/UI/Tokens/index.test.tsx index 838143c7b21..a68c483475c 100644 --- a/app/components/UI/Tokens/index.test.tsx +++ b/app/components/UI/Tokens/index.test.tsx @@ -8,12 +8,9 @@ import { createStackNavigator } from '@react-navigation/stack'; import { getAssetTestId } from '../../../../wdio/screen-objects/testIDs/Screens/WalletView.testIds'; import { backgroundState } from '../../../util/test/initial-root-state'; import { strings } from '../../../../locales/i18n'; -import AppConstants from '../../../../app/core/AppConstants'; -import Routes from '../../../../app/constants/navigation/Routes'; import { WalletViewSelectorsIDs } from '../../../../e2e/selectors/wallet/WalletView.selectors'; import Engine from '../../../core/Engine'; import { createTokensBottomSheetNavDetails } from './TokensBottomSheet'; -import useStakingEligibility from '../Stake/hooks/useStakingEligibility'; // eslint-disable-next-line import/no-namespace import * as networks from '../../../util/networks'; // eslint-disable-next-line import/no-namespace @@ -414,54 +411,6 @@ describe('Tokens', () => { ).toBeDefined(); }); - it('renders stake button correctly', () => { - const { getByTestId } = renderComponent(initialState); - - expect(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)).toBeDefined(); - }); - - it('navigates to Web view when stake button is pressed and user is not eligible', async () => { - (useStakingEligibility as jest.Mock).mockReturnValue({ - isEligible: false, - isLoadingEligibility: false, - refreshPooledStakingEligibility: jest - .fn() - .mockResolvedValue({ isEligible: false }), - error: false, - }); - const { getByTestId } = renderComponent(initialState); - - fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); - await waitFor(() => { - expect(mockNavigate).toHaveBeenCalledWith(Routes.BROWSER.HOME, { - params: { - newTabUrl: `${AppConstants.STAKE.URL}?metamaskEntry=mobile`, - timestamp: 123, - }, - screen: Routes.BROWSER.VIEW, - }); - }); - }); - - it('navigates to Stake Input screen when stake button is pressed and user is eligible', async () => { - (useStakingEligibility as jest.Mock).mockReturnValue({ - isEligible: true, - isLoadingEligibility: false, - refreshPooledStakingEligibility: jest - .fn() - .mockResolvedValue({ isEligible: true }), - error: false, - }); - const { getByTestId } = renderComponent(initialState); - - fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); - await waitFor(() => { - expect(mockNavigate).toHaveBeenCalledWith('StakeScreens', { - screen: Routes.STAKING.STAKE, - }); - }); - }); - it('should refresh tokens and call necessary controllers', async () => { const { getByTestId } = renderComponent(initialState); @@ -513,48 +462,6 @@ describe('Tokens', () => { }); }); - it('navigates to Stake Input screen only when eligible', async () => { - (useStakingEligibility as jest.Mock).mockReturnValue({ - isEligible: true, - isLoadingEligibility: false, - refreshPooledStakingEligibility: jest - .fn() - .mockResolvedValue({ isEligible: true }), - error: false, - }); - - const { getByTestId } = renderComponent(initialState); - - fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); - - await waitFor(() => { - expect(mockNavigate).toHaveBeenCalledWith('StakeScreens', { - screen: Routes.STAKING.STAKE, - }); - }); - }); - - it('does not navigate to Stake Input screen if not eligible', async () => { - (useStakingEligibility as jest.Mock).mockReturnValue({ - isEligible: false, - isLoadingEligibility: false, - refreshPooledStakingEligibility: jest - .fn() - .mockResolvedValue({ isEligible: false }), - error: false, - }); - - const { getByTestId } = renderComponent(initialState); - - fireEvent.press(getByTestId(WalletViewSelectorsIDs.STAKE_BUTTON)); - - await waitFor(() => { - expect(mockNavigate).not.toHaveBeenCalledWith('StakeScreens', { - screen: Routes.STAKING.STAKE, - }); - }); - }); - it('calls onRefresh and updates state', async () => { const { getByTestId } = renderComponent(initialState);