forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-streams.test.ts
More file actions
84 lines (71 loc) · 3.12 KB
/
Copy pathuse-streams.test.ts
File metadata and controls
84 lines (71 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'
import type { StreamData } from '@/types/stream'
// ── Mocks ──────────────────────────────────────────────────────────────────
vi.mock('@/lib/contract', () => ({
fetchStreamsForAddress: vi.fn(),
fetchStream: vi.fn(),
}))
vi.mock('@/hooks/use-wallet', () => ({
useWallet: vi.fn(() => ({ address: 'GABC123' })),
}))
vi.mock('@/components/providers/network-provider', () => ({
useNetwork: vi.fn(() => ({ network: 'testnet' })),
}))
import { fetchStreamsForAddress } from '@/lib/contract'
import { useWallet } from '@/hooks/use-wallet'
import { useStreams, invalidateStreams } from '@/hooks/use-streams'
const mockStream: StreamData = {
id: '1',
sender: 'GABC123',
recipient: 'GXYZ789',
token: { address: 'TOKEN', symbol: 'USDC', decimals: 7 },
depositedAmount: 1000n,
withdrawnAmount: 100n,
startTime: 0n,
endTime: 9999999999n,
cliffTime: 0n,
cliffAmount: 0n,
amountPerSecond: 1n,
linearAmount: 1000n,
duration: 9999999999n,
cancelled: false,
}
describe('useStreams', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(fetchStreamsForAddress).mockResolvedValue([mockStream])
})
it('fetches streams on mount', async () => {
const { result } = renderHook(() => useStreams({ enablePolling: false }))
expect(result.current.loading).toBe(true)
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.all).toHaveLength(1)
expect(fetchStreamsForAddress).toHaveBeenCalledWith('testnet', 'GABC123')
})
it('categorizes streams into sent and received', async () => {
const received: StreamData = { ...mockStream, id: '2', sender: 'GOTHER', recipient: 'GABC123' }
vi.mocked(fetchStreamsForAddress).mockResolvedValue([mockStream, received])
const { result } = renderHook(() => useStreams({ enablePolling: false }))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.sent).toHaveLength(1)
expect(result.current.received).toHaveLength(1)
})
it('returns empty arrays when wallet not connected', async () => {
vi.mocked(useWallet).mockReturnValue({ address: null } as any)
const { result } = renderHook(() => useStreams({ enablePolling: false }))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.all).toHaveLength(0)
})
it('handles fetch error gracefully', async () => {
vi.mocked(fetchStreamsForAddress).mockRejectedValue(new Error('Network error'))
const { result } = renderHook(() => useStreams({ enablePolling: false }))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.all).toHaveLength(0)
})
it('exposes a refetch function', async () => {
const { result } = renderHook(() => useStreams({ enablePolling: false }))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(typeof result.current.refetch).toBe('function')
})
})