2023-03-31 19:58:25 +02:00
|
|
|
/* eslint-disable jest/require-top-level-describe */
|
|
|
|
import React from 'react';
|
|
|
|
import { fireEvent, renderWithProvider } from '../../../../test/jest';
|
|
|
|
import configureStore from '../../../store/store';
|
|
|
|
import mockState from '../../../../test/data/mock-state.json';
|
|
|
|
import {
|
2023-07-14 15:21:53 +02:00
|
|
|
CHAIN_IDS,
|
2023-03-31 19:58:25 +02:00
|
|
|
MAINNET_DISPLAY_NAME,
|
|
|
|
SEPOLIA_DISPLAY_NAME,
|
|
|
|
} from '../../../../shared/constants/network';
|
|
|
|
import { NetworkListMenu } from '.';
|
|
|
|
|
|
|
|
const mockSetShowTestNetworks = jest.fn();
|
|
|
|
const mockSetProviderType = jest.fn();
|
2023-04-13 18:54:03 +02:00
|
|
|
const mockToggleNetworkMenu = jest.fn();
|
2023-03-31 19:58:25 +02:00
|
|
|
jest.mock('../../../store/actions.ts', () => ({
|
|
|
|
setShowTestNetworks: () => mockSetShowTestNetworks,
|
|
|
|
setProviderType: () => mockSetProviderType,
|
2023-04-13 18:54:03 +02:00
|
|
|
toggleNetworkMenu: () => mockToggleNetworkMenu,
|
2023-03-31 19:58:25 +02:00
|
|
|
}));
|
|
|
|
|
2023-07-25 02:06:37 +02:00
|
|
|
const render = (
|
|
|
|
showTestNetworks = false,
|
|
|
|
currentChainId = '0x5',
|
|
|
|
providerConfigId = 'chain5',
|
|
|
|
) => {
|
|
|
|
const state = {
|
2023-03-31 19:58:25 +02:00
|
|
|
metamask: {
|
|
|
|
...mockState.metamask,
|
2023-07-14 15:21:53 +02:00
|
|
|
providerConfig: {
|
|
|
|
...mockState.metamask.providerConfig,
|
|
|
|
chainId: currentChainId,
|
2023-07-25 02:06:37 +02:00
|
|
|
id: providerConfigId,
|
2023-07-14 15:21:53 +02:00
|
|
|
},
|
2023-03-31 19:58:25 +02:00
|
|
|
preferences: {
|
|
|
|
showTestNetworks,
|
|
|
|
},
|
|
|
|
},
|
2023-07-25 02:06:37 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const store = configureStore(state);
|
2023-04-13 18:54:03 +02:00
|
|
|
return renderWithProvider(<NetworkListMenu onClose={jest.fn()} />, store);
|
2023-03-31 19:58:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('NetworkListMenu', () => {
|
|
|
|
it('displays important controls', () => {
|
|
|
|
const { getByText } = render();
|
|
|
|
|
|
|
|
expect(getByText('Add network')).toBeInTheDocument();
|
|
|
|
expect(getByText('Show test networks')).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders mainnet item', () => {
|
|
|
|
const { getByText } = render();
|
|
|
|
expect(getByText(MAINNET_DISPLAY_NAME)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('renders test networks when it should', () => {
|
|
|
|
const { getByText } = render(true);
|
|
|
|
expect(getByText(SEPOLIA_DISPLAY_NAME)).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggles showTestNetworks when toggle is clicked', () => {
|
|
|
|
const { queryAllByRole } = render();
|
|
|
|
const [testNetworkToggle] = queryAllByRole('checkbox');
|
|
|
|
fireEvent.click(testNetworkToggle);
|
|
|
|
expect(mockSetShowTestNetworks).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2023-07-14 15:21:53 +02:00
|
|
|
it('disables toggle when on test network', () => {
|
2023-07-25 02:06:37 +02:00
|
|
|
render(false, CHAIN_IDS.GOERLI);
|
|
|
|
expect(document.querySelector('.toggle-button--disabled')).toBeDefined();
|
2023-07-14 15:21:53 +02:00
|
|
|
});
|
|
|
|
|
2023-03-31 19:58:25 +02:00
|
|
|
it('switches networks when an item is clicked', () => {
|
|
|
|
const { getByText } = render();
|
|
|
|
fireEvent.click(getByText(MAINNET_DISPLAY_NAME));
|
2023-04-13 18:54:03 +02:00
|
|
|
expect(mockToggleNetworkMenu).toHaveBeenCalled();
|
2023-03-31 19:58:25 +02:00
|
|
|
expect(mockSetProviderType).toHaveBeenCalled();
|
|
|
|
});
|
2023-07-25 02:06:37 +02:00
|
|
|
|
|
|
|
it('shows the correct selected network when networks share the same chain ID', () => {
|
|
|
|
// Mainnet and Custom Mainnet RPC both use chain ID 0x1
|
|
|
|
render(false, '0x1', 'testNetworkConfigurationId');
|
|
|
|
|
|
|
|
// Contains Mainnet and the two custom networks
|
|
|
|
const networkItems = document.querySelectorAll(
|
|
|
|
'.multichain-network-list-item',
|
|
|
|
);
|
|
|
|
expect(networkItems).toHaveLength(3);
|
|
|
|
|
|
|
|
const selectedNodes = document.querySelectorAll(
|
|
|
|
'.multichain-network-list-item--selected',
|
|
|
|
);
|
|
|
|
expect(selectedNodes).toHaveLength(1);
|
|
|
|
|
|
|
|
const selectedNodeText = selectedNodes[0].querySelector(
|
|
|
|
'.multichain-network-list-item__network-name',
|
|
|
|
).textContent;
|
|
|
|
expect(selectedNodeText).toStrictEqual('Custom Mainnet RPC');
|
|
|
|
});
|
2023-03-31 19:58:25 +02:00
|
|
|
});
|