Skip to content

Commit

Permalink
fix: fix refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Oct 16, 2024
1 parent 9cecfde commit 7684162
Show file tree
Hide file tree
Showing 5 changed files with 643 additions and 87 deletions.
96 changes: 9 additions & 87 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ import BottomSheetFooter from '../../../component-library/components/BottomSheet
import { ExtendedNetwork } from '../Settings/NetworksSettings/NetworkSettings/CustomNetworkView/CustomNetwork.types';
import { isNetworkUiRedesignEnabled } from '../../../util/networks/isNetworkUiRedesignEnabled';
import { Hex } from '@metamask/utils';
import ListItemSelect from '../../../component-library/components/List/ListItemSelect';
import hideProtocolFromUrl from '../../../util/hideProtocolFromUrl';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { LINEA_DEFAULT_RPC_URL } from '../../../constants/urls';
import { useNetworkInfo } from '../../../selectors/selectedNetworkController';
import { NetworkConfiguration } from '@metamask/network-controller';
import Logger from '../../../util/Logger';
import RpcSelectionModal from './RpcSelectionModal/RpcSelectionModal';

interface infuraNetwork {
name: string;
Expand Down Expand Up @@ -850,91 +850,6 @@ const NetworkSelector = () => {
onPress: () => confirmRemoveRpc(),
};

const renderBottomSheetRpc = useCallback(() => {
let imageSource;

if (showMultiRpcSelectModal.chainId === CHAIN_IDS.MAINNET) {
imageSource = images.ETHEREUM;
} else if (showMultiRpcSelectModal.chainId === CHAIN_IDS.LINEA_MAINNET) {
imageSource = images['LINEA-MAINNET'];
} else {
//@ts-expect-error - The utils/network file is still JS and this function expects a networkType, and should be optional
imageSource = getNetworkImageSource({
chainId: showMultiRpcSelectModal?.chainId?.toString(),
});
}

if (!showMultiRpcSelectModal.isVisible) return null;

const chainId = showMultiRpcSelectModal.chainId;

const rpcEndpoints =
networkConfigurations[chainId as `0x${string}`]?.rpcEndpoints || [];

return (
<BottomSheet
ref={rpcMenuSheetRef}
onClose={closeRpcModal}
shouldNavigateBack={false}
>
<BottomSheetHeader style={styles.baseHeader}>
<Text variant={TextVariant.HeadingMD}>
{strings('app_settings.select_rpc_url')}{' '}
</Text>
<Cell
variant={CellVariant.Display}
title={Networks.mainnet.name}
avatarProps={{
variant: AvatarVariant.Network,
name: showMultiRpcSelectModal.networkName,
imageSource,
size: AvatarSize.Sm,
style: { marginRight: 0 },
}}
style={styles.cellBorder}
>
<Text style={styles.alternativeText} variant={TextVariant.BodyMD}>
{showMultiRpcSelectModal.networkName}
</Text>
</Cell>
</BottomSheetHeader>
<View style={styles.rpcMenu}>
{rpcEndpoints.map(({ url, networkClientId }, index) => (
<ListItemSelect
key={index}
isSelected={
networkClientId ===
rpcEndpoints[
networkConfigurations[chainId as `0x${string}`]
.defaultRpcEndpointIndex
].networkClientId
}
isDisabled={false}
gap={8}
onPress={() => {
onRpcSelect(networkClientId, chainId as `0x${string}`);
closeRpcModal();
}}
>
<View style={styles.rpcText}>
<Text style={styles.textCentred}>
{hideKeyFromUrl(hideProtocolFromUrl(url))}
</Text>
</View>
</ListItemSelect>
))}
</View>
</BottomSheet>
);
}, [
showMultiRpcSelectModal,
rpcMenuSheetRef,
closeRpcModal,
styles,
networkConfigurations,
onRpcSelect,
]);

const renderBottomSheetContent = () => (
<>
<SheetHeader title={strings('networks.select_network')} />
Expand Down Expand Up @@ -1037,7 +952,14 @@ const NetworkSelector = () => {
</BottomSheet>
) : null}

{renderBottomSheetRpc()}
<RpcSelectionModal
showMultiRpcSelectModal={showMultiRpcSelectModal}
closeRpcModal={closeRpcModal}
onRpcSelect={onRpcSelect}
rpcMenuSheetRef={rpcMenuSheetRef}
networkConfigurations={networkConfigurations}
styles={styles}
/>

{showConfirmDeleteModal.isVisible ? (
<BottomSheet
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Third party dependencies.
import React from 'react';
import renderWithProvider from '../../../../util/test/renderWithProvider';

// Internal dependencies.
import RpcSelectionModal from './RpcSelectionModal';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import { NetworkConfiguration } from '@metamask/network-controller';

const MOCK_STORE_STATE = {
engine: {
backgroundState: {
NetworkController: {
networkConfigurations: {
[CHAIN_IDS.MAINNET]: {
rpcEndpoints: [
{
url: 'https://mainnet.infura.io/v3/{infuraProjectId}',
networkClientId: 'mainnet',
},
],
defaultRpcEndpointIndex: 0,
blockExplorerUrls: ['https://etherscan.io'],
chainId: CHAIN_IDS.MAINNET,
name: 'Ethereum Mainnet',
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18,
},
},
},
},
},
},
};

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

jest.mock('react-native-safe-area-context', () => {
// using disting digits for mock rects to make sure they are not mixed up
const inset = { top: 1, right: 2, bottom: 3, left: 4 };
const frame = { width: 5, height: 6, x: 7, y: 8 };
return {
SafeAreaProvider: jest.fn().mockImplementation(({ children }) => children),
SafeAreaConsumer: jest
.fn()
.mockImplementation(({ children }) => children(inset)),
useSafeAreaInsets: jest.fn().mockImplementation(() => inset),
useSafeAreaFrame: jest.fn().mockImplementation(() => frame),
};
});

jest.mock('@react-navigation/native', () => {
const actualReactNavigation = jest.requireActual('@react-navigation/native');
return {
...actualReactNavigation,
useNavigation: () => ({
navigate: jest.fn(),
setOptions: jest.fn(),
goBack: jest.fn(),
reset: jest.fn(),
}),
};
});

describe('RpcSelectionModal', () => {
// Fully mock rpcMenuSheetRef to match the BottomSheetRef type
const mockRpcMenuSheetRef = {
current: {
onOpenBottomSheet: jest.fn(),
onCloseBottomSheet: jest.fn(),
},
};

const defaultProps = {
showMultiRpcSelectModal: {
isVisible: true,
chainId: CHAIN_IDS.MAINNET,
networkName: 'Mainnet',
},
closeRpcModal: jest.fn(),
onRpcSelect: jest.fn(),
rpcMenuSheetRef: mockRpcMenuSheetRef,
networkConfigurations: MOCK_STORE_STATE.engine.backgroundState
.NetworkController.networkConfigurations as unknown as Record<
string,
NetworkConfiguration
>,

styles: {
baseHeader: {},
cellBorder: {},
rpcMenu: {},
rpcText: {},
textCentred: {},
alternativeText: {},
},
};

it('should render correctly', () => {
const { toJSON } = renderWithProvider(
<RpcSelectionModal {...defaultProps} />,
);
expect(toJSON()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import React, { FC, useCallback } from 'react';
import { View } from 'react-native';
import BottomSheet, {
BottomSheetRef,
} from '../../../../component-library/components/BottomSheets/BottomSheet';
import BottomSheetHeader from '../../../../component-library/components/BottomSheets/BottomSheetHeader';
import Text from '../../../../component-library/components/Texts/Text/Text';
import Cell, {
CellVariant,
} from '../../../../component-library/components/Cells/Cell';
import ListItemSelect from '../../../../component-library/components/List/ListItemSelect';
import {
AvatarSize,
AvatarVariant,
} from '../../../../component-library/components/Avatars/Avatar';
import { TextVariant } from '../../../../component-library/components/Texts/Text';
import Networks, { getNetworkImageSource } from '../../../../util/networks';
import { strings } from '../../../../../locales/i18n';
import { CHAIN_IDS } from '@metamask/transaction-controller';
import images from 'images/image-icons';
import hideProtocolFromUrl from '../../../../util/hideProtocolFromUrl';
import hideKeyFromUrl from '../../../..//util/hideKeyFromUrl';
import { NetworkConfiguration } from '@metamask/network-controller';

interface RpcSelectionModalProps {
showMultiRpcSelectModal: {
isVisible: boolean;
chainId: string;
networkName: string;
};
closeRpcModal: () => void;
onRpcSelect: (networkClientId: string, chainId: `0x${string}`) => void;
rpcMenuSheetRef: React.RefObject<BottomSheetRef>;
networkConfigurations: Record<string, NetworkConfiguration>;
// TODO: Replace "any" with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
styles: any;
}

const RpcSelectionModal: FC<RpcSelectionModalProps> = ({
showMultiRpcSelectModal,
closeRpcModal,
onRpcSelect,
rpcMenuSheetRef,
networkConfigurations,
styles,
}) => {
const renderRpcSelection = useCallback(() => {
let imageSource;

if (showMultiRpcSelectModal.chainId === CHAIN_IDS.MAINNET) {
imageSource = images.ETHEREUM;
} else if (showMultiRpcSelectModal.chainId === CHAIN_IDS.LINEA_MAINNET) {
imageSource = images['LINEA-MAINNET'];
} else {
//@ts-expect-error - The utils/network file is still JS and this function expects a networkType, and should be optional
imageSource = getNetworkImageSource({
chainId: showMultiRpcSelectModal?.chainId?.toString(),
});
}

if (!showMultiRpcSelectModal.isVisible) return null;

const chainId = showMultiRpcSelectModal.chainId;

const rpcEndpoints =
networkConfigurations[chainId as `0x${string}`]?.rpcEndpoints || [];

return (
<BottomSheet
ref={rpcMenuSheetRef}
onClose={closeRpcModal}
shouldNavigateBack={false}
>
<BottomSheetHeader style={styles.baseHeader}>
<Text variant={TextVariant.HeadingMD}>
{strings('app_settings.select_rpc_url')}{' '}
</Text>
<Cell
variant={CellVariant.Display}
title={Networks.mainnet.name}
avatarProps={{
variant: AvatarVariant.Network,
name: showMultiRpcSelectModal.networkName,
imageSource,
size: AvatarSize.Sm,
style: { marginRight: 0 },
}}
style={styles.cellBorder}
>
<Text style={styles.alternativeText} variant={TextVariant.BodyMD}>
{showMultiRpcSelectModal.networkName}
</Text>
</Cell>
</BottomSheetHeader>
<View style={styles.rpcMenu}>
{rpcEndpoints.map(
(
{
url,
networkClientId,
}: { url: string; networkClientId: string },
index: number,
) => (
<ListItemSelect
key={index}
isSelected={
networkClientId ===
rpcEndpoints[
networkConfigurations[chainId as `0x${string}`]
.defaultRpcEndpointIndex
].networkClientId
}
isDisabled={false}
gap={8}
onPress={() => {
onRpcSelect(networkClientId, chainId as `0x${string}`);
closeRpcModal();
}}
>
<View style={styles.rpcText}>
<Text style={styles.textCentred}>
{hideKeyFromUrl(hideProtocolFromUrl(url))}
</Text>
</View>
</ListItemSelect>
),
)}
</View>
</BottomSheet>
);
}, [
showMultiRpcSelectModal,
rpcMenuSheetRef,
closeRpcModal,
styles,
networkConfigurations,
onRpcSelect,
]);

return renderRpcSelection();
};

export default RpcSelectionModal;
Loading

0 comments on commit 7684162

Please sign in to comment.