forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-batch-create.test.ts
More file actions
105 lines (86 loc) · 3.47 KB
/
Copy pathuse-batch-create.test.ts
File metadata and controls
105 lines (86 loc) · 3.47 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, act } from '@testing-library/react'
const mockUseWallet = vi.hoisted(() => vi.fn(() => ({ address: 'GSENDER', isConnected: true })))
vi.mock('@/lib/contract', () => ({
createStream: vi.fn(),
createStreamsBatch: vi.fn(),
}))
vi.mock('@/hooks/use-wallet', () => ({
useWallet: mockUseWallet,
}))
vi.mock('@/components/providers/network-provider', () => ({
useNetwork: vi.fn(() => ({ network: 'testnet' })),
}))
vi.mock('@/hooks/use-streams', () => ({
invalidateStreams: vi.fn(),
}))
import { createStream, createStreamsBatch } from '@/lib/contract'
import { useBatchCreate, type BatchStreamInput } from '@/hooks/use-batch-create'
const TOKEN = { address: 'CUSDC', symbol: 'USDC', decimals: 7 }
const makeStream = (i = 0): BatchStreamInput => ({
recipient: `GRCPT${i}`,
token: TOKEN,
totalAmount: 1000n,
startTime: 0n,
endTime: 9999n,
cliffTime: 0n,
cliffAmount: 0n,
})
describe('useBatchCreate', () => {
beforeEach(() => {
vi.clearAllMocks()
mockUseWallet.mockReturnValue({ address: 'GSENDER', isConnected: true })
vi.mocked(createStream).mockResolvedValue('stream-id-1')
vi.mocked(createStreamsBatch).mockResolvedValue(['stream-id-1'])
})
it('throws when wallet not connected', async () => {
mockUseWallet.mockReturnValue({ address: null, isConnected: false } as any)
const { result } = renderHook(() => useBatchCreate())
await expect(
act(() => result.current.createBatch([makeStream()], { batchDelay: 0 }))
).rejects.toThrow('Wallet not connected')
})
it('throws for empty streams array', async () => {
const { result } = renderHook(() => useBatchCreate())
await expect(
act(() => result.current.createBatch([], { batchDelay: 0 }))
).rejects.toThrow('No streams to create')
})
it('throws when batch exceeds 100 streams', async () => {
const { result } = renderHook(() => useBatchCreate())
const streams = Array.from({ length: 101 }, (_, i) => makeStream(i))
await expect(
act(() => result.current.createBatch(streams, { batchDelay: 0 }))
).rejects.toThrow('Batch size exceeds maximum')
})
it('creates streams and tracks progress', async () => {
vi.mocked(createStreamsBatch).mockResolvedValue(['id-1', 'id-2'])
const { result } = renderHook(() => useBatchCreate())
let final: any
await act(async () => {
final = await result.current.createBatch([makeStream(0), makeStream(1)], { batchDelay: 0 })
})
expect(vi.mocked(createStreamsBatch)).toHaveBeenCalledTimes(1)
expect(final.completed).toBe(2)
expect(final.failed).toBe(0)
expect(final.successIds).toEqual(['id-1', 'id-2'])
expect(final.isRunning).toBe(false)
})
it('records errors for failed streams without stopping', async () => {
vi.mocked(createStreamsBatch).mockRejectedValueOnce(new Error('rejected'))
const { result } = renderHook(() => useBatchCreate())
let final: any
await act(async () => {
final = await result.current.createBatch([makeStream(0), makeStream(1)], { batchDelay: 0 })
})
expect(vi.mocked(createStreamsBatch)).toHaveBeenCalledTimes(1)
expect(final.completed).toBe(0)
expect(final.failed).toBe(2)
expect(final.errors.get(0)).toBe('rejected')
expect(final.errors.get(1)).toBe('rejected')
})
it('exposes a cancel function', () => {
const { result } = renderHook(() => useBatchCreate())
expect(typeof result.current.cancel).toBe('function')
})
})