Skip to content

Commit 7146d0a

Browse files
committed
test: update e2e-tests
1 parent 95bf157 commit 7146d0a

File tree

3 files changed

+78
-28
lines changed

3 files changed

+78
-28
lines changed

e2e/zombienet/src/hooks/useContractQuery.test.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,6 @@ describe('useContractQuery', () => {
2828
expect(totalSupply.current.data).toEqual(BigInt(1e20));
2929
expect(totalSupply.current.isLoading).toEqual(false);
3030
});
31-
32-
totalSupply.current.refresh();
33-
34-
await waitFor(() => {
35-
expect(totalSupply.current.isRefreshing).toEqual(true);
36-
});
37-
38-
await waitFor(() => {
39-
expect(totalSupply.current.data).toEqual(BigInt(1e20));
40-
expect(totalSupply.current.isRefreshing).toEqual(false);
41-
});
42-
43-
const { result: balanceOf } = renderHook(
44-
() => useContractQuery({ contract, args: ['0x_FAKE_'], fn: 'psp22BalanceOf' }),
45-
{
46-
wrapper,
47-
},
48-
);
49-
await waitFor(() => {
50-
expect(balanceOf.current.error).toBeDefined();
51-
});
52-
53-
console.log('Error:', balanceOf.current.error);
5431
});
5532

5633
it('should dry run successfully', async () => {
@@ -89,7 +66,6 @@ describe('useContractQuery', () => {
8966
});
9067

9168
const beforeTranfer = balanceOf.current.data!;
92-
9369
console.log('Before transfer:', beforeTranfer);
9470

9571
const { result: transfer } = renderHook(
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 { renderHook, waitFor } from '@testing-library/react';
5+
import { useContractQuery, useContractTx } from 'typink';
6+
import { Contract } from 'dedot/contracts';
7+
import { Psp22ContractApi } from 'contracts/psp22';
8+
9+
describe('useContractQuery_2', () => {
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('should define error when errors occured', async () => {
19+
const { result: balanceOf } = renderHook(
20+
() => useContractQuery({ contract, args: ['0x__FAKE'], fn: 'psp22BalanceOf' }),
21+
{ wrapper },
22+
);
23+
24+
await waitFor(() => {
25+
expect(balanceOf.current.error).toBeDefined();
26+
});
27+
28+
console.log('Error:', balanceOf.current.error);
29+
});
30+
31+
it('should run refresh correctly', async () => {
32+
const { result: balanceOf } = renderHook(
33+
() => useContractQuery({ contract, args: [ALICE], fn: 'psp22BalanceOf' }),
34+
{ wrapper },
35+
);
36+
37+
await waitFor(() => {
38+
expect(balanceOf.current.data).toBeDefined();
39+
});
40+
41+
const beforeTranfer = balanceOf.current.data!;
42+
43+
console.log('Before transfer:', beforeTranfer);
44+
45+
const { result: transfer } = renderHook(
46+
() => useContractTx(contract, 'psp22Transfer'), // prettier-end-here
47+
{ wrapper },
48+
);
49+
50+
await new Promise<void>((resolve) => {
51+
transfer.current.signAndSend({
52+
args: [BOB, BigInt(1e12), '0x'],
53+
callback: ({ status }) => {
54+
if (status.type === 'Finalized') {
55+
resolve();
56+
}
57+
},
58+
});
59+
});
60+
61+
console.log('Transfer completed!');
62+
63+
balanceOf.current.refresh();
64+
65+
await waitFor(() => {
66+
expect(balanceOf.current.isRefreshing).toEqual(true);
67+
});
68+
69+
await waitFor(() => {
70+
expect(balanceOf.current.isRefreshing).toEqual(false);
71+
expect(balanceOf.current.data).toBeDefined();
72+
expect(balanceOf.current.data).toEqual(beforeTranfer - BigInt(1e12));
73+
});
74+
});
75+
});

packages/typink/src/hooks/useContractQuery.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type UseContractQueryReturnType<
1414
M extends keyof ContractQuery<T> = keyof ContractQuery<T>,
1515
> = {
1616
isLoading: boolean;
17-
refresh: () => void;
17+
refresh: () => Promise<void>;
1818
isRefreshing: boolean;
1919
error?: Error;
2020
} & Partial<Awaited<ReturnType<T['query'][M]>>>;
@@ -50,8 +50,8 @@ export function useContractQuery<
5050
} & Args<Pop<Parameters<T['query'][M]>>>,
5151
): UseContractQueryReturnType<T, M> {
5252
// TODO replace loading tracking state with tanstack
53-
54-
const { client } = useTypink();
53+
54+
const { client } = useTypink();
5555

5656
const [isLoading, setIsLoading] = useState<boolean>(true);
5757
const [isRefreshing, setIsRefreshing] = useState<boolean>(false);
@@ -151,4 +151,3 @@ export function useContractQuery<
151151
error,
152152
} as any;
153153
}
154-

0 commit comments

Comments
 (0)