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

fix: fix duplicated network select #11524

Merged
merged 3 commits into from
Oct 2, 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
68 changes: 68 additions & 0 deletions app/components/Views/NetworkSelector/NetworkSelector.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ describe('Network Selector', () => {
expect(testNetworksSwitch.props.value).toBeTruthy();
expect(testNetworksSwitch.props.disabled).toBeTruthy();
});

it('changes to non infura network when another network cell is pressed', async () => {
const { getByText } = renderComponent(initialState);
const gnosisCell = getByText('Gnosis Chain');
Expand Down Expand Up @@ -238,6 +239,7 @@ describe('Network Selector', () => {

expect(mockEngine.context.NetworkController.setActiveNetwork).toBeCalled();
});

it('renders correctly with no network configurations', async () => {
(isNetworkUiRedesignEnabled as jest.Mock).mockImplementation(() => true);
const stateWithNoNetworkConfigurations = {
Expand Down Expand Up @@ -287,4 +289,70 @@ describe('Network Selector', () => {
fireEvent.press(rpcOption);
});
});

// Add this test for selecting between two Polygon networks
it('should select only one Polygon network when two networks with different RPC URLs exist', async () => {
jest.clearAllMocks(); // Clears mock data, ensuring that no mock has been called
jest.resetAllMocks(); // Resets mock implementation and mock instances

const customState = {
...initialState,
engine: {
backgroundState: {
...initialState.engine.backgroundState,
NetworkController: {
networkConfigurations: {
polygonNetwork1: {
chainId: '0x89', // Polygon Mainnet
nickname: 'Polygon Mainnet 1',
rpcUrl: 'https://polygon-mainnet-1.rpc',
ticker: 'POL',
},
polygonNetwork2: {
chainId: '0x89', // Polygon Mainnet (same chainId, different RPC URL)
nickname: 'Polygon Mainnet 2',
rpcUrl: 'https://polygon-mainnet-2.rpc',
ticker: 'POL',
},
},
},
},
},
};

(
Engine.context.NetworkController.getNetworkClientById as jest.Mock
).mockReturnValue({
configuration: {
chainId: '0x89', // Polygon Mainnet
nickname: 'Polygon Mainnet 1',
rpcUrl: 'https://polygon-mainnet-1.rpc',
ticker: 'POL',
type: 'custom',
},
});

const { getByText, queryByTestId } = renderComponent(customState);

// Ensure both networks are rendered
const polygonNetwork1 = getByText('Polygon Mainnet 1');
const polygonNetwork2 = getByText('Polygon Mainnet 2');
expect(polygonNetwork1).toBeTruthy();
expect(polygonNetwork2).toBeTruthy();

// Select the first network
fireEvent.press(polygonNetwork1);

// Wait for the selection to be applied
await waitFor(() => {
const polygonNetwork1Selected = queryByTestId(
'Polygon Mainnet 1-selected',
);
expect(polygonNetwork1Selected).toBeTruthy();
});

// Assert that the second network is NOT selected
const polygonNetwork2Selected = queryByTestId('Polygon Mainnet 2-selected');
expect(polygonNetwork2Selected).toBeNull(); // Not selected
});
});
15 changes: 11 additions & 4 deletions app/components/Views/NetworkSelector/NetworkSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ const NetworkSelector = () => {
imageSource: images['LINEA-MAINNET'],
size: avatarSize,
}}
isSelected={chainId === selectedChainId}
isSelected={chainId === selectedChainId && !providerConfig.rpcUrl}
onPress={() => onNetworkChange(LINEA_MAINNET)}
/>
);
Expand Down Expand Up @@ -520,7 +520,8 @@ const NetworkSelector = () => {

return (
<Cell
key={chainId}
key={`${chainId}-${rpcUrl}`}
testID={`network-cell-${name}`}
variant={CellVariant.Select}
title={name}
avatarProps={{
Expand All @@ -529,10 +530,16 @@ const NetworkSelector = () => {
imageSource: image,
size: avatarSize,
}}
isSelected={Boolean(chainId === selectedChainId && selectedRpcUrl)}
isSelected={Boolean(
chainId === selectedChainId && selectedRpcUrl === rpcUrl,
)}
onPress={() => onSetRpcTarget(rpcUrl)}
style={styles.networkCell}
/>
>
{Boolean(
chainId === selectedChainId && selectedRpcUrl === rpcUrl,
) && <View testID={`${name}-selected`} />}
</Cell>
);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ exports[`Network Selector renders correctly 1`] = `
"position": "relative",
}
}
testID="cellselect"
testID="network-cell-Avalanche Mainnet C-Chain"
>
<View
accessibilityRole="none"
Expand Down Expand Up @@ -808,7 +808,7 @@ exports[`Network Selector renders correctly 1`] = `
"position": "relative",
}
}
testID="cellselect"
testID="network-cell-Polygon Mainnet"
>
<View
accessibilityRole="none"
Expand Down Expand Up @@ -911,7 +911,7 @@ exports[`Network Selector renders correctly 1`] = `
"position": "relative",
}
}
testID="cellselect"
testID="network-cell-Optimism"
>
<View
accessibilityRole="none"
Expand Down Expand Up @@ -1014,7 +1014,7 @@ exports[`Network Selector renders correctly 1`] = `
"position": "relative",
}
}
testID="cellselect"
testID="network-cell-Gnosis Chain"
>
<View
accessibilityRole="none"
Expand Down
Loading