Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit 3c38e53

Browse files
committed
feat: add test and comment loadSwitchableNetwork
1 parent e95c9b2 commit 3c38e53

File tree

5 files changed

+471
-5
lines changed

5 files changed

+471
-5
lines changed

packages/blocto-sdk/jest.config.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
'^.+\\.(ts|tsx)?$': 'ts-jest',
77
'^.+\\.(js|jsx)$': 'babel-jest',
88
},
9-
transformIgnorePatterns: ['node_modules/(?!variables/.*)'],
9+
testPathIgnorePatterns: ['node_modules/(?!variables/.*)', '/fixtures/'],
1010
automock: false,
1111
resetMocks: false,
1212
};
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import EthereumProvider from '../providers/ethereum';
2+
import { getEvmSupport } from '../lib/getEvmSupport';
3+
import { getEvmSupportList } from './fixtures/getEvmSupport';
4+
jest.mock('../lib/getEvmSupport');
5+
6+
describe('Testing BloctoSDK ethereum provider initialization and network loading', () => {
7+
beforeEach(() => {
8+
(getEvmSupport as jest.Mock).mockResolvedValue(getEvmSupportList);
9+
});
10+
11+
test('should initialize with unloadedNetwork', () => {
12+
const ethereumWithSwitchable = new EthereumProvider({
13+
defaultChainId: '0xaa36a7',
14+
switchableChains: [
15+
{
16+
chainId: '0xaa36a7',
17+
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
18+
},
19+
{
20+
chainId: '0x61',
21+
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
22+
},
23+
],
24+
});
25+
26+
expect(ethereumWithSwitchable['_blocto'].unloadedNetwork).toBeDefined();
27+
expect(
28+
ethereumWithSwitchable['_blocto']?.unloadedNetwork?.[0].chainId
29+
).toBe('0xaa36a7');
30+
expect(
31+
ethereumWithSwitchable['_blocto']?.unloadedNetwork?.[1].chainId
32+
).toBe('0x61');
33+
expect(ethereumWithSwitchable['_blocto'].unloadedNetwork?.length).toBe(2);
34+
});
35+
36+
test('get support chain list', async () => {
37+
const ethereum = new EthereumProvider({
38+
chainId: '0xaa36a7',
39+
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
40+
});
41+
const supportedChains = await ethereum.supportChainList();
42+
expect(supportedChains).toContainEqual({
43+
chainId: '11155111',
44+
chainName: 'Sepolia',
45+
});
46+
});
47+
48+
test('should add chain and switch to it', async () => {
49+
const ethereum = new EthereumProvider({
50+
chainId: '0xaa36a7',
51+
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
52+
});
53+
await expect(
54+
ethereum.request({
55+
method: 'wallet_switchEthereumChain',
56+
params: [{ chainId: '0x61' }],
57+
})
58+
).rejects.toThrow(
59+
'Unrecognized chain ID "97". Try adding the chain using wallet_addEthereumChain first.'
60+
);
61+
62+
await ethereum.request({
63+
method: 'wallet_addEthereumChain',
64+
params: [
65+
{
66+
chainId: '0x61',
67+
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
68+
},
69+
],
70+
});
71+
await ethereum.request({
72+
method: 'wallet_switchEthereumChain',
73+
params: [{ chainId: '0x61' }],
74+
});
75+
expect(ethereum.chainId).toBe('0x61');
76+
});
77+
78+
test('create sdk instance with switchableChains and switch to it', async () => {
79+
const ethereum = new EthereumProvider({
80+
defaultChainId: '0xaa36a7',
81+
switchableChains: [
82+
{
83+
chainId: '0xaa36a7',
84+
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
85+
},
86+
{
87+
chainId: '0x61',
88+
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
89+
},
90+
],
91+
});
92+
await ethereum.request({
93+
method: 'wallet_switchEthereumChain',
94+
params: [{ chainId: '0x61' }],
95+
});
96+
expect(ethereum.chainId).toBe('0x61');
97+
});
98+
test('create sdk instance with switchableChains and call eth_accounts', async () => {
99+
const ethereum = new EthereumProvider({
100+
defaultChainId: '0xaa36a7',
101+
switchableChains: [
102+
{
103+
chainId: '0xaa36a7',
104+
rpcUrls: ['https://ethereum-sepolia.blockpi.network/v1/rpc/public'],
105+
},
106+
{
107+
chainId: '0x61',
108+
rpcUrls: ['https://data-seed-prebsc-1-s1.binance.org:8545'],
109+
},
110+
],
111+
});
112+
// Trigger the loading of switchable networks
113+
await ethereum.request({
114+
method: 'eth_accounts',
115+
});
116+
expect(ethereum.chainId).toBe('0xaa36a7');
117+
// should remove unloadedNetwork after loading
118+
expect(ethereum['_blocto'].unloadedNetwork).toBeUndefined();
119+
});
120+
121+
test('should not call loadSwitchableNetwork if unloadedNetwork is empty', async () => {
122+
const ethereum = new EthereumProvider({
123+
chainId: '0xaa36a7',
124+
rpc: 'https://ethereum-sepolia.blockpi.network/v1/rpc/public',
125+
});
126+
const loadSwitchableNetworkSpy = jest.spyOn(ethereum, 'loadSwitchableNetwork');
127+
128+
await ethereum.request({ method: 'eth_accounts' });
129+
130+
expect(loadSwitchableNetworkSpy).not.toHaveBeenCalled();
131+
});
132+
});

0 commit comments

Comments
 (0)