|
| 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 | +}); |
0 commit comments