|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { initBridge } from '../index'; |
| 3 | +import { Bridge } from '../bridge'; |
| 4 | +import { store } from '../store'; |
| 5 | +import { NetworkEnum, SignInDesignEnum, TChainId } from '@aelf-web-login/wallet-adapter-base'; |
| 6 | +import { ThemeType } from '@portkey/connect-web-wallet'; |
| 7 | + |
| 8 | +// Mock Bridge class |
| 9 | +vi.mock('../bridge', () => ({ |
| 10 | + Bridge: vi.fn().mockImplementation(() => ({ |
| 11 | + _wallets: [], |
| 12 | + _sideChainId: 'tDVV', |
| 13 | + theme: 'dark', |
| 14 | + _activeWallet: undefined, |
| 15 | + })), |
| 16 | +})); |
| 17 | + |
| 18 | +// Mock store |
| 19 | +vi.mock('../store', () => ({ |
| 20 | + store: { |
| 21 | + dispatch: vi.fn(), |
| 22 | + getState: vi.fn(), |
| 23 | + subscribe: vi.fn(), |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +describe('initBridge', () => { |
| 28 | + const mockWallets = [ |
| 29 | + { |
| 30 | + name: 'Portkey Web Wallet', |
| 31 | + icon: 'icon1', |
| 32 | + loginState: 'unlocked', |
| 33 | + wallet: {}, |
| 34 | + loginEagerly: vi.fn(), |
| 35 | + login: vi.fn(), |
| 36 | + logout: vi.fn(), |
| 37 | + connect: vi.fn(), |
| 38 | + disconnect: vi.fn(), |
| 39 | + signMessage: vi.fn(), |
| 40 | + signTransaction: vi.fn(), |
| 41 | + sendTransaction: vi.fn(), |
| 42 | + goAssets: vi.fn(), |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'Portkey Discover', |
| 46 | + icon: 'icon2', |
| 47 | + loginState: 'unlocked', |
| 48 | + wallet: {}, |
| 49 | + loginEagerly: vi.fn(), |
| 50 | + login: vi.fn(), |
| 51 | + logout: vi.fn(), |
| 52 | + connect: vi.fn(), |
| 53 | + disconnect: vi.fn(), |
| 54 | + signMessage: vi.fn(), |
| 55 | + signTransaction: vi.fn(), |
| 56 | + sendTransaction: vi.fn(), |
| 57 | + goAssets: vi.fn(), |
| 58 | + }, |
| 59 | + ] as any; |
| 60 | + |
| 61 | + const mockBaseConfig = { |
| 62 | + networkType: NetworkEnum.MAINNET, |
| 63 | + appName: 'Test App', |
| 64 | + chainId: 'AELF' as TChainId, |
| 65 | + sideChainId: 'tDVV' as TChainId, |
| 66 | + design: SignInDesignEnum.CryptoDesign, |
| 67 | + theme: 'dark' as ThemeType, |
| 68 | + showVconsole: false, |
| 69 | + omitTelegramScript: false, |
| 70 | + enableAcceleration: true, |
| 71 | + }; |
| 72 | + |
| 73 | + const mockConfig = { |
| 74 | + baseConfig: mockBaseConfig, |
| 75 | + wallets: mockWallets, |
| 76 | + }; |
| 77 | + |
| 78 | + beforeEach(() => { |
| 79 | + vi.clearAllMocks(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should initialize bridge with correct configuration', () => { |
| 83 | + const result = initBridge(mockConfig); |
| 84 | + |
| 85 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, mockBaseConfig); |
| 86 | + expect(result).toEqual({ |
| 87 | + instance: expect.any(Object), |
| 88 | + store: store, |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should initialize bridge with minimal configuration', () => { |
| 93 | + const minimalConfig = { |
| 94 | + baseConfig: { |
| 95 | + networkType: NetworkEnum.MAINNET, |
| 96 | + appName: 'Test App', |
| 97 | + chainId: 'AELF' as TChainId, |
| 98 | + sideChainId: 'tDVV' as TChainId, |
| 99 | + }, |
| 100 | + wallets: mockWallets, |
| 101 | + }; |
| 102 | + |
| 103 | + const result = initBridge(minimalConfig); |
| 104 | + |
| 105 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, minimalConfig.baseConfig); |
| 106 | + expect(result).toEqual({ |
| 107 | + instance: expect.any(Object), |
| 108 | + store: store, |
| 109 | + }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should initialize bridge with all optional parameters', () => { |
| 113 | + const fullConfig = { |
| 114 | + baseConfig: { |
| 115 | + ...mockBaseConfig, |
| 116 | + loginConfig: { |
| 117 | + loginMethods: ['Email', 'Phone'], |
| 118 | + } as any, |
| 119 | + showVconsole: true, |
| 120 | + omitTelegramScript: true, |
| 121 | + enableAcceleration: false, |
| 122 | + }, |
| 123 | + wallets: mockWallets, |
| 124 | + }; |
| 125 | + |
| 126 | + const result = initBridge(fullConfig); |
| 127 | + |
| 128 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, fullConfig.baseConfig); |
| 129 | + expect(result).toEqual({ |
| 130 | + instance: expect.any(Object), |
| 131 | + store: store, |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should handle empty wallets array', () => { |
| 136 | + const configWithEmptyWallets = { |
| 137 | + baseConfig: mockBaseConfig, |
| 138 | + wallets: [], |
| 139 | + }; |
| 140 | + |
| 141 | + const result = initBridge(configWithEmptyWallets); |
| 142 | + |
| 143 | + expect(Bridge).toHaveBeenCalledWith([], mockBaseConfig); |
| 144 | + expect(result).toEqual({ |
| 145 | + instance: expect.any(Object), |
| 146 | + store: store, |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should handle different network types', () => { |
| 151 | + const testnetConfig = { |
| 152 | + baseConfig: { |
| 153 | + ...mockBaseConfig, |
| 154 | + networkType: NetworkEnum.TESTNET, |
| 155 | + }, |
| 156 | + wallets: mockWallets, |
| 157 | + }; |
| 158 | + |
| 159 | + const result = initBridge(testnetConfig); |
| 160 | + |
| 161 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, testnetConfig.baseConfig); |
| 162 | + expect(result).toEqual({ |
| 163 | + instance: expect.any(Object), |
| 164 | + store: store, |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should handle different themes', () => { |
| 169 | + const lightThemeConfig = { |
| 170 | + baseConfig: { |
| 171 | + ...mockBaseConfig, |
| 172 | + theme: 'light' as ThemeType, |
| 173 | + }, |
| 174 | + wallets: mockWallets, |
| 175 | + }; |
| 176 | + |
| 177 | + const result = initBridge(lightThemeConfig); |
| 178 | + |
| 179 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, lightThemeConfig.baseConfig); |
| 180 | + expect(result).toEqual({ |
| 181 | + instance: expect.any(Object), |
| 182 | + store: store, |
| 183 | + }); |
| 184 | + }); |
| 185 | + |
| 186 | + it('should handle different sign-in designs', () => { |
| 187 | + const popupDesignConfig = { |
| 188 | + baseConfig: { |
| 189 | + ...mockBaseConfig, |
| 190 | + design: SignInDesignEnum.Web2Design, |
| 191 | + }, |
| 192 | + wallets: mockWallets, |
| 193 | + }; |
| 194 | + |
| 195 | + const result = initBridge(popupDesignConfig); |
| 196 | + |
| 197 | + expect(Bridge).toHaveBeenCalledWith(mockWallets, popupDesignConfig.baseConfig); |
| 198 | + expect(result).toEqual({ |
| 199 | + instance: expect.any(Object), |
| 200 | + store: store, |
| 201 | + }); |
| 202 | + }); |
| 203 | + |
| 204 | + it('should return correct interface structure', () => { |
| 205 | + const result = initBridge(mockConfig); |
| 206 | + |
| 207 | + expect(result).toHaveProperty('instance'); |
| 208 | + expect(result).toHaveProperty('store'); |
| 209 | + expect(typeof result.instance).toBe('object'); |
| 210 | + expect(result.store).toBe(store); |
| 211 | + }); |
| 212 | + |
| 213 | + it('should handle console.log call', () => { |
| 214 | + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 215 | + |
| 216 | + initBridge(mockConfig); |
| 217 | + |
| 218 | + expect(consoleSpy).toHaveBeenCalledWith('init bridge'); |
| 219 | + |
| 220 | + consoleSpy.mockRestore(); |
| 221 | + }); |
| 222 | +}); |
0 commit comments