Skip to content

Commit f1b5ae7

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

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { renderHook, waitFor } from '@testing-library/react';
55
import { useContractQuery, useContractTx, useRawContract } from 'typink';
66
import { Contract } from 'dedot/contracts';
77
import { Psp22ContractApi } from 'contracts/psp22';
8-
98
describe('useContractQuery', () => {
109
let contractAddress: string, contract: Contract<Psp22ContractApi>;
1110
beforeAll(async () => {
@@ -29,28 +28,37 @@ describe('useContractQuery', () => {
2928
expect(totalSupply.current.isLoading).toEqual(false);
3029
});
3130

32-
totalSupply.current.refresh();
31+
const { result: balanceOf, rerender } = renderHook(
32+
({ address }) => useContractQuery({ contract, args: [address], fn: 'psp22BalanceOf' }),
33+
{
34+
wrapper,
35+
initialProps: {
36+
address: '0x__FAKE',
37+
},
38+
},
39+
);
3340

3441
await waitFor(() => {
35-
expect(totalSupply.current.isRefreshing).toEqual(true);
42+
expect(balanceOf.current.error).toBeDefined();
3643
});
3744

45+
console.log('Error:', balanceOf.current.error);
46+
47+
rerender({ address: ALICE });
48+
3849
await waitFor(() => {
39-
expect(totalSupply.current.data).toEqual(BigInt(1e20));
40-
expect(totalSupply.current.isRefreshing).toEqual(false);
50+
expect(balanceOf.current.data).toEqual(BigInt(1e20));
4151
});
4252

43-
const { result: balanceOf } = renderHook(
44-
() => useContractQuery({ contract, args: ['0x_FAKE_'], fn: 'psp22BalanceOf' }),
45-
{
46-
wrapper,
47-
},
48-
);
53+
balanceOf.current.refresh();
54+
4955
await waitFor(() => {
50-
expect(balanceOf.current.error).toBeDefined();
56+
expect(balanceOf.current.isRefreshing).toEqual(true);
5157
});
5258

53-
console.log('Error:', balanceOf.current.error);
59+
await waitFor(() => {
60+
expect(balanceOf.current.isRefreshing).toEqual(false);
61+
});
5462
});
5563

5664
it('should dry run successfully', async () => {

packages/typink/src/hooks/useContractQuery.ts

Lines changed: 7 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);
@@ -101,11 +101,15 @@ export function useContractQuery<
101101
try {
102102
setIsRefreshing(true);
103103

104+
console.log('Refreshing query:', fn, args, options);
105+
104106
const result = await contract.query[fn](...args, options);
105107

106108
setResult(result);
107109
setError(undefined);
108110
setIsRefreshing(false);
111+
112+
console.log('[DONE]: Refreshing query: ', fn, args, options);
109113
} catch (error: any) {
110114
console.error('Error when refreshing the query:', error);
111115

@@ -151,4 +155,3 @@ export function useContractQuery<
151155
error,
152156
} as any;
153157
}
154-

0 commit comments

Comments
 (0)