Skip to content

Commit

Permalink
fix issue where setNetworkClientIdForDomain was called without checki…
Browse files Browse the repository at this point in the history
…ng whether the origin was eligible for setting its own network
  • Loading branch information
adonesky1 committed Aug 2, 2024
1 parent 5b56034 commit 5a94d97
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
14 changes: 10 additions & 4 deletions ui/components/multichain/network-list-menu/network-list-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
getUseRequestQueue,
getNetworkConfigurations,
getEditedNetwork,
getAllDomains,
} from '../../../selectors';
import ToggleButton from '../../ui/toggle-button';
import {
Expand Down Expand Up @@ -100,6 +101,7 @@ export const NetworkListMenu = ({ onClose }) => {
const selectedTabOrigin = useSelector(getOriginOfCurrentTab);
const useRequestQueue = useSelector(getUseRequestQueue);
const networkConfigurations = useSelector(getNetworkConfigurations);
const domains = useSelector(getAllDomains);

const dispatch = useDispatch();
const history = useHistory();
Expand Down Expand Up @@ -307,10 +309,14 @@ export const NetworkListMenu = ({ onClose }) => {
dispatch(setActiveNetwork(network.id));
}

// If presently on a dapp, communicate a change to
// the dapp via silent switchEthereumChain that the
// network has changed due to user action
if (useRequestQueue && selectedTabOrigin) {
// If presently on and connected to a dapp, communicate a change to
// the dapp via silent switchEthereumChain that the network has
// changed due to user action
if (
useRequestQueue &&
selectedTabOrigin &&
domains[selectedTabOrigin]
) {
setNetworkClientIdForDomain(selectedTabOrigin, network.id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const render = ({
providerConfigId = 'chain5',
isUnlocked = true,
origin = MOCK_ORIGIN,
selectedTabOriginInDomainsState = true,
} = {}) => {
const state = {
metamask: {
Expand All @@ -48,6 +49,11 @@ const render = ({
showTestNetworks,
},
useRequestQueue: true,
domains: {
...(selectedTabOriginInDomainsState
? { [origin]: providerConfigId }
: {}),
},
},
activeTab: {
origin,
Expand All @@ -61,6 +67,7 @@ const render = ({
describe('NetworkListMenu', () => {
beforeEach(() => {
process.env.ENABLE_NETWORK_UI_REDESIGN = 'false';
jest.clearAllMocks();
});

it('renders properly', () => {
Expand Down Expand Up @@ -158,22 +165,32 @@ describe('NetworkListMenu', () => {
).toHaveLength(0);
});

it('fires setNetworkClientIdForDomain when network item is clicked', () => {
const { getByText } = render();
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.MAINNET,
);
describe('selectedTabOrigin is connected to wallet', () => {
it('fires setNetworkClientIdForDomain when network item is clicked', () => {
const { getByText } = render();
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.MAINNET,
);
});

it('fires setNetworkClientIdForDomain when test network item is clicked', () => {
const { getByText } = render({ showTestNetworks: true });
fireEvent.click(getByText(SEPOLIA_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.SEPOLIA,
);
});
});

it('fires setNetworkClientIdForDomain when test network item is clicked', () => {
const { getByText } = render({ showTestNetworks: true });
fireEvent.click(getByText(SEPOLIA_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).toHaveBeenCalledWith(
MOCK_ORIGIN,
NETWORK_TYPES.SEPOLIA,
);
describe('selectedTabOrigin is not connected to wallet', () => {
it('does not fire setNetworkClientIdForDomain when network item is clicked', () => {
const { getByText } = render({ selectedTabOriginInDomainsState: false });
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
expect(mockSetNetworkClientIdForDomain).not.toHaveBeenCalled();
});
});

describe('NetworkListMenu with ENABLE_NETWORK_UI_REDESIGN', () => {
Expand Down

0 comments on commit 5a94d97

Please sign in to comment.