Skip to content

Commit

Permalink
chore: reorganize stake button tests
Browse files Browse the repository at this point in the history
This moves the stake button tests from Tokens test to StakeButton.test.tsx  for relevant code ownership.
This  also adds a test to navigate to stake screen when on unsupported network.
  • Loading branch information
amitabh94 committed Jan 15, 2025
1 parent 4dcc21f commit 33c2749
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -40,6 +42,7 @@ jest.mock('../../../../hooks/useMetrics');
jest.mock('../../../../../core/Engine', () => ({
context: {
NetworkController: {
setActiveNetwork: jest.fn(() => Promise.resolve()),
getNetworkClientById: () => ({
configuration: {
chainId: '0x1',
Expand All @@ -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 = {
Expand All @@ -77,9 +87,9 @@ const STATE_MOCK = {
},
};

const renderComponent = () =>
const renderComponent = (state = STATE_MOCK) =>
renderWithProvider(<StakeButton asset={MOCK_STAKED_ETH_ASSET} />, {
state: STATE_MOCK,
state,
});

describe('StakeButton', () => {
Expand All @@ -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,
Expand Down
93 changes: 0 additions & 93 deletions app/components/UI/Tokens/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 33c2749

Please sign in to comment.