-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into event_indexer_propose_events
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { Ethereum } from '@wagmi/core'; | ||
import type { Chain } from '../domain/chain'; | ||
import { ethers } from 'ethers'; | ||
import { switchEthereumChain } from './switchEthereumChain'; | ||
|
||
const ethereum = { | ||
request: jest.fn(), | ||
}; | ||
|
||
const chain = { | ||
id: 1, | ||
name: 'Ethereum Mainnet', | ||
rpc: 'rpc', | ||
explorerUrl: '', | ||
bridgeAddress: '', | ||
headerSyncAddress: '', | ||
} as Chain; | ||
|
||
describe('switchEthereumChain()', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it('should switchEthereumChain switches to the correct chain', async () => { | ||
await switchEthereumChain(ethereum as unknown as Ethereum, chain); | ||
expect(ethereum.request).toHaveBeenCalledWith({ | ||
method: 'wallet_switchEthereumChain', | ||
params: [{ chainId: '0x1' }], | ||
}); | ||
}); | ||
|
||
it('should addChain when ethereum thros chain not exist error', async () => { | ||
class EthereumError extends Error { | ||
code: number; | ||
constructor(code) { | ||
super(); | ||
this.code = code; | ||
} | ||
} | ||
|
||
ethereum.request.mockImplementationOnce(() => { | ||
const error = new EthereumError(4902); | ||
throw error; | ||
}); | ||
|
||
await switchEthereumChain(ethereum as unknown as Ethereum, chain); | ||
expect(ethereum.request).toHaveBeenCalledWith({ | ||
method: 'wallet_addEthereumChain', | ||
params: [ | ||
{ | ||
chainId: ethers.utils.hexValue(chain.id), | ||
chainName: chain.name, | ||
rpcUrls: [chain.rpc], | ||
nativeCurrency: { | ||
symbol: 'ETH', | ||
decimals: 18, | ||
name: 'Ethereum', | ||
}, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); |