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

refactor: remove global network usage from transaction simulation #12743

Merged
merged 2 commits into from
Dec 20, 2024
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = {
{
files: [
'app/components/UI/Name/**/*.{js,ts,tsx}',
'app/components/UI/SimulationDetails/**/*.{js,ts,tsx}',
'app/components/hooks/DisplayName/**/*.{js,ts,tsx}'
],
rules: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@ import AmountPill from './AmountPill';
import {
AssetIdentifier,
AssetType,
NATIVE_ASSET_IDENTIFIER,
NativeAssetIdentifier,
TokenAssetIdentifier,
} from '../types';

const TOKEN_ID_MOCK = '0xabc';
const CHAIN_ID_MOCK = '0x123';

const ERC20_ASSET_MOCK: TokenAssetIdentifier = {
type: AssetType.ERC20,
address: '0x456',
chainId: CHAIN_ID_MOCK,
};
const ERC721_ASSET_MOCK: TokenAssetIdentifier = {
type: AssetType.ERC721,
address: '0x123',
tokenId: TOKEN_ID_MOCK,
chainId: CHAIN_ID_MOCK,
};
const ERC1155_ASSET_MOCK: TokenAssetIdentifier = {
type: AssetType.ERC1155,
address: '0x789',
tokenId: TOKEN_ID_MOCK,
chainId: CHAIN_ID_MOCK,
};
const NATIVE_ASSET_MOCK: NativeAssetIdentifier = {
type: AssetType.Native,
chainId: CHAIN_ID_MOCK,
};

const renderAndExpect = (
Expand Down Expand Up @@ -83,7 +91,7 @@ describe('AmountPill', () => {
it.each(nativeAndErc20Cases)(
'renders the correct sign and amount for $amount',
({ amount, expected }) => {
renderAndExpect(NATIVE_ASSET_IDENTIFIER, amount, expected);
renderAndExpect(NATIVE_ASSET_MOCK, amount, expected);
},
);
});
Expand Down
51 changes: 31 additions & 20 deletions app/components/UI/SimulationDetails/AssetPill/AssetPill.test.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import React from 'react';
import { render } from '@testing-library/react-native';

import AssetPill from './AssetPill';
import {
selectChainId,
selectTicker,
} from '../../../../selectors/networkController';
import { AssetType, AssetIdentifier } from '../types';
import renderWithProvider from '../../../../util/test/renderWithProvider';
import { mockNetworkState } from '../../../../util/test/network';

jest.mock('react-redux', () => ({
...jest.requireActual('react-redux'),
useSelector: jest.fn().mockImplementation((selector) => selector()),
}));
jest.mock('../../../../selectors/networkController');
jest.mock(
'../../../../component-library/components/Avatars/Avatar/variants/AvatarNetwork',
() => 'AvatarNetwork',
Expand All @@ -22,18 +14,33 @@ jest.mock('../../../hooks/useStyles', () => ({
useStyles: () => ({ styles: {} }),
}));

describe('AssetPill', () => {
const selectChainIdMock = jest.mocked(selectChainId);
const selectTickerMock = jest.mocked(selectTicker);
const CHAIN_ID_MOCK = '0x123';

beforeAll(() => {
selectChainIdMock.mockReturnValue('0x1');
selectTickerMock.mockReturnValue('ETH');
});
const STATE_MOCK = {
engine: {
backgroundState: {
NetworkController: {
...mockNetworkState({
chainId: CHAIN_ID_MOCK,
}),
},
},
},
};

describe('AssetPill', () => {
it('renders correctly for native assets', () => {
const asset = { type: AssetType.Native } as AssetIdentifier;
const { getByText, getByTestId } = render(<AssetPill asset={asset} />);
const asset = {
type: AssetType.Native,
chainId: CHAIN_ID_MOCK,
} as AssetIdentifier;

const { getByText, getByTestId } = renderWithProvider(
<AssetPill asset={asset} />,
{
state: STATE_MOCK,
},
);

expect(
getByTestId('simulation-details-asset-pill-avatar-network'),
Expand All @@ -45,8 +52,12 @@ describe('AssetPill', () => {
const asset = {
type: AssetType.ERC20,
address: '0xabc123',
chainId: CHAIN_ID_MOCK,
} as AssetIdentifier;
const { getByTestId } = render(<AssetPill asset={asset} />);

const { getByTestId } = renderWithProvider(<AssetPill asset={asset} />, {
state: STATE_MOCK,
});

expect(getByTestId('simulation-details-asset-pill-name')).toBeTruthy();
});
Expand Down
29 changes: 14 additions & 15 deletions app/components/UI/SimulationDetails/AssetPill/AssetPill.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@ import Text, {
} from '../../../../component-library/components/Texts/Text';
import AvatarNetwork from '../../../../component-library/components/Avatars/Avatar/variants/AvatarNetwork';
import { AvatarSize } from '../../../../component-library/components/Avatars/Avatar/Avatar.types';
import {
selectChainId,
selectTicker,
} from '../../../../selectors/networkController';
import { NetworkList } from '../../../../util/networks';
import { useStyles } from '../../../hooks/useStyles';
import Name from '../../Name/Name';
import { NameType } from '../../Name/Name.types';
import { AssetIdentifier, AssetType } from '../types';
import styleSheet from './AssetPill.styles';
import { selectNetworkConfigurations } from '../../../../selectors/networkController';

interface AssetPillProperties extends ViewProps {
asset: AssetIdentifier;
Expand All @@ -35,42 +32,44 @@ const getNetworkImage = (chainId: Hex) => {
return network?.imageSource || null;
};

const NativeAssetPill: React.FC = () => {
const NativeAssetPill: React.FC<AssetPillProperties> = ({ asset }) => {
const { styles } = useStyles(styleSheet, {});
const ticker = useSelector(selectTicker);
const chainId = useSelector(selectChainId);
const imageSource = getNetworkImage(chainId);
const imageSource = getNetworkImage(asset.chainId);

const networkConfigurationsByChainId = useSelector(
selectNetworkConfigurations,
);

const { nativeCurrency } =
networkConfigurationsByChainId[asset.chainId] || {};

return (
<View style={styles.nativeAssetPill}>
<AvatarNetwork
testID="simulation-details-asset-pill-avatar-network"
size={AvatarSize.Xs}
name={ticker}
name={nativeCurrency}
imageSource={imageSource}
/>
<Text variant={TextVariant.BodyMD}>{ticker}</Text>
<Text variant={TextVariant.BodyMD}>{nativeCurrency}</Text>
</View>
);
};

const AssetPill: React.FC<AssetPillProperties> = ({ asset }) => {
const { styles } = useStyles(styleSheet, {});

// TODO: Remove global network selector usage once simulations refactored.
const chainId = useSelector(selectChainId);

return (
<View style={styles.assetPill}>
{asset.type === AssetType.Native ? (
<NativeAssetPill />
<NativeAssetPill asset={asset} />
) : (
<Name
preferContractSymbol
testID="simulation-details-asset-pill-name"
type={NameType.EthereumAddress}
value={asset.address}
variation={chainId}
variation={asset.chainId}
/>
)}
</View>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ jest.mock('../FiatDisplay/FiatDisplay', () => ({
TotalFiatDisplay: 'TotalFiatDisplay',
}));

const CHAIN_ID_MOCK = '0x123';

const balanceChangesMock = [
{
asset: {
type: 'ERC20',
address: '0xabc123',
chainId: CHAIN_ID_MOCK,
},
amount: new BigNumber(100),
fiatAmount: 100,
Expand Down Expand Up @@ -68,6 +71,7 @@ describe('BalanceChangeList', () => {
asset: {
type: 'ERC20',
address: '0xabc123',
chainId: CHAIN_ID_MOCK,
},
amount: new BigNumber(100),
fiatAmount: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ import { BigNumber } from 'bignumber.js';
import BalanceChangeRow from './BalanceChangeRow';
import { AssetType, BalanceChange } from '../types';

jest.mock('../AmountPill/AmountPill', () => 'AmountPill');
jest.mock('../AssetPill/AssetPill', () => 'AssetPill');
jest.mock('../FiatDisplay/FiatDisplay', () => ({
IndividualFiatDisplay: 'IndividualFiatDisplay',
}));

const CHAIN_ID_MOCK = '0x123';

const balanceChangeMock: BalanceChange = {
asset: {
type: AssetType.ERC20,
address: '0xabc123',
chainId: CHAIN_ID_MOCK,
},
amount: new BigNumber(100),
fiatAmount: 0,
} as BalanceChange;

jest.mock('../AmountPill/AmountPill', () => 'AmountPill');
jest.mock('../AssetPill/AssetPill', () => 'AssetPill');

describe('BalanceChangeList', () => {
it('renders a balance change row', () => {
const { getByText, getByTestId } = render(
Expand Down
Loading
Loading