@@ -2,7 +2,7 @@ import { beforeAll, describe, expect, it } from 'vitest';
22import { ALICE , BOB , CHARLIE , deployPsp22Contract , psp22Metadata , wrapper } from '../utils' ;
33import { numberToHex } from 'dedot/utils' ;
44import { renderHook , waitFor } from '@testing-library/react' ;
5- import { useContractQuery } from 'typink' ;
5+ import { useContractQuery , useContractTx , useRawContract } from 'typink' ;
66import { Contract } from 'dedot/contracts' ;
77import { Psp22ContractApi } from 'contracts/psp22' ;
88
@@ -39,6 +39,62 @@ describe('useContractQuery', () => {
3939 } ) ;
4040 } ) ;
4141
42+ it ( 'should automatically call refresh on new block when watch is enabled' , async ( ) => {
43+ const { result : rawContract } = renderHook ( ( ) => useRawContract < Psp22ContractApi > ( psp22Metadata , contractAddress ) , {
44+ wrapper,
45+ } ) ;
46+
47+ await waitFor ( ( ) => {
48+ expect ( rawContract . current . contract ) . toBeDefined ( ) ;
49+ expect ( rawContract . current . contract ?. client . options . signer ) . toBeDefined ( ) ;
50+ } ) ;
51+
52+ const contract = rawContract . current . contract ;
53+
54+ const { result : balanceOf } = renderHook (
55+ ( ) => useContractQuery ( { contract, fn : 'psp22BalanceOf' , args : [ ALICE ] , watch : true } ) ,
56+ {
57+ wrapper,
58+ } ,
59+ ) ;
60+
61+ await waitFor ( ( ) => {
62+ expect ( balanceOf . current . data ) . toBeDefined ( ) ;
63+ } ) ;
64+
65+ const beforeTranfer = balanceOf . current . data ;
66+
67+ console . log ( 'Before transfer:' , beforeTranfer ) ;
68+
69+ const { result : transfer } = renderHook (
70+ ( ) => useContractTx ( contract , 'psp22Transfer' ) , // prettier-end-here
71+ {
72+ wrapper,
73+ } ,
74+ ) ;
75+
76+ await new Promise < void > ( ( resolve ) => {
77+ transfer . current . signAndSend ( {
78+ args : [ BOB , BigInt ( 1e12 ) , '0x' ] ,
79+ callback : ( { status } ) => {
80+ if ( status . type === 'Finalized' ) {
81+ resolve ( ) ;
82+ }
83+ } ,
84+ } ) ;
85+ } ) ;
86+ console . log ( 'Transfer completed!' ) ;
87+
88+ await waitFor (
89+ ( ) => {
90+ expect ( balanceOf . current . data ) . not . toEqual ( beforeTranfer ) ;
91+ } ,
92+ { timeout : 12000 } ,
93+ ) ;
94+
95+ console . log ( 'After transfer:' , balanceOf . current . data ) ;
96+ } ) ;
97+
4298 it ( 'should fail dry-run' , async ( ) => {
4399 const { result } = renderHook (
44100 ( ) =>
0 commit comments