|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +import { type AccountState, ChainController, SIWXUtil } from '@reown/appkit-controllers' |
| 4 | + |
| 5 | +import { AppKit } from '../../src/client/appkit.js' |
| 6 | +import { mockOptions } from '../mocks/Options.js' |
| 7 | +import { |
| 8 | + mockBlockchainApiController, |
| 9 | + mockStorageUtil, |
| 10 | + mockWindowAndDocument |
| 11 | +} from '../test-utils.js' |
| 12 | + |
| 13 | +// Mock the SIWXUtil |
| 14 | +vi.mock('@reown/appkit-controllers', async () => { |
| 15 | + const actual = await vi.importActual('@reown/appkit-controllers') |
| 16 | + return { |
| 17 | + ...actual, |
| 18 | + SIWXUtil: { |
| 19 | + addEmbeddedWalletSession: vi.fn() |
| 20 | + } |
| 21 | + } |
| 22 | +}) |
| 23 | + |
| 24 | +describe('onAuthProviderConnected', () => { |
| 25 | + let appKit: AppKit |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + mockWindowAndDocument() |
| 29 | + mockStorageUtil() |
| 30 | + mockBlockchainApiController() |
| 31 | + vi.clearAllMocks() |
| 32 | + |
| 33 | + appKit = new AppKit(mockOptions) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('basic functionality', () => { |
| 37 | + it('should handle basic user connection without SIWX', async () => { |
| 38 | + const mockUser = { |
| 39 | + address: '0x1234567890123456789012345678901234567890', |
| 40 | + chainId: 'eip155:1', |
| 41 | + preferredAccountType: 'eoa', |
| 42 | + smartAccountDeployed: false, |
| 43 | + |
| 44 | + username: 'testuser' |
| 45 | + } |
| 46 | + |
| 47 | + const setCaipAddressSpy = vi.spyOn(appKit, 'setCaipAddress') |
| 48 | + const setUserSpy = vi.spyOn(appKit, 'setUser') |
| 49 | + const setSmartAccountDeployedSpy = vi.spyOn(appKit, 'setSmartAccountDeployed') |
| 50 | + const setPreferredAccountTypeSpy = vi.spyOn(appKit, 'setPreferredAccountType') |
| 51 | + const syncAuthConnectorThemeSpy = vi.spyOn(appKit as any, 'syncAuthConnectorTheme') |
| 52 | + const syncAccountSpy = vi.spyOn(appKit as any, 'syncAccount') |
| 53 | + const setLoadingSpy = vi.spyOn(appKit, 'setLoading') |
| 54 | + |
| 55 | + vi.spyOn(ChainController, 'getAccountData').mockReturnValue({ |
| 56 | + user: { accounts: [{ type: 'eoa', address: '0x1234567890123456789012345678901234567890' }] } |
| 57 | + } as AccountState) |
| 58 | + |
| 59 | + await appKit['onAuthProviderConnected'](mockUser) |
| 60 | + |
| 61 | + expect(setCaipAddressSpy).toHaveBeenCalled() |
| 62 | + expect(setUserSpy).toHaveBeenCalledWith( |
| 63 | + { |
| 64 | + accounts: [{ type: 'eoa', address: '0x1234567890123456789012345678901234567890' }], |
| 65 | + address: '0x1234567890123456789012345678901234567890', |
| 66 | + chainId: 'eip155:1', |
| 67 | + preferredAccountType: 'eoa', |
| 68 | + smartAccountDeployed: false, |
| 69 | + |
| 70 | + username: 'testuser' |
| 71 | + }, |
| 72 | + 'eip155' |
| 73 | + ) |
| 74 | + expect(setSmartAccountDeployedSpy).toHaveBeenCalledWith(false, 'eip155') |
| 75 | + expect(setPreferredAccountTypeSpy).toHaveBeenCalledWith('eoa', 'eip155') |
| 76 | + expect(syncAuthConnectorThemeSpy).toHaveBeenCalledWith(undefined) |
| 77 | + expect(syncAccountSpy).toHaveBeenCalledWith({ |
| 78 | + address: '0x1234567890123456789012345678901234567890', |
| 79 | + chainId: 'eip155:1', |
| 80 | + chainNamespace: 'eip155' |
| 81 | + }) |
| 82 | + expect(setLoadingSpy).toHaveBeenCalledWith(false, 'eip155') |
| 83 | + }) |
| 84 | + |
| 85 | + it('should handle user connection with SIWX data', async () => { |
| 86 | + const mockUser = { |
| 87 | + address: '0x1234567890123456789012345678901234567890', |
| 88 | + chainId: 'eip155:1', |
| 89 | + preferredAccountType: 'eoa', |
| 90 | + smartAccountDeployed: true, |
| 91 | + |
| 92 | + username: 'testuser', |
| 93 | + message: 'test message', |
| 94 | + signature: 'test signature', |
| 95 | + siwxMessage: { |
| 96 | + chainId: 'eip155:1', |
| 97 | + accountAddress: '0x1234567890123456789012345678901234567890', |
| 98 | + notBefore: '2023-01-01T00:00:00Z', |
| 99 | + statement: 'test statement', |
| 100 | + resources: ['test resource'], |
| 101 | + requestId: 'test-request-id', |
| 102 | + issuedAt: '2023-01-01T00:00:00Z', |
| 103 | + domain: 'test.com', |
| 104 | + uri: 'https://test.com', |
| 105 | + version: '1', |
| 106 | + nonce: 'test-nonce' |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const addEmbeddedWalletSessionSpy = vi.spyOn(SIWXUtil, 'addEmbeddedWalletSession') |
| 111 | + const setCaipAddressSpy = vi.spyOn(appKit, 'setCaipAddress') |
| 112 | + const setUserSpy = vi.spyOn(appKit, 'setUser') |
| 113 | + const setSmartAccountDeployedSpy = vi.spyOn(appKit, 'setSmartAccountDeployed') |
| 114 | + const setPreferredAccountTypeSpy = vi.spyOn(appKit, 'setPreferredAccountType') |
| 115 | + const syncAuthConnectorThemeSpy = vi.spyOn(appKit as any, 'syncAuthConnectorTheme') |
| 116 | + const syncAccountSpy = vi.spyOn(appKit as any, 'syncAccount') |
| 117 | + const setLoadingSpy = vi.spyOn(appKit, 'setLoading') |
| 118 | + |
| 119 | + vi.spyOn(ChainController, 'getAccountData').mockReturnValue({ |
| 120 | + user: { accounts: [{ type: 'eoa', address: '0x1234567890123456789012345678901234567890' }] } |
| 121 | + } as AccountState) |
| 122 | + |
| 123 | + await appKit['onAuthProviderConnected'](mockUser) |
| 124 | + |
| 125 | + expect(addEmbeddedWalletSessionSpy).toHaveBeenCalledWith( |
| 126 | + { |
| 127 | + chainId: 'eip155:1', |
| 128 | + accountAddress: '0x1234567890123456789012345678901234567890', |
| 129 | + notBefore: '2023-01-01T00:00:00Z', |
| 130 | + statement: 'test statement', |
| 131 | + resources: ['test resource'], |
| 132 | + requestId: 'test-request-id', |
| 133 | + issuedAt: '2023-01-01T00:00:00Z', |
| 134 | + domain: 'test.com', |
| 135 | + uri: 'https://test.com', |
| 136 | + version: '1', |
| 137 | + nonce: 'test-nonce' |
| 138 | + }, |
| 139 | + 'test message', |
| 140 | + 'test signature' |
| 141 | + ) |
| 142 | + expect(setCaipAddressSpy).toHaveBeenCalled() |
| 143 | + expect(setUserSpy).toHaveBeenCalledWith( |
| 144 | + { |
| 145 | + accounts: [{ type: 'eoa', address: '0x1234567890123456789012345678901234567890' }], |
| 146 | + address: '0x1234567890123456789012345678901234567890', |
| 147 | + chainId: 'eip155:1', |
| 148 | + preferredAccountType: 'eoa', |
| 149 | + smartAccountDeployed: true, |
| 150 | + |
| 151 | + username: 'testuser' |
| 152 | + }, |
| 153 | + 'eip155' |
| 154 | + ) |
| 155 | + expect(setSmartAccountDeployedSpy).toHaveBeenCalledWith(true, 'eip155') |
| 156 | + expect(setPreferredAccountTypeSpy).toHaveBeenCalledWith('eoa', 'eip155') |
| 157 | + expect(syncAuthConnectorThemeSpy).toHaveBeenCalledWith(undefined) |
| 158 | + expect(syncAccountSpy).toHaveBeenCalledWith({ |
| 159 | + address: '0x1234567890123456789012345678901234567890', |
| 160 | + chainId: 'eip155:1', |
| 161 | + chainNamespace: 'eip155' |
| 162 | + }) |
| 163 | + expect(setLoadingSpy).toHaveBeenCalledWith(false, 'eip155') |
| 164 | + }) |
| 165 | + }) |
| 166 | +}) |
0 commit comments