Skip to content

Commit 45b807e

Browse files
committed
add useContract3
1 parent 0b2cb8b commit 45b807e

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { beforeAll, describe, expect, it } from 'vitest';
2+
import { ALICE, BOB, deployPsp22Contract, psp22Metadata, wrapper } from '../utils';
3+
import { numberToHex } from 'dedot/utils';
4+
import { Contract } from 'dedot/contracts';
5+
import { Psp22ContractApi } from '../contracts/psp22';
6+
import { renderHook, waitFor } from '@testing-library/react';
7+
import { usePSP22Balance } from 'typink';
8+
9+
describe('useContract3', () => {
10+
let contractAddress: string, contract: Contract<Psp22ContractApi>;
11+
beforeAll(async () => {
12+
const randomSalt = numberToHex(Date.now());
13+
contractAddress = await deployPsp22Contract(randomSalt);
14+
console.log('Deployed contract address', contractAddress);
15+
contract = new Contract<Psp22ContractApi>(client, psp22Metadata, contractAddress, { defaultCaller: ALICE });
16+
});
17+
18+
it('get total supply', async () => {
19+
const { data: state } = await contract.query.psp22TotalSupply();
20+
21+
console.log(`Total supply`, state);
22+
expect(state).toBe(BigInt(1e20));
23+
});
24+
25+
it('get alice balance', async () => {
26+
const { data: state } = await contract.query.psp22BalanceOf(ALICE);
27+
28+
console.log(`alice balance`, state);
29+
expect(state).toBe(BigInt(1e20));
30+
});
31+
32+
it('should load balance properly', async () => {
33+
const { result } = renderHook(
34+
() => usePSP22Balance({ contractAddress, address: ALICE }), // prettier-end-here
35+
{ wrapper },
36+
);
37+
38+
expect(result.current.data).toBeUndefined();
39+
40+
await waitFor(() => {
41+
expect(result.current.data).toBe(BigInt(1e20));
42+
});
43+
});
44+
45+
it('should update balance when address changes', async () => {
46+
const { result, rerender } = renderHook(({ address }) => usePSP22Balance({ contractAddress, address }), {
47+
wrapper,
48+
initialProps: {
49+
address: ALICE,
50+
},
51+
});
52+
53+
expect(result.current.isLoading).toBe(true);
54+
55+
// Wait for ALICE's balance to be fetched
56+
await waitFor(() => {
57+
expect(result.current.data).toBeDefined();
58+
expect(result.current.isLoading).toBe(false);
59+
});
60+
61+
const aliceBalance = result.current.data;
62+
63+
rerender({ address: BOB });
64+
65+
// Wait for BOB's balance to be fetched
66+
await waitFor(() => {
67+
expect(result.current.data).toBe(0n);
68+
});
69+
70+
// BOB's balance should be different from ALICE's
71+
expect(result.current.data).not.toEqual(aliceBalance);
72+
});
73+
});

0 commit comments

Comments
 (0)