1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 06:07:06 +01:00
metamask-extension/app/scripts/lib/rpc-method-middleware/handlers/switch-ethereum-chain.test.js

129 lines
4.2 KiB
JavaScript

import {
CHAIN_IDS,
NETWORK_TYPES,
} from '../../../../../shared/constants/network';
import switchEthereumChain from './switch-ethereum-chain';
const NON_INFURA_CHAIN_ID = '0x123456789';
const mockRequestUserApproval = ({ requestData }) => {
return Promise.resolve(requestData);
};
const MOCK_MAINNET_CONFIGURATION = {
id: 123,
chainId: CHAIN_IDS.MAINNET,
type: NETWORK_TYPES.MAINNET,
};
const MOCK_LINEA_MAINNET_CONFIGURATION = {
id: 123,
chainId: CHAIN_IDS.LINEA_MAINNET,
type: NETWORK_TYPES.LINEA_MAINNET,
};
describe('switchEthereumChainHandler', () => {
it('should call setProviderType when switching to a built in infura network', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.MAINNET }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_MAINNET_CONFIGURATION.type,
);
});
it('should call setProviderType when switching to a built in infura network, when chainId from request is lower case', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toLowerCase() }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_LINEA_MAINNET_CONFIGURATION.type,
);
});
it('should call setProviderType when switching to a built in infura network, when chainId from request is upper case', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: CHAIN_IDS.LINEA_MAINNET.toUpperCase() }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => NON_INFURA_CHAIN_ID,
findNetworkConfigurationBy: () => MOCK_LINEA_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetProviderType).toHaveBeenCalledTimes(1);
expect(mockSetProviderType).toHaveBeenCalledWith(
MOCK_LINEA_MAINNET_CONFIGURATION.type,
);
});
it('should call setActiveNetwork when switching to a custom network', async () => {
const mockSetProviderType = jest.fn();
const mockSetActiveNetwork = jest.fn();
const switchEthereumChainHandler = switchEthereumChain.implementation;
await switchEthereumChainHandler(
{
origin: 'example.com',
params: [{ chainId: NON_INFURA_CHAIN_ID }],
},
{},
jest.fn(),
jest.fn(),
{
getCurrentChainId: () => CHAIN_IDS.MAINNET,
findNetworkConfigurationBy: () => MOCK_MAINNET_CONFIGURATION,
setProviderType: mockSetProviderType,
setActiveNetwork: mockSetActiveNetwork,
requestUserApproval: mockRequestUserApproval,
},
);
expect(mockSetActiveNetwork).toHaveBeenCalledTimes(1);
expect(mockSetActiveNetwork).toHaveBeenCalledWith(
MOCK_MAINNET_CONFIGURATION.id,
);
});
});