|
| 1 | +import { act } from 'react' |
| 2 | +import useIsExpiredSwap from '@/features/swap/hooks/useIsExpiredSwap' |
| 3 | +import { renderHook } from '@/tests/test-utils' |
| 4 | +import * as guards from '@/utils/transaction-guards' |
| 5 | +import type { TransactionInfo } from '@safe-global/safe-gateway-typescript-sdk' |
| 6 | + |
| 7 | +describe('useIsExpiredSwap', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.useFakeTimers() |
| 10 | + jest.clearAllMocks() |
| 11 | + }) |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + jest.useRealTimers() |
| 15 | + }) |
| 16 | + |
| 17 | + it('returns false if txInfo is not a swap order', () => { |
| 18 | + jest.spyOn(guards, 'isSwapOrderTxInfo').mockReturnValue(false) |
| 19 | + |
| 20 | + const txInfo = {} as TransactionInfo |
| 21 | + const { result } = renderHook(() => useIsExpiredSwap(txInfo)) |
| 22 | + |
| 23 | + expect(result.current).toBe(false) |
| 24 | + }) |
| 25 | + |
| 26 | + it('returns true if the swap has already expired', () => { |
| 27 | + // Mock so that txInfo is considered a swap order |
| 28 | + jest.spyOn(guards, 'isSwapOrderTxInfo').mockReturnValue(true) |
| 29 | + |
| 30 | + const now = Date.now() |
| 31 | + const pastUnixTime = Math.floor((now - 1000) / 1000) // 1 second in the past |
| 32 | + const txInfo = { validUntil: pastUnixTime } as TransactionInfo |
| 33 | + |
| 34 | + const { result } = renderHook(() => useIsExpiredSwap(txInfo)) |
| 35 | + |
| 36 | + // Since expiry is in the past, should return true immediately |
| 37 | + expect(result.current).toBe(true) |
| 38 | + }) |
| 39 | + |
| 40 | + it('returns false initially and true after expiry time if the swap has not yet expired', () => { |
| 41 | + jest.spyOn(guards, 'isSwapOrderTxInfo').mockReturnValue(true) |
| 42 | + |
| 43 | + const now = Date.now() |
| 44 | + // set expiry 2 seconds in the future |
| 45 | + const futureUnixTime = Math.floor((now + 2000) / 1000) |
| 46 | + const txInfo = { validUntil: futureUnixTime } as TransactionInfo |
| 47 | + |
| 48 | + const { result, unmount } = renderHook(() => useIsExpiredSwap(txInfo)) |
| 49 | + |
| 50 | + // Initially should be false because it hasn't expired yet |
| 51 | + expect(result.current).toBe(false) |
| 52 | + |
| 53 | + // Advance time by 2 seconds to simulate waiting until expiry |
| 54 | + act(() => { |
| 55 | + jest.advanceTimersByTime(2000) |
| 56 | + }) |
| 57 | + |
| 58 | + // After the timer completes, it should become true |
| 59 | + expect(result.current).toBe(true) |
| 60 | + |
| 61 | + // Unmount to ensure cleanup runs without errors |
| 62 | + unmount() |
| 63 | + }) |
| 64 | + |
| 65 | + it('cancels the timeout when unmounted', () => { |
| 66 | + jest.spyOn(guards, 'isSwapOrderTxInfo').mockReturnValue(true) |
| 67 | + |
| 68 | + const now = Date.now() |
| 69 | + // set expiry 5 seconds in the future |
| 70 | + const futureUnixTime = Math.floor((now + 5000) / 1000) |
| 71 | + const txInfo = { validUntil: futureUnixTime } as TransactionInfo |
| 72 | + |
| 73 | + const { result, unmount } = renderHook(() => useIsExpiredSwap(txInfo)) |
| 74 | + expect(result.current).toBe(false) |
| 75 | + |
| 76 | + // Unmount the hook before the timer finishes |
| 77 | + unmount() |
| 78 | + |
| 79 | + // Advance time to ensure no setState runs after unmount |
| 80 | + act(() => { |
| 81 | + jest.advanceTimersByTime(5000) |
| 82 | + }) |
| 83 | + }) |
| 84 | + |
| 85 | + it('uses MAX_TIMEOUT if the validUntil value is too large', () => { |
| 86 | + jest.spyOn(guards, 'isSwapOrderTxInfo').mockReturnValue(true) |
| 87 | + |
| 88 | + const MAX_TIMEOUT = 2147483647 |
| 89 | + |
| 90 | + const now = Date.now() |
| 91 | + // Set validUntil so far in the future that timeUntilExpiry would exceed MAX_TIMEOUT |
| 92 | + const largeFutureTime = Math.floor((now + MAX_TIMEOUT + 10_000) / 1000) |
| 93 | + const txInfo = { validUntil: largeFutureTime } as TransactionInfo |
| 94 | + |
| 95 | + const { result } = renderHook(() => useIsExpiredSwap(txInfo)) |
| 96 | + expect(result.current).toBe(false) |
| 97 | + |
| 98 | + // The timeout should be capped at MAX_TIMEOUT |
| 99 | + // Advance time by MAX_TIMEOUT and check if expired is now true |
| 100 | + act(() => { |
| 101 | + jest.advanceTimersByTime(MAX_TIMEOUT) |
| 102 | + }) |
| 103 | + |
| 104 | + expect(result.current).toBe(true) |
| 105 | + }) |
| 106 | +}) |
0 commit comments