1- import { describe , it , expect , vi , beforeEach } from 'vitest' ;
1+ import { beforeEach , describe , expect , it , vi } from 'vitest' ;
22import { renderHook } from '@testing-library/react' ;
33import { useTypink } from '../useTypink.js' ;
44import { ContractDeployer } from 'dedot/contracts' ;
55import { waitForNextUpdate } from './test-utils.js' ;
66import { useDeployerTx } from '../useDeployerTx.js' ;
7+ import { checkBalanceSufficiency } from '../../helpers/index.js' ;
8+ import { BalanceInsufficientError } from '../../utils/index.js' ;
79
810// Mock the useTypink hook
911vi . mock ( '../useTypink' , ( ) => ( {
1012 useTypink : vi . fn ( ) ,
1113} ) ) ;
1214
15+ vi . mock ( '../../helpers' , ( ) => ( {
16+ checkBalanceSufficiency : vi . fn ( ) ,
17+ } ) ) ;
18+
1319describe ( 'useDeployerTx' , ( ) => {
1420 const randomGasLimit = { refTime : 1000n , proofSize : 1000n } ;
1521 let mockContractDeployer : ContractDeployer < any > ;
1622 let mockConnectedAccount : { address : string } ;
1723
1824 beforeEach ( ( ) => {
1925 mockContractDeployer = {
26+ client : vi . fn ( ) ,
2027 tx : {
2128 message : vi . fn ( ) ,
2229 } ,
@@ -30,6 +37,8 @@ describe('useDeployerTx', () => {
3037 ( useTypink as any ) . mockReturnValue ( {
3138 connectedAccount : mockConnectedAccount ,
3239 } ) ;
40+
41+ ( checkBalanceSufficiency as any ) . mockImplementation ( ( ) => Promise . resolve ( true ) ) ;
3342 } ) ;
3443
3544 it ( 'should return the correct structure' , ( ) => {
@@ -56,7 +65,7 @@ describe('useDeployerTx', () => {
5665 ) ,
5766 ) ;
5867
59- await expect ( result . current . signAndSend ( { } as any ) ) . rejects . toThrow ( 'Contract Deployer Not Found ' ) ;
68+ await expect ( result . current . signAndSend ( { } as any ) ) . rejects . toThrow ( 'ContractDeployer not found ' ) ;
6069 } ) ;
6170
6271 it ( 'should throw an error if connectedAccount is undefined' , async ( ) => {
@@ -72,7 +81,9 @@ describe('useDeployerTx', () => {
7281 ) ,
7382 ) ;
7483
75- await expect ( result . current . signAndSend ( { } as any ) ) . rejects . toThrow ( 'Connected Account Not Found' ) ;
84+ await expect ( result . current . signAndSend ( { } as any ) ) . rejects . toThrow (
85+ 'No connected account. Please connect your wallet.' ,
86+ ) ;
7687 } ) ;
7788
7889 it ( 'should call the contract method with correct parameters' , async ( ) => {
@@ -171,6 +182,26 @@ describe('useDeployerTx', () => {
171182 ) ,
172183 ) ;
173184
174- await expect ( result . current . signAndSend ( { args : [ ] } ) ) . rejects . toThrow ( 'Contract error' ) ;
185+ await expect ( result . current . signAndSend ( { args : [ ] } ) ) . rejects . toThrow ( 'Contract Message Error: Contract error' ) ;
186+ } ) ;
187+
188+ it ( 'should throw an error when balance is insufficient' , async ( ) => {
189+ vi . mocked ( checkBalanceSufficiency ) . mockRejectedValue ( new BalanceInsufficientError ( mockConnectedAccount . address ) ) ;
190+
191+ const { result } = renderHook ( ( ) =>
192+ useDeployerTx (
193+ mockContractDeployer ,
194+ // @ts -ignore
195+ 'message' ,
196+ ) ,
197+ ) ;
198+
199+ await expect ( result . current . signAndSend ( { args : [ ] } ) ) . rejects . toThrow (
200+ new BalanceInsufficientError ( mockConnectedAccount . address ) ,
201+ ) ;
202+
203+ expect ( checkBalanceSufficiency ) . toHaveBeenCalledWith ( expect . anything ( ) , 'mock-address' ) ;
204+ expect ( mockContractDeployer . query . message ) . not . toHaveBeenCalled ( ) ;
205+ expect ( mockContractDeployer . tx . message ) . not . toHaveBeenCalled ( ) ;
175206 } ) ;
176207} ) ;
0 commit comments