forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard-stats.test.tsx
More file actions
169 lines (140 loc) · 6.24 KB
/
Copy pathdashboard-stats.test.tsx
File metadata and controls
169 lines (140 loc) · 6.24 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, within } from '@testing-library/react'
import { DashboardStats } from '@/components/streams/dashboard-stats'
import type { StreamData, TokenInfo } from '@/types/stream'
const NOW = 1_700_050_000
vi.mock('@/hooks/use-now', () => ({ useNow: vi.fn(() => NOW) }))
const showUsdMock = vi.fn()
vi.mock('@/hooks/use-show-usd', () => ({ useShowUsd: () => showUsdMock() }))
const usePortfolioValueMock = vi.fn()
vi.mock('@/hooks/use-token-price', async (importOriginal) => {
const actual = await importOriginal<typeof import('@/hooks/use-token-price')>()
return {
...actual,
usePortfolioValue: (streams: StreamData[]) => usePortfolioValueMock(streams),
}
})
const USDC: TokenInfo = { address: 'CUSDC', symbol: 'USDC', decimals: 6 }
const XLM: TokenInfo = { address: 'CXLM', symbol: 'XLM', decimals: 7 }
function makeStream(overrides: Partial<StreamData> = {}): StreamData {
return {
id: 'stream-1',
sender: 'GSENDER111',
recipient: 'GRECIPIENT222',
token: USDC,
depositedAmount: 1_000_000_000n,
withdrawnAmount: 0n,
startTime: BigInt(NOW - 1000),
endTime: BigInt(NOW + 1000),
cliffTime: BigInt(NOW - 1000),
cliffAmount: 0n,
amountPerSecond: 500_000n,
linearAmount: 1_000_000_000n,
duration: 2000n,
cancelled: false,
...overrides,
}
}
describe('DashboardStats', () => {
beforeEach(() => {
showUsdMock.mockReturnValue([false, vi.fn()])
usePortfolioValueMock.mockReturnValue({ totalUsd: null, loading: false, stale: false })
})
it('shows a dash and zero counts when there are no streams', () => {
render(<DashboardStats sent={[]} received={[]} />)
const withdraw = screen.getByTestId('stat-available-to-withdraw')
expect(within(withdraw).getByText('—')).toBeInTheDocument()
expect(within(withdraw).getByText('across received streams')).toBeInTheDocument()
const receiving = screen.getByTestId('stat-receiving')
expect(within(receiving).getByText('0')).toBeInTheDocument()
expect(within(receiving).getByText('0 streaming now')).toBeInTheDocument()
const sending = screen.getByTestId('stat-sending')
expect(within(sending).getByText('0')).toBeInTheDocument()
expect(within(sending).getByText('0 streaming now')).toBeInTheDocument()
})
it('reports sent and received counts and how many are actively streaming', () => {
const received = [
makeStream({ id: 'r1' }), // streaming
makeStream({ id: 'r2', startTime: BigInt(NOW + 500), endTime: BigInt(NOW + 1500) }), // scheduled
]
const sent = [
makeStream({ id: 's1' }), // streaming
makeStream({ id: 's2' }), // streaming
makeStream({ id: 's3', endTime: BigInt(NOW - 1) }), // completed
]
render(<DashboardStats sent={sent} received={received} />)
const receiving = screen.getByTestId('stat-receiving')
expect(within(receiving).getByText('2')).toBeInTheDocument()
expect(within(receiving).getByText('1 streaming now')).toBeInTheDocument()
const sending = screen.getByTestId('stat-sending')
expect(within(sending).getByText('3')).toBeInTheDocument()
expect(within(sending).getByText('2 streaming now')).toBeInTheDocument()
})
it('sums withdrawable amounts within a single token and formats them', () => {
// Each stream unlocks half of a 6-decimal 1,000 token deposit and has
// withdrawn nothing, so the pair should sum to "1,000 USDC".
const received = [
makeStream({ id: 'r1', startTime: BigInt(NOW - 1000), endTime: BigInt(NOW + 1000) }),
makeStream({ id: 'r2', startTime: BigInt(NOW - 1000), endTime: BigInt(NOW + 1000) }),
]
render(<DashboardStats sent={[]} received={received} />)
const withdraw = screen.getByTestId('stat-available-to-withdraw')
expect(within(withdraw).getByText('1,000')).toBeInTheDocument()
expect(within(withdraw).getByText('USDC')).toBeInTheDocument()
expect(within(withdraw).getByText('across received streams')).toBeInTheDocument()
})
it('shows the dominant token plus a count of additional tokens when multiple tokens are withdrawable', () => {
const received = [
makeStream({
id: 'r1',
token: USDC,
depositedAmount: 1_000_000_000n,
linearAmount: 1_000_000_000n,
duration: 1000n,
startTime: BigInt(NOW - 1000),
endTime: BigInt(NOW + 1000),
}),
makeStream({
id: 'r2',
token: XLM,
depositedAmount: 1_000_000_000n,
linearAmount: 1_000n,
duration: 1000n,
startTime: BigInt(NOW - 1000),
endTime: BigInt(NOW + 1000),
}),
]
render(<DashboardStats sent={[]} received={received} />)
const withdraw = screen.getByTestId('stat-available-to-withdraw')
expect(within(withdraw).getByText('USDC')).toBeInTheDocument()
expect(within(withdraw).getByText('+1 more token')).toBeInTheDocument()
})
it('shows zero available when the only received stream has not reached its cliff yet', () => {
const received = [
makeStream({
id: 'r1',
cliffTime: BigInt(NOW + 500),
startTime: BigInt(NOW - 500),
endTime: BigInt(NOW + 1500),
}),
]
render(<DashboardStats sent={[]} received={received} />)
const withdraw = screen.getByTestId('stat-available-to-withdraw')
expect(within(withdraw).getByText('0')).toBeInTheDocument()
expect(within(withdraw).getByText('USDC')).toBeInTheDocument()
})
it('shows the portfolio USD total when USD display is enabled', () => {
showUsdMock.mockReturnValue([true, vi.fn()])
usePortfolioValueMock.mockReturnValue({ totalUsd: 1234.5, loading: false, stale: false })
render(<DashboardStats sent={[]} received={[makeStream()]} />)
const total = screen.getByTestId('stat-total-streaming')
expect(within(total).getByText('$1,234.50')).toBeInTheDocument()
expect(within(total).getByText('locked across all streams')).toBeInTheDocument()
})
it('shows a dash and a settings hint when USD display is disabled', () => {
render(<DashboardStats sent={[]} received={[]} />)
const total = screen.getByTestId('stat-total-streaming')
expect(within(total).getByText('—')).toBeInTheDocument()
expect(within(total).getByText('enable USD in settings')).toBeInTheDocument()
})
})