Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: reorganize stake button tests #13017

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading