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

UX: Network Menu: Disable testnet toggle when on testnet #19951

Merged
merged 1 commit into from
Jul 14, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getCurrentChainId,
getNonTestNetworks,
getTestNetworks,
getCurrentNetwork,
} from '../../../selectors';
import ToggleButton from '../../ui/toggle-button';
import {
Expand Down Expand Up @@ -58,16 +57,15 @@ export const NetworkListMenu = ({ onClose }) => {

const nonTestNetworks = useSelector(getNonTestNetworks);
const testNetworks = useSelector(getTestNetworks);

const showTestNetworks = useSelector(getShowTestNetworks);
const currentChainId = useSelector(getCurrentChainId);

const currentNetwork = useSelector(getCurrentNetwork);

const dispatch = useDispatch();
const history = useHistory();
const trackEvent = useContext(MetaMetricsContext);

const currentlyOnTestNetwork = TEST_CHAINS.includes(currentChainId);

const environmentType = getEnvironmentType();
const isFullScreen = environmentType === ENVIRONMENT_TYPE_FULLSCREEN;

Expand All @@ -80,8 +78,8 @@ export const NetworkListMenu = ({ onClose }) => {
if (!lineaMainnetReleased && network.providerType === 'linea-mainnet') {
return null;
}
const isCurrentNetwork = currentNetwork.id === network.id;

const isCurrentNetwork = currentChainId === network.chainId;
const canDeleteNetwork =
!isCurrentNetwork && !UNREMOVABLE_CHAIN_IDS.includes(network.chainId);

Expand Down Expand Up @@ -155,6 +153,7 @@ export const NetworkListMenu = ({ onClose }) => {
<Text>{t('showTestnetNetworks')}</Text>
<ToggleButton
value={showTestNetworks}
disabled={currentlyOnTestNetwork}
onToggle={(value) => {
const shouldShowTestNetworks = !value;
dispatch(setShowTestNetworks(shouldShowTestNetworks));
Expand All @@ -167,7 +166,7 @@ export const NetworkListMenu = ({ onClose }) => {
}}
/>
</Box>
{showTestNetworks ? (
{showTestNetworks || currentlyOnTestNetwork ? (
<Box className="multichain-network-list-menu">
{generateMenuItems(testNetworks)}
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { fireEvent, renderWithProvider } from '../../../../test/jest';
import configureStore from '../../../store/store';
import mockState from '../../../../test/data/mock-state.json';
import {
CHAIN_IDS,
MAINNET_DISPLAY_NAME,
SEPOLIA_DISPLAY_NAME,
} from '../../../../shared/constants/network';
Expand All @@ -18,10 +19,14 @@ jest.mock('../../../store/actions.ts', () => ({
toggleNetworkMenu: () => mockToggleNetworkMenu,
}));

const render = (showTestNetworks = false) => {
const render = (showTestNetworks = false, currentChainId = '0x1') => {
const store = configureStore({
metamask: {
...mockState.metamask,
providerConfig: {
...mockState.metamask.providerConfig,
chainId: currentChainId,
},
preferences: {
showTestNetworks,
},
Expand Down Expand Up @@ -55,6 +60,11 @@ describe('NetworkListMenu', () => {
expect(mockSetShowTestNetworks).toHaveBeenCalled();
});

it('disables toggle when on test network', () => {
const { container } = render(false, CHAIN_IDS.GOERLI);
expect(container.querySelector('.toggle-button--disabled')).toBeDefined();
});

it('switches networks when an item is clicked', () => {
const { getByText } = render();
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
Expand Down