Skip to content

Commit 2a301f7

Browse files
committed
chore: Add additional unit test coverage for getIsSwapsChain and getIsBridgeChain selectors
1 parent 5da7495 commit 2a301f7

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

ui/selectors/selectors.test.js

+162
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,7 @@ describe('#getConnectedSitesList', () => {
20482048
});
20492049

20502050
it('returns the token object for the overridden chainId when overrideChainId is provided', () => {
2051+
const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');
20512052
const expectedToken = {
20522053
symbol: 'POL',
20532054
name: 'Polygon',
@@ -2064,6 +2065,167 @@ describe('#getConnectedSitesList', () => {
20642065
);
20652066

20662067
expect(result).toStrictEqual(expectedToken);
2068+
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
2069+
});
2070+
});
2071+
2072+
describe('getIsSwapsChain', () => {
2073+
it('returns true for an allowed chainId in production environment', () => {
2074+
process.env.METAMASK_ENVIRONMENT = 'production';
2075+
2076+
const state = {
2077+
...mockState,
2078+
metamask: {
2079+
...mockState.metamask,
2080+
selectedNetworkClientId: 'testNetworkConfigurationId', // corresponds to mainnet RPC in mockState
2081+
},
2082+
};
2083+
2084+
const result = selectors.getIsSwapsChain(state);
2085+
2086+
expect(result).toBe(true);
2087+
});
2088+
2089+
it('returns true for an allowed chainId in development environment', () => {
2090+
process.env.METAMASK_ENVIRONMENT = 'development';
2091+
2092+
const state = {
2093+
...mockState,
2094+
metamask: {
2095+
...mockState.metamask,
2096+
selectedNetworkClientId: 'goerli',
2097+
},
2098+
};
2099+
2100+
const result = selectors.getIsSwapsChain(state);
2101+
2102+
expect(result).toBe(true);
2103+
});
2104+
2105+
it('returns false for a disallowed chainId in production environment', () => {
2106+
process.env.METAMASK_ENVIRONMENT = 'production';
2107+
2108+
const state = {
2109+
...mockState,
2110+
metamask: {
2111+
...mockState.metamask,
2112+
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
2113+
networkConfigurationsByChainId: {
2114+
'0x8080': {
2115+
chainId: '0x8080',
2116+
name: 'Custom Mainnet RPC',
2117+
nativeCurrency: 'ETH',
2118+
defaultRpcEndpointIndex: 0,
2119+
rpcEndpoints: [
2120+
{
2121+
type: 'custom',
2122+
url: 'https://testrpc.com',
2123+
networkClientId: 'fooChain',
2124+
},
2125+
],
2126+
},
2127+
},
2128+
},
2129+
};
2130+
2131+
const result = selectors.getIsSwapsChain(state);
2132+
2133+
expect(result).toBe(false);
2134+
});
2135+
2136+
it('returns false for a disallowed chainId in development environment', () => {
2137+
process.env.METAMASK_ENVIRONMENT = 'development';
2138+
2139+
const state = {
2140+
...mockState,
2141+
metamask: {
2142+
...mockState.metamask,
2143+
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
2144+
networkConfigurationsByChainId: {
2145+
'0x8080': {
2146+
chainId: '0x8080',
2147+
name: 'Custom Mainnet RPC',
2148+
nativeCurrency: 'ETH',
2149+
defaultRpcEndpointIndex: 0,
2150+
rpcEndpoints: [
2151+
{
2152+
type: 'custom',
2153+
url: 'https://testrpc.com',
2154+
networkClientId: 'fooChain',
2155+
},
2156+
],
2157+
},
2158+
},
2159+
},
2160+
};
2161+
2162+
const result = selectors.getIsSwapsChain(state);
2163+
2164+
expect(result).toBe(false);
2165+
});
2166+
2167+
it('respects the overrideChainId parameter', () => {
2168+
process.env.METAMASK_ENVIRONMENT = 'production';
2169+
2170+
const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');
2171+
2172+
const result = selectors.getIsSwapsChain(mockState, '0x89');
2173+
expect(result).toBe(true);
2174+
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
2175+
});
2176+
});
2177+
2178+
describe('getIsBridgeChain', () => {
2179+
it('returns true for an allowed bridge chainId', () => {
2180+
const state = {
2181+
...mockState,
2182+
metamask: {
2183+
...mockState.metamask,
2184+
selectedNetworkClientId: 'testNetworkConfigurationId', // corresponds to mainnet RPC in mockState
2185+
},
2186+
};
2187+
2188+
const result = selectors.getIsBridgeChain(state);
2189+
2190+
expect(result).toBe(true);
2191+
});
2192+
2193+
it('returns false for a disallowed bridge chainId', () => {
2194+
const state = {
2195+
...mockState,
2196+
metamask: {
2197+
...mockState.metamask,
2198+
selectedNetworkClientId: 'fooChain', // corresponds to mainnet RPC in mockState
2199+
networkConfigurationsByChainId: {
2200+
'0x8080': {
2201+
chainId: '0x8080',
2202+
name: 'Custom Mainnet RPC',
2203+
nativeCurrency: 'ETH',
2204+
defaultRpcEndpointIndex: 0,
2205+
rpcEndpoints: [
2206+
{
2207+
type: 'custom',
2208+
url: 'https://testrpc.com',
2209+
networkClientId: 'fooChain',
2210+
},
2211+
],
2212+
},
2213+
},
2214+
},
2215+
};
2216+
2217+
const result = selectors.getIsBridgeChain(state);
2218+
2219+
expect(result).toBe(false);
2220+
});
2221+
2222+
it('respects the overrideChainId parameter', () => {
2223+
const getCurrentChainIdSpy = jest.spyOn(selectors, 'getCurrentChainId');
2224+
2225+
const result = selectors.getIsBridgeChain(mockState, '0x89');
2226+
2227+
expect(result).toBe(true);
2228+
expect(getCurrentChainIdSpy).not.toHaveBeenCalled(); // Ensure overrideChainId is used
20672229
});
20682230
});
20692231
});

0 commit comments

Comments
 (0)