|
| 1 | +import { createDeferredPromise } from '@clerk/shared/utils'; |
| 2 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { useUserProfileWeb3WalletsController } from './user-profile-web3-wallets-section.controller'; |
| 6 | + |
| 7 | +const wallets = [{ id: 'wallet_1', address: '0x1234567890abcdef', isVerified: true }]; |
| 8 | +const availableProviders = [{ id: 'web3_metamask_signature', provider: 'MetaMask' }]; |
| 9 | + |
| 10 | +describe('Web3 wallet controller', () => { |
| 11 | + it('WEB3-06 holds a pending connection and blocks another attempt', async () => { |
| 12 | + const pending = createDeferredPromise(); |
| 13 | + const connect = vi.fn(() => pending.promise); |
| 14 | + const { result } = renderHook(() => |
| 15 | + useUserProfileWeb3WalletsController({ wallets, availableProviders, connect, setPrimary: vi.fn() }), |
| 16 | + ); |
| 17 | + |
| 18 | + act(() => { |
| 19 | + void result.current.onConnect('web3_metamask_signature'); |
| 20 | + void result.current.onConnect('web3_metamask_signature'); |
| 21 | + void result.current.onConnect('web3_solana_signature'); |
| 22 | + }); |
| 23 | + expect(result.current.pendingId).toBe('web3_metamask_signature'); |
| 24 | + expect(connect).toHaveBeenCalledOnce(); |
| 25 | + expect(result.current.solanaPickerOpen).toBe(false); |
| 26 | + |
| 27 | + await act(async () => { |
| 28 | + pending.resolve(); |
| 29 | + await pending.promise; |
| 30 | + }); |
| 31 | + await waitFor(() => expect(result.current.pendingId).toBeUndefined()); |
| 32 | + }); |
| 33 | + |
| 34 | + it('WEB3-07 surfaces connection errors on the provider and allows retry', async () => { |
| 35 | + const connect = vi |
| 36 | + .fn() |
| 37 | + .mockRejectedValueOnce(new Error('Wallet extension missing')) |
| 38 | + .mockResolvedValueOnce(undefined); |
| 39 | + const { result } = renderHook(() => |
| 40 | + useUserProfileWeb3WalletsController({ wallets, availableProviders, connect, setPrimary: vi.fn() }), |
| 41 | + ); |
| 42 | + |
| 43 | + await act(async () => result.current.onConnect('web3_metamask_signature')); |
| 44 | + expect(result.current.availableProviders[0].connectError).toBe('Wallet extension missing'); |
| 45 | + await act(async () => result.current.onConnect('web3_metamask_signature')); |
| 46 | + expect(result.current.availableProviders[0].connectError).toBeUndefined(); |
| 47 | + }); |
| 48 | + |
| 49 | + it('WEB3-08 surfaces primary selection errors on the wallet', async () => { |
| 50 | + const { result } = renderHook(() => |
| 51 | + useUserProfileWeb3WalletsController({ |
| 52 | + wallets, |
| 53 | + availableProviders, |
| 54 | + connect: vi.fn(), |
| 55 | + setPrimary: vi.fn().mockRejectedValue(new Error('Reverification canceled')), |
| 56 | + }), |
| 57 | + ); |
| 58 | + |
| 59 | + await act(async () => result.current.onSetPrimary('wallet_1')); |
| 60 | + expect(result.current.wallets[0].primaryError).toBe('Reverification canceled'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('WEB3-09 opens the Solana picker before connecting', () => { |
| 64 | + const connect = vi.fn(); |
| 65 | + const { result } = renderHook(() => |
| 66 | + useUserProfileWeb3WalletsController({ wallets, availableProviders, connect, setPrimary: vi.fn() }), |
| 67 | + ); |
| 68 | + act(() => void result.current.onConnect('web3_solana_signature')); |
| 69 | + expect(result.current.solanaPickerOpen).toBe(true); |
| 70 | + expect(connect).not.toHaveBeenCalled(); |
| 71 | + act(() => result.current.closeSolanaPicker()); |
| 72 | + expect(result.current.solanaPickerOpen).toBe(false); |
| 73 | + }); |
| 74 | +}); |
0 commit comments