Skip to content

Commit

Permalink
Merge branch 'main' into event_indexer_propose_events
Browse files Browse the repository at this point in the history
  • Loading branch information
dionysuzx authored Apr 20, 2023
2 parents a53dff4 + 43247d3 commit 897a72f
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions packages/bridge-ui/src/utils/switchEthereumChain.spec.ts
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',
},
},
],
});
});
});

0 comments on commit 897a72f

Please sign in to comment.