forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-token-verification.test.ts
More file actions
78 lines (66 loc) · 3.05 KB
/
Copy pathuse-token-verification.test.ts
File metadata and controls
78 lines (66 loc) · 3.05 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
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'
vi.mock('@/lib/contract', () => ({
getTokenMetadata: vi.fn(),
}))
vi.mock('@/lib/stellar', () => ({
isVerifiedToken: vi.fn(() => false),
isFavoriteToken: vi.fn(() => false),
toggleFavoriteToken: vi.fn(),
}))
import { getTokenMetadata } from '@/lib/contract'
import { isVerifiedToken } from '@/lib/stellar'
import { useTokenVerification } from '@/hooks/use-token-verification'
const VALID_TOKEN = { address: 'CUSDC', symbol: 'USDC', decimals: 7 }
describe('useTokenVerification', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(getTokenMetadata).mockResolvedValue(VALID_TOKEN)
})
it('returns loading true while verifying', async () => {
const { result } = renderHook(() => useTokenVerification('CUSDC'))
expect(result.current.loading).toBe(true)
await waitFor(() => expect(result.current.loading).toBe(false))
})
it('returns valid metadata for a valid token', async () => {
const { result } = renderHook(() => useTokenVerification('CUSDC'))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.isValid).toBe(true)
expect(result.current.metadata).toEqual(VALID_TOKEN)
expect(result.current.error).toBeNull()
})
it('shows warning for unverified custom token', async () => {
vi.mocked(isVerifiedToken).mockReturnValue(false)
const { result } = renderHook(() => useTokenVerification('CUSTOM123'))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.isVerified).toBe(false)
expect(result.current.warning).toContain('not verified')
})
it('marks token as verified when in verified list', async () => {
vi.mocked(isVerifiedToken).mockReturnValue(true)
const { result } = renderHook(() => useTokenVerification('CUSDC'))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.isVerified).toBe(true)
expect(result.current.warning).toBeNull()
})
it('sets error when getTokenMetadata returns null', async () => {
vi.mocked(getTokenMetadata).mockResolvedValue(null)
const { result } = renderHook(() => useTokenVerification('INVALID'))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.isValid).toBe(false)
expect(result.current.error).toBeTruthy()
})
it('sets error when getTokenMetadata throws', async () => {
vi.mocked(getTokenMetadata).mockRejectedValue(new Error('RPC error'))
const { result } = renderHook(() => useTokenVerification('BADADDR'))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(result.current.isValid).toBe(false)
expect(result.current.error).toBe('RPC error')
})
it('does nothing for empty address', async () => {
const { result } = renderHook(() => useTokenVerification(''))
await waitFor(() => expect(result.current.loading).toBe(false))
expect(getTokenMetadata).not.toHaveBeenCalled()
expect(result.current.metadata).toBeNull()
})
})