Skip to content

Commit a27fe24

Browse files
committed
test
1 parent f93eb43 commit a27fe24

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { renderHook, waitFor } from '@testing-library/react';
3+
import { useBalance, useBalances } from 'typink';
4+
import { ALICE, BOB, CHARLIE, wrapper } from '../utils';
5+
6+
describe('useBalance', () => {
7+
it('should load balance properly', async () => {
8+
const { result } = renderHook(() => useBalance(ALICE), { wrapper });
9+
10+
// Initially, the balance should be undefined
11+
expect(result.current).toBeUndefined();
12+
13+
// Wait for the balance to be fetched
14+
await waitFor(() => {
15+
expect(result.current).toBeDefined();
16+
});
17+
18+
// After fetching, the free balance should be a positive bigint
19+
expect(result.current?.free).toBeGreaterThan(0n);
20+
});
21+
22+
it('should update balance when address changes', async () => {
23+
const { result, rerender } = renderHook(({ address }) => useBalance(address), {
24+
wrapper,
25+
initialProps: { address: ALICE },
26+
});
27+
28+
// Wait for ALICE's balance to be fetched
29+
await waitFor(() => {
30+
expect(result.current).toBeDefined();
31+
});
32+
33+
const aliceBalance = result.current;
34+
35+
const address = '5DFdEZVVJyT7Bz1XMznaXxPeRUTfNn2mhbKmzMnKdMfFpECD';
36+
rerender({ address });
37+
38+
// Initially, the balance should be undefined again
39+
expect(result.current).toBeUndefined();
40+
41+
// Wait for BOB's balance to be fetched
42+
await waitFor(() => {
43+
expect(result.current).toBeDefined();
44+
});
45+
46+
// BOB's balance should be different from ALICE's
47+
expect(result.current?.free).toEqual(0n);
48+
expect(result.current).not.toEqual(aliceBalance);
49+
});
50+
51+
it('should return undefined for invalid address', async () => {
52+
const { result } = renderHook(() => useBalance('invalid_address'), { wrapper });
53+
54+
// The balance should remain undefined
55+
await waitFor(
56+
() => {
57+
expect(result.current).toBeUndefined();
58+
},
59+
{ timeout: 5000 },
60+
);
61+
});
62+
63+
it('should return correct balance properties', async () => {
64+
const { result } = renderHook(() => useBalance(ALICE), { wrapper });
65+
66+
await waitFor(() => {
67+
expect(result.current).toBeDefined();
68+
});
69+
70+
expect(result.current).toHaveProperty('free');
71+
expect(result.current).toHaveProperty('reserved');
72+
expect(result.current).toHaveProperty('frozen');
73+
74+
expect(typeof result.current?.free).toBe('bigint');
75+
expect(typeof result.current?.reserved).toBe('bigint');
76+
expect(typeof result.current?.frozen).toBe('bigint');
77+
});
78+
});
79+
80+
describe('useBalances', () => {
81+
it('should load balances properly', async () => {
82+
const addresses = [ALICE, BOB];
83+
const { result } = renderHook(() => useBalances(addresses), { wrapper });
84+
85+
// Initially, the balances should be an empty object
86+
expect(result.current).toEqual({});
87+
88+
// Wait for the balances to be fetched
89+
await waitFor(() => {
90+
expect(Object.keys(result.current).length).toBe(2);
91+
});
92+
93+
// After fetching, both addresses should have positive free balances
94+
expect(result.current[ALICE].free).toBeGreaterThan(0n);
95+
expect(result.current[BOB].free).toBeGreaterThan(0n);
96+
});
97+
98+
it('should update balances when addresses change', async () => {
99+
const { result, rerender } = renderHook(({ addresses }) => useBalances(addresses), {
100+
wrapper,
101+
initialProps: { addresses: [ALICE, BOB] },
102+
});
103+
104+
// Wait for initial balances to be fetched
105+
await waitFor(() => {
106+
expect(Object.keys(result.current).length).toBe(2);
107+
});
108+
109+
// Change addresses
110+
rerender({ addresses: [ALICE, BOB, CHARLIE] });
111+
112+
// Wait for new balances to be fetched
113+
await waitFor(() => {
114+
expect(Object.keys(result.current).length).toBe(3);
115+
});
116+
117+
expect(result.current[ALICE]).toBeDefined();
118+
expect(result.current[BOB]).toBeDefined();
119+
expect(result.current[CHARLIE]).toBeDefined();
120+
});
121+
122+
it('should return correct balance properties for multiple addresses', async () => {
123+
const addresses = [ALICE, BOB, CHARLIE];
124+
const { result } = renderHook(() => useBalances(addresses), { wrapper });
125+
126+
await waitFor(() => {
127+
expect(Object.keys(result.current).length).toBe(3);
128+
});
129+
130+
for (const address of addresses) {
131+
expect(result.current[address]).toHaveProperty('free');
132+
expect(result.current[address]).toHaveProperty('reserved');
133+
expect(result.current[address]).toHaveProperty('frozen');
134+
135+
expect(typeof result.current[address].free).toBe('bigint');
136+
expect(typeof result.current[address].reserved).toBe('bigint');
137+
expect(typeof result.current[address].frozen).toBe('bigint');
138+
}
139+
});
140+
});

0 commit comments

Comments
 (0)