|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { expect, test, describe, vi, beforeEach, afterEach } from 'vitest'; |
| 6 | +import { createConfig } from '@wagmi/core'; |
| 7 | +import { polygonMumbai, arbitrumGoerli } from '@wagmi/chains'; |
| 8 | +import * as viem from 'viem'; |
| 9 | +import { normalizeChainId } from '@wagmi/core'; |
| 10 | +import { blocto } from './index'; |
| 11 | + |
| 12 | +vi.mock('viem'); |
| 13 | + |
| 14 | +describe('blocto-connector', () => { |
| 15 | + let connector: any; |
| 16 | + beforeEach(() => { |
| 17 | + const config = createConfig({ |
| 18 | + chains: [polygonMumbai, arbitrumGoerli], |
| 19 | + pollingInterval: 100, |
| 20 | + storage: null, |
| 21 | + transports: { |
| 22 | + [polygonMumbai.id]: viem.http(), |
| 23 | + [arbitrumGoerli.id]: viem.http(), |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + const connectorFn = blocto(); |
| 28 | + connector = config._internal.connectors.setup(connectorFn); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + connector = null; |
| 33 | + }); |
| 34 | + |
| 35 | + test('setup', () => { |
| 36 | + expect(connector.name).toEqual('Blocto'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('connect', async () => { |
| 40 | + const chainId = 1; |
| 41 | + const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a']; |
| 42 | + const provider = { |
| 43 | + request: vi.fn().mockResolvedValue(accounts), |
| 44 | + }; |
| 45 | + connector.getProvider = vi.fn().mockResolvedValue(provider); |
| 46 | + connector.getAccounts = vi.fn().mockResolvedValue(accounts); |
| 47 | + connector.getChainId = vi.fn().mockResolvedValue(chainId); |
| 48 | + |
| 49 | + const result = await connector.connect({ chainId }); |
| 50 | + |
| 51 | + expect(result).toEqual({ accounts, chainId }); |
| 52 | + expect(connector.getProvider).toHaveBeenCalledWith({ chainId }); |
| 53 | + expect(provider.request).toHaveBeenCalledWith({ |
| 54 | + method: 'eth_requestAccounts', |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + test('disconnect', async () => { |
| 59 | + const provider = { |
| 60 | + request: vi.fn().mockResolvedValue(undefined), |
| 61 | + }; |
| 62 | + connector.getProvider = vi.fn().mockResolvedValue(provider); |
| 63 | + |
| 64 | + await connector.disconnect(); |
| 65 | + |
| 66 | + expect(connector.getProvider).toHaveBeenCalled(); |
| 67 | + expect(provider.request).toHaveBeenCalledWith({ |
| 68 | + method: 'wallet_disconnect', |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + test('getAccounts', async () => { |
| 73 | + const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a']; |
| 74 | + const provider = { |
| 75 | + request: vi.fn().mockResolvedValue(accounts), |
| 76 | + }; |
| 77 | + connector.getProvider = vi.fn().mockResolvedValue(provider); |
| 78 | + vi.spyOn(viem, 'getAddress').mockImplementation((x) => x as `0x${string}`); |
| 79 | + |
| 80 | + const result = await connector.getAccounts(); |
| 81 | + |
| 82 | + expect(result).toEqual(['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a']); |
| 83 | + expect(connector.getProvider).toHaveBeenCalled(); |
| 84 | + expect(provider.request).toHaveBeenCalledWith({ method: 'eth_accounts' }); |
| 85 | + }); |
| 86 | + |
| 87 | + test('getChainId', async () => { |
| 88 | + const chainId = '0x1'; |
| 89 | + const provider = { |
| 90 | + chainId: undefined, |
| 91 | + request: vi.fn().mockResolvedValue(chainId), |
| 92 | + }; |
| 93 | + connector.getProvider = vi.fn().mockResolvedValue(provider); |
| 94 | + |
| 95 | + const result = await connector.getChainId(); |
| 96 | + |
| 97 | + expect(result).toEqual(normalizeChainId(chainId)); |
| 98 | + expect(connector.getProvider).toHaveBeenCalled(); |
| 99 | + expect(provider.request).toHaveBeenCalledWith({ method: 'eth_chainId' }); |
| 100 | + }); |
| 101 | + |
| 102 | + test('isAuthorized', async () => { |
| 103 | + const accounts = ['0xc61B4Aa62E5FD40cceB08C602Eb5D157b257b49a']; |
| 104 | + connector.getAccounts = vi.fn().mockResolvedValue(accounts); |
| 105 | + |
| 106 | + const result = await connector.isAuthorized(); |
| 107 | + |
| 108 | + expect(result).toEqual(false); |
| 109 | + }); |
| 110 | + |
| 111 | + test('switchChain', async () => { |
| 112 | + const chainId = arbitrumGoerli.id; |
| 113 | + const provider = { |
| 114 | + request: vi.fn().mockResolvedValue(undefined), |
| 115 | + supportChainList: vi.fn().mockResolvedValue( |
| 116 | + [polygonMumbai, arbitrumGoerli].map(({ id, name }) => ({ |
| 117 | + chainId: id, |
| 118 | + chainName: name, |
| 119 | + })) |
| 120 | + ), |
| 121 | + }; |
| 122 | + connector.getProvider = vi.fn().mockResolvedValue(provider); |
| 123 | + vi.spyOn(viem, 'numberToHex').mockReturnValue(viem.numberToHex(chainId)); |
| 124 | + |
| 125 | + const chain = await connector.switchChain({ chainId }); |
| 126 | + |
| 127 | + expect(connector.getProvider).toHaveBeenCalled(); |
| 128 | + expect(provider.request).toHaveBeenCalledWith({ |
| 129 | + method: 'wallet_addEthereumChain', |
| 130 | + params: [ |
| 131 | + { |
| 132 | + chainId: viem.numberToHex(chainId), |
| 133 | + rpcUrls: arbitrumGoerli.rpcUrls.default.http, |
| 134 | + }, |
| 135 | + ], |
| 136 | + }); |
| 137 | + expect(provider.request).toHaveBeenCalledWith({ |
| 138 | + method: 'wallet_switchEthereumChain', |
| 139 | + params: [ |
| 140 | + { |
| 141 | + chainId: viem.numberToHex(chainId), |
| 142 | + }, |
| 143 | + ], |
| 144 | + }); |
| 145 | + expect(chain.id).toEqual(chainId); |
| 146 | + }); |
| 147 | +}); |
0 commit comments