Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(bridge-ui): switchEthereumChain test #13508

Merged
merged 9 commits into from
Apr 20, 2023
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',
},
},
],
});
});
});