-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
643 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
app/components/Views/NetworkSelector/RpcSelectionModal/RpcSelectionModal.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
144 changes: 144 additions & 0 deletions
144
app/components/Views/NetworkSelector/RpcSelectionModal/RpcSelectionModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.