1
1
import { fireEvent , render , screen , waitFor } from "@testing-library/react" ;
2
- import { describe , it } from "vitest" ;
2
+ import { beforeEach , describe , it } from "vitest" ;
3
3
import { ERC20DepositForm } from "../src" ;
4
4
import withMantineTheme from "./utils/WithMantineTheme" ;
5
5
import { getAddress } from "viem" ;
6
+ import {
7
+ useSimulateErc20Approve ,
8
+ useSimulateErc20PortalDepositErc20Tokens ,
9
+ useWriteErc20Approve ,
10
+ useWriteErc20PortalDepositErc20Tokens ,
11
+ } from "@cartesi/rollups-wagmi" ;
6
12
7
13
const Component = withMantineTheme ( ERC20DepositForm ) ;
8
14
@@ -32,34 +38,21 @@ const defaultProps = {
32
38
onSuccess : ( ) => undefined ,
33
39
} ;
34
40
35
- vi . mock ( "@cartesi/rollups-wagmi" , async ( ) => {
36
- const actual = await vi . importActual ( "@cartesi/rollups-wagmi" ) ;
37
- return {
38
- ...( actual as any ) ,
39
- useSimulateErc20Approve : ( ) => ( {
40
- data : {
41
- request : { } ,
42
- } ,
43
- config : { } ,
44
- } ) ,
45
- useWriteErc20Approve : ( ) => ( {
46
- data : { } ,
47
- wait : vi . fn ( ) ,
48
- reset : vi . fn ( ) ,
49
- } ) ,
50
- useSimulateErc20PortalDepositErc20Tokens : ( ) => ( {
51
- data : {
52
- request : { } ,
53
- } ,
54
- config : { } ,
55
- } ) ,
56
- useWriteErc20PortalDepositErc20Tokens : ( ) => ( {
57
- data : { } ,
58
- wait : vi . fn ( ) ,
59
- reset : vi . fn ( ) ,
60
- } ) ,
61
- } ;
41
+ vi . mock ( "@cartesi/rollups-wagmi" ) ;
42
+ const useSimulateErc20ApproveMock = vi . mocked ( useSimulateErc20Approve , {
43
+ partial : true ,
62
44
} ) ;
45
+ const useWriteErc20ApproveMock = vi . mocked ( useWriteErc20Approve , {
46
+ partial : true ,
47
+ } ) ;
48
+ const useSimulateErc20PortalDepositErc20TokensMock = vi . mocked (
49
+ useSimulateErc20PortalDepositErc20Tokens ,
50
+ { partial : true } ,
51
+ ) ;
52
+ const useWriteErc20PortalDepositErc20TokensMock = vi . mocked (
53
+ useWriteErc20PortalDepositErc20Tokens ,
54
+ { partial : true } ,
55
+ ) ;
63
56
64
57
vi . mock ( "wagmi" , async ( ) => {
65
58
return {
@@ -112,6 +105,26 @@ vi.mock("../src/hooks/useWatchQueryOnBlockChange", () => ({
112
105
} ) ) ;
113
106
114
107
describe ( "Rollups ERC20DepositForm" , ( ) => {
108
+ beforeEach ( ( ) => {
109
+ useSimulateErc20ApproveMock . mockReturnValue ( {
110
+ data : {
111
+ request : { } ,
112
+ } ,
113
+ } ) ;
114
+ useWriteErc20ApproveMock . mockReturnValue ( {
115
+ data : "0x721be000f6054b5e0e57aaab791015b53f0a18f4" ,
116
+ reset : vi . fn ( ) ,
117
+ } ) ;
118
+ useSimulateErc20PortalDepositErc20TokensMock . mockReturnValue ( {
119
+ data : {
120
+ request : { } ,
121
+ } ,
122
+ } ) ;
123
+ useWriteErc20PortalDepositErc20TokensMock . mockReturnValue ( {
124
+ data : "0x721be000f6054b5e0e57aaab791015b53f0a18f4" ,
125
+ reset : vi . fn ( ) ,
126
+ } ) ;
127
+ } ) ;
115
128
describe ( "ApplicationAutocomplete" , ( ) => {
116
129
it ( "should display correct label" , ( ) => {
117
130
render ( < Component { ...defaultProps } /> ) ;
@@ -190,16 +203,17 @@ describe("Rollups ERC20DepositForm", () => {
190
203
expect ( onSuccessMock ) . toHaveBeenCalled ( ) ;
191
204
} ) ;
192
205
193
- it ( "should correctly format address" , async ( ) => {
194
- const rollupsWagmi = await import ( "@cartesi/rollups-wagmi" ) ;
206
+ it ( "should correctly format address" , ( ) => {
195
207
const mockedHook = vi . fn ( ) . mockReturnValue ( {
196
- ...rollupsWagmi . useSimulateErc20PortalDepositErc20Tokens ,
208
+ data : {
209
+ request : { } ,
210
+ } ,
197
211
loading : false ,
198
212
error : null ,
199
213
} ) ;
200
- rollupsWagmi . useSimulateErc20PortalDepositErc20Tokens = vi
201
- . fn ( )
202
- . mockImplementation ( mockedHook ) ;
214
+ useSimulateErc20PortalDepositErc20TokensMock . mockImplementation (
215
+ mockedHook ,
216
+ ) ;
203
217
204
218
const { container } = render ( < Component { ...defaultProps } /> ) ;
205
219
const input = container . querySelector ( "input" ) as HTMLInputElement ;
@@ -320,16 +334,15 @@ describe("Rollups ERC20DepositForm", () => {
320
334
expect ( screen . getByDisplayValue ( value ) === amountInput ) . toBe ( true ) ;
321
335
} ) ;
322
336
323
- it ( "should correctly process small decimal numbers" , async ( ) => {
324
- const rollupsWagmi = await import ( "@cartesi/rollups-wagmi" ) ;
337
+ it ( "should correctly process small decimal numbers" , ( ) => {
325
338
const mockedHook = vi . fn ( ) . mockReturnValue ( {
326
- ...rollupsWagmi . useSimulateErc20Approve ,
339
+ data : {
340
+ request : { } ,
341
+ } ,
327
342
loading : false ,
328
343
error : null ,
329
344
} ) ;
330
- rollupsWagmi . useSimulateErc20Approve = vi
331
- . fn ( )
332
- . mockImplementation ( mockedHook ) ;
345
+ useSimulateErc20ApproveMock . mockImplementation ( mockedHook ) ;
333
346
334
347
const { container } = render ( < Component { ...defaultProps } /> ) ;
335
348
const amountInput = container . querySelector (
@@ -353,6 +366,72 @@ describe("Rollups ERC20DepositForm", () => {
353
366
} ,
354
367
} ) ;
355
368
} ) ;
369
+
370
+ it ( "should disable approve call when amount is '0'" , ( ) => {
371
+ const mockedHook = vi . fn ( ) . mockReturnValue ( {
372
+ data : {
373
+ request : { } ,
374
+ } ,
375
+ loading : false ,
376
+ error : null ,
377
+ } ) ;
378
+ useSimulateErc20ApproveMock . mockImplementation ( mockedHook ) ;
379
+
380
+ const { container } = render ( < Component { ...defaultProps } /> ) ;
381
+ const amountInput = container . querySelector (
382
+ '[type="number"]' ,
383
+ ) as HTMLInputElement ;
384
+
385
+ fireEvent . change ( amountInput , {
386
+ target : {
387
+ value : "0" ,
388
+ } ,
389
+ } ) ;
390
+
391
+ expect ( mockedHook ) . toHaveBeenLastCalledWith ( {
392
+ address : "0x0000000000000000000000000000000000000000" ,
393
+ args : [ "0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB" , 0n ] ,
394
+ query : {
395
+ enabled : false ,
396
+ } ,
397
+ } ) ;
398
+ } ) ;
399
+
400
+ it ( "should disable deposit call when amount is '0'" , ( ) => {
401
+ const mockedHook = vi . fn ( ) . mockReturnValue ( {
402
+ data : {
403
+ request : { } ,
404
+ } ,
405
+ loading : false ,
406
+ error : null ,
407
+ } ) ;
408
+ useSimulateErc20PortalDepositErc20TokensMock . mockImplementation (
409
+ mockedHook ,
410
+ ) ;
411
+
412
+ const { container } = render ( < Component { ...defaultProps } /> ) ;
413
+ const amountInput = container . querySelector (
414
+ '[type="number"]' ,
415
+ ) as HTMLInputElement ;
416
+
417
+ fireEvent . change ( amountInput , {
418
+ target : {
419
+ value : "0" ,
420
+ } ,
421
+ } ) ;
422
+
423
+ expect ( mockedHook ) . toHaveBeenLastCalledWith ( {
424
+ args : [
425
+ "0x0000000000000000000000000000000000000000" ,
426
+ "0x0000000000000000000000000000000000000000" ,
427
+ 0n ,
428
+ "0x" ,
429
+ ] ,
430
+ query : {
431
+ enabled : false ,
432
+ } ,
433
+ } ) ;
434
+ } ) ;
356
435
} ) ;
357
436
describe ( "Max Amount Button" , ( ) => {
358
437
vi . mock ( "wagmi" , async ( ) => {
@@ -435,45 +514,6 @@ describe("Rollups ERC20DepositForm", () => {
435
514
} ) ;
436
515
} ) ;
437
516
438
- describe ( "Amount input" , ( ) => {
439
- it ( "should correctly process small decimal numbers" , async ( ) => {
440
- const rollupsWagmi = await import ( "@cartesi/rollups-wagmi" ) ;
441
- const mockedHook = vi . fn ( ) . mockReturnValue ( {
442
- ...rollupsWagmi . useSimulateErc20Approve ,
443
- data : {
444
- request : { } ,
445
- } ,
446
- loading : false ,
447
- error : null ,
448
- } ) ;
449
- rollupsWagmi . useSimulateErc20Approve = vi
450
- . fn ( )
451
- . mockImplementation ( mockedHook ) ;
452
-
453
- const { container } = render ( < Component { ...defaultProps } /> ) ;
454
- const amountInput = container . querySelector (
455
- '[type="number"]' ,
456
- ) as HTMLInputElement ;
457
-
458
- fireEvent . change ( amountInput , {
459
- target : {
460
- value : "0.0000001" ,
461
- } ,
462
- } ) ;
463
-
464
- expect ( mockedHook ) . toHaveBeenLastCalledWith ( {
465
- address : "0x0000000000000000000000000000000000000000" ,
466
- args : [
467
- "0x9C21AEb2093C32DDbC53eEF24B873BDCd1aDa1DB" ,
468
- 100000000000n ,
469
- ] ,
470
- query : {
471
- enabled : true ,
472
- } ,
473
- } ) ;
474
- } ) ;
475
- } ) ;
476
-
477
517
describe ( "Deposit button" , ( ) => {
478
518
it ( "should invoke onSearchApplications function after successful deposit" , async ( ) => {
479
519
const wagmi = await import ( "wagmi" ) ;
@@ -494,16 +534,17 @@ describe("Rollups ERC20DepositForm", () => {
494
534
expect ( onSearchApplicationsMock ) . toHaveBeenCalledWith ( "" ) ;
495
535
} ) ;
496
536
497
- it ( "should correctly format extra data" , async ( ) => {
498
- const rollupsWagmi = await import ( "@cartesi/rollups-wagmi" ) ;
537
+ it ( "should correctly format extra data" , ( ) => {
499
538
const mockedHook = vi . fn ( ) . mockReturnValue ( {
500
- ...rollupsWagmi . useSimulateErc20PortalDepositErc20Tokens ,
539
+ data : {
540
+ request : { } ,
541
+ } ,
501
542
loading : false ,
502
543
error : null ,
503
544
} ) ;
504
- rollupsWagmi . useSimulateErc20PortalDepositErc20Tokens = vi
505
- . fn ( )
506
- . mockImplementation ( mockedHook ) ;
545
+ useSimulateErc20PortalDepositErc20TokensMock . mockImplementation (
546
+ mockedHook ,
547
+ ) ;
507
548
508
549
const { container } = render ( < Component { ...defaultProps } /> ) ;
509
550
const textarea = container . querySelector (
0 commit comments