Skip to content

Commit

Permalink
fix: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Nov 26, 2024
1 parent d6d57c3 commit 83504b2
Show file tree
Hide file tree
Showing 5 changed files with 420 additions and 0 deletions.
11 changes: 11 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
arguments=--init-script /var/folders/j9/20w1vr5s10v1y55vbf73rbx40000gn/T/db3b08fc4a9ef609cb16b96b200fa13e563f396e9bb1ed0905fdab7bc3bc513b.gradle --init-script /var/folders/j9/20w1vr5s10v1y55vbf73rbx40000gn/T/52cde0cfcf3e28b8b7510e992210d9614505e0911af0c190bd590d7158574963.gradle
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/Users/salim/Library/Java/JavaVirtualMachines/azul-16.0.2/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`NetworkAssetLogo Component matches the snapshot for non-mainnet 1`] = `null`;
73 changes: 73 additions & 0 deletions app/components/UI/NetworkAssetLogo/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import { render } from '@testing-library/react-native';
import NetworkAssetLogo from '.';
import TokenIcon from '../Swaps/components/TokenIcon';
import { ChainId } from '@metamask/controller-utils';

// Mock the TokenIcon component
jest.mock('../Swaps/components/TokenIcon', () => jest.fn(() => null));

describe('NetworkAssetLogo Component', () => {
it('matches the snapshot for non-mainnet', () => {
const { toJSON } = render(
<NetworkAssetLogo
chainId="42"
ticker="DAI"
style={{}}
big
biggest={false}
testID="network-asset-logo"
/>,
);

expect(toJSON()).toMatchSnapshot();
});

it('renders TokenIcon with ETH for mainnet chainId', () => {
const props = {
chainId: ChainId.mainnet,
ticker: 'TEST',
style: { width: 50, height: 50 },
big: true,
biggest: false,
testID: 'network-asset-logo',
};

render(<NetworkAssetLogo {...props} />);

expect(TokenIcon).toHaveBeenCalledWith(
{
big: props.big,
biggest: props.biggest,
symbol: 'ETH',
style: props.style,
testID: props.testID,
},
{},
);
});

it('renders TokenIcon with ticker for non-mainnet chainId', () => {
const props = {
chainId: '0x38', // Binance Smart Chain
ticker: 'BNB',
style: { width: 40, height: 40 },
big: false,
biggest: true,
testID: 'network-asset-logo',
};

render(<NetworkAssetLogo {...props} />);

expect(TokenIcon).toHaveBeenCalledWith(
{
big: props.big,
biggest: props.biggest,
symbol: props.ticker,
style: props.style,
testID: props.testID,
},
{},
);
});
});
196 changes: 196 additions & 0 deletions app/components/Views/AssetOptions/AssetOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { useNavigation } from '@react-navigation/native';
import { useSelector } from 'react-redux';
import AssetOptions from './AssetOptions';
import { strings } from '../../../../locales/i18n';

// Mock dependencies
jest.mock('@react-navigation/native', () => ({
useNavigation: jest.fn(),
}));

jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));

jest.mock('react-native-safe-area-context', () => ({
useSafeAreaInsets: jest.fn(() => ({ bottom: 10 })),
}));

jest.mock('../../../component-library/hooks', () => ({
useStyles: () => ({ styles: {} }),
}));

jest.mock('../../../components/hooks/useMetrics', () => ({
useMetrics: () => ({
trackEvent: jest.fn(),
isEnabled: jest.fn(() => true),
}),
}));

jest.mock('../../../components/UI/Swaps/utils/useBlockExplorer', () =>
jest.fn(() => ({
baseUrl: 'https://example-explorer.com',
token: (address: string) => `https://example-explorer.com/token/${address}`,
})),
);

jest.mock('../../../core/Engine', () => ({
context: {
TokensController: {
ignoreTokens: jest.fn(),
},
NetworkController: {
findNetworkClientIdByChainId: jest.fn(() => 'test-network'),
},
},
}));

jest.mock('../../../core/NotificationManager', () => ({
showSimpleNotification: jest.fn(),
}));

jest.mock('../../../util/Logger', () => ({
log: jest.fn(),
}));

jest.mock('../../../constants/navigation/Routes', () => ({
MODAL: {
ROOT_MODAL_FLOW: 'RootModalFlow',
},
BROWSER: {
HOME: 'BrowserHome',
VIEW: 'BrowserView',
},
}));

jest.mock('../../../selectors/networkController', () => ({
selectChainId: jest.fn(() => '1'),
selectProviderConfig: jest.fn(() => ({})),
selectNetworkConfigurations: jest.fn(() => ({})),
}));

jest.mock('../../../selectors/tokenListController', () => ({
selectTokenList: jest.fn(() => ({})),
}));

describe('AssetOptions Component', () => {
const mockNavigation = {
navigate: jest.fn(),
goBack: jest.fn(),
};

beforeEach(() => {
(useNavigation as jest.Mock).mockReturnValue(mockNavigation);
(useSelector as jest.Mock).mockImplementation((selector) => {
if (selector.name === 'selectChainId') return '1';
if (selector.name === 'selectProviderConfig') return {};
if (selector.name === 'selectTokenList')
return { '0x123': { symbol: 'ABC' } };
return {};
});
});

afterEach(() => {
jest.clearAllMocks();
});

it('matches the snapshot', () => {
const { toJSON } = render(
<AssetOptions
route={{
params: {
address: '0x123',
isNativeCurrency: false,
},
}}
/>,
);

expect(toJSON()).toMatchSnapshot();
});

it('renders correctly and displays options', () => {
const { getByText } = render(
<AssetOptions
route={{
params: {
address: '0x123',
isNativeCurrency: false,
},
}}
/>,
);

expect(
getByText(strings('asset_details.options.view_on_portfolio')),
).toBeTruthy();
expect(
getByText(strings('asset_details.options.view_on_block')),
).toBeTruthy();
expect(
getByText(strings('asset_details.options.token_details')),
).toBeTruthy();
expect(
getByText(strings('asset_details.options.remove_token')),
).toBeTruthy();
});

it('handles "View on Block Explorer" press', () => {
const { getByText } = render(
<AssetOptions
route={{
params: {
address: '0x123',
isNativeCurrency: false,
},
}}
/>,
);

fireEvent.press(getByText(strings('asset_details.options.view_on_block')));
expect(mockNavigation.navigate).toHaveBeenCalledWith('Webview', {
screen: 'SimpleWebview',
params: {
url: 'https://example-explorer.com/token/0x123',
title: 'example-explorer.com',
},
});
});

it('handles "Remove Token" press', () => {
const { getByText } = render(
<AssetOptions
route={{
params: {
address: '0x123',
isNativeCurrency: false,
},
}}
/>,
);

fireEvent.press(getByText(strings('asset_details.options.remove_token')));
expect(mockNavigation.navigate).toHaveBeenCalledWith('RootModalFlow', {
screen: 'AssetHideConfirmation',
params: expect.anything(),
});
});

it('handles "Token Details" press', () => {
const { getByText } = render(
<AssetOptions
route={{
params: {
address: '0x123',
isNativeCurrency: false,
},
}}
/>,
);

fireEvent.press(getByText(strings('asset_details.options.token_details')));
expect(mockNavigation.navigate).toHaveBeenCalledWith('AssetDetails');
});
});
Loading

0 comments on commit 83504b2

Please sign in to comment.