|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { |
| 4 | + WALLET_CONNECTION_STALL_TIMEOUT_MS, |
| 5 | + useWalletConnectionStallDetection, |
| 6 | +} from '@/hooks/useWalletConnectionStallDetection'; |
| 7 | + |
| 8 | +describe('useWalletConnectionStallDetection', () => { |
| 9 | + beforeEach(() => { |
| 10 | + vi.useFakeTimers(); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + vi.useRealTimers(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('reports a stalled wallet connection after the named timeout elapses', () => { |
| 18 | + const { result } = renderHook(() => |
| 19 | + useWalletConnectionStallDetection({ |
| 20 | + isAwaitingWalletResponse: true, |
| 21 | + }) |
| 22 | + ); |
| 23 | + |
| 24 | + expect(result.current).toBe(false); |
| 25 | + |
| 26 | + act(() => { |
| 27 | + vi.advanceTimersByTime(WALLET_CONNECTION_STALL_TIMEOUT_MS - 1); |
| 28 | + }); |
| 29 | + |
| 30 | + expect(result.current).toBe(false); |
| 31 | + |
| 32 | + act(() => { |
| 33 | + vi.advanceTimersByTime(1); |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result.current).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it('does not report a stall when the connection succeeds before the timeout', () => { |
| 40 | + const { result, rerender } = renderHook( |
| 41 | + ({ hasWalletResponse, isAwaitingWalletResponse }) => |
| 42 | + useWalletConnectionStallDetection({ |
| 43 | + hasWalletResponse, |
| 44 | + isAwaitingWalletResponse, |
| 45 | + }), |
| 46 | + { |
| 47 | + initialProps: { |
| 48 | + hasWalletResponse: false, |
| 49 | + isAwaitingWalletResponse: true, |
| 50 | + }, |
| 51 | + } |
| 52 | + ); |
| 53 | + |
| 54 | + act(() => { |
| 55 | + vi.advanceTimersByTime(WALLET_CONNECTION_STALL_TIMEOUT_MS / 2); |
| 56 | + }); |
| 57 | + |
| 58 | + rerender({ hasWalletResponse: true, isAwaitingWalletResponse: false }); |
| 59 | + |
| 60 | + act(() => { |
| 61 | + vi.advanceTimersByTime(WALLET_CONNECTION_STALL_TIMEOUT_MS); |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result.current).toBe(false); |
| 65 | + }); |
| 66 | + |
| 67 | + it('does not report a stall when the connection fails before the timeout', () => { |
| 68 | + const { result, rerender } = renderHook( |
| 69 | + ({ hasWalletResponse, isAwaitingWalletResponse }) => |
| 70 | + useWalletConnectionStallDetection({ |
| 71 | + hasWalletResponse, |
| 72 | + isAwaitingWalletResponse, |
| 73 | + }), |
| 74 | + { |
| 75 | + initialProps: { |
| 76 | + hasWalletResponse: false, |
| 77 | + isAwaitingWalletResponse: true, |
| 78 | + }, |
| 79 | + } |
| 80 | + ); |
| 81 | + |
| 82 | + rerender({ hasWalletResponse: true, isAwaitingWalletResponse: true }); |
| 83 | + |
| 84 | + act(() => { |
| 85 | + vi.advanceTimersByTime(WALLET_CONNECTION_STALL_TIMEOUT_MS); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(result.current).toBe(false); |
| 89 | + }); |
| 90 | +}); |
0 commit comments