@@ -224,28 +224,27 @@ describe('ConnectWallet', () => {
224
224
225
225
describe ( 'wallet display modes' , ( ) => {
226
226
it ( 'should render modal when config.wallet.display is "modal"' , ( ) => {
227
- const setIsOpenMock = vi . fn ( ) ;
228
227
vi . mocked ( useOnchainKit ) . mockReturnValue ( {
229
228
config : { wallet : { display : 'modal' } } ,
230
229
} ) ;
231
230
vi . mocked ( useAccount ) . mockReturnValue ( {
232
231
address : '' ,
233
232
status : 'disconnected' ,
234
233
} ) ;
235
- vi . mocked ( useWalletContext ) . mockReturnValue ( {
236
- isOpen : false ,
237
- setIsOpen : setIsOpenMock ,
234
+ vi . mocked ( useConnect ) . mockReturnValue ( {
235
+ connectors : [ { id : 'mockConnector' } ] ,
236
+ connect : vi . fn ( ) ,
237
+ status : 'idle' ,
238
238
} ) ;
239
239
240
240
render ( < ConnectWallet text = "Connect Wallet" /> ) ;
241
241
242
- const container = screen . getByTestId ( 'ockConnectWallet_Container' ) ;
243
- expect ( container ) . toBeInTheDocument ( ) ;
244
- const connectButton = screen . getByTestId ( 'ockConnectButton' ) ;
245
- expect ( connectButton ) . toBeInTheDocument ( ) ;
242
+ // Click the connect button
243
+ const button = screen . getByTestId ( 'ockConnectButton' ) ;
244
+ fireEvent . click ( button ) ;
246
245
247
- fireEvent . click ( connectButton ) ;
248
- expect ( setIsOpenMock ) . toHaveBeenCalledWith ( true ) ;
246
+ // Verify that the modal is rendered using the correct test ID
247
+ expect ( screen . getByTestId ( 'ockModalOverlay' ) ) . toBeInTheDocument ( ) ;
249
248
} ) ;
250
249
251
250
it ( 'should render direct connect when config.wallet.display is undefined' , ( ) => {
@@ -317,40 +316,50 @@ describe('ConnectWallet', () => {
317
316
} ) ;
318
317
319
318
describe ( 'modal handling' , ( ) => {
320
- it ( 'should close modal when handleClose is called' , ( ) => {
321
- const setIsOpenMock = vi . fn ( ) ;
322
- vi . mocked ( useWalletContext ) . mockReturnValue ( {
323
- isOpen : true ,
324
- setIsOpen : setIsOpenMock ,
325
- } ) ;
319
+ it ( 'should close modal when clicking overlay' , ( ) => {
326
320
vi . mocked ( useOnchainKit ) . mockReturnValue ( {
327
321
config : { wallet : { display : 'modal' } } ,
328
322
} ) ;
323
+ vi . mocked ( useAccount ) . mockReturnValue ( {
324
+ address : '' ,
325
+ status : 'disconnected' ,
326
+ } ) ;
329
327
330
328
render ( < ConnectWallet text = "Connect Wallet" /> ) ;
331
329
332
- const modalOverlay = screen . getByTestId ( 'modal-overlay' ) ;
330
+ // Open the modal first
331
+ const connectButton = screen . getByTestId ( 'ockConnectButton' ) ;
332
+ fireEvent . click ( connectButton ) ;
333
+
334
+ // Now click the overlay to close
335
+ const modalOverlay = screen . getByTestId ( 'ockModalOverlay' ) ;
333
336
fireEvent . click ( modalOverlay ) ;
334
337
335
- expect ( setIsOpenMock ) . toHaveBeenCalledWith ( false ) ;
338
+ // Modal should no longer be in the document
339
+ expect ( screen . queryByTestId ( 'ockModalOverlay' ) ) . not . toBeInTheDocument ( ) ;
336
340
} ) ;
337
341
338
- it ( 'should close modal when close button is clicked' , ( ) => {
339
- const setIsOpenMock = vi . fn ( ) ;
340
- vi . mocked ( useWalletContext ) . mockReturnValue ( {
341
- isOpen : true ,
342
- setIsOpen : setIsOpenMock ,
343
- } ) ;
342
+ it ( 'should close modal when clicking close button' , ( ) => {
344
343
vi . mocked ( useOnchainKit ) . mockReturnValue ( {
345
344
config : { wallet : { display : 'modal' } } ,
346
345
} ) ;
346
+ vi . mocked ( useAccount ) . mockReturnValue ( {
347
+ address : '' ,
348
+ status : 'disconnected' ,
349
+ } ) ;
347
350
348
351
render ( < ConnectWallet text = "Connect Wallet" /> ) ;
349
352
353
+ // Open the modal first
354
+ const connectButton = screen . getByTestId ( 'ockConnectButton' ) ;
355
+ fireEvent . click ( connectButton ) ;
356
+
357
+ // Click close button using aria-label instead of test-id
350
358
const closeButton = screen . getByLabelText ( 'Close modal' ) ;
351
359
fireEvent . click ( closeButton ) ;
352
360
353
- expect ( setIsOpenMock ) . toHaveBeenCalledWith ( false ) ;
361
+ // Modal should no longer be in the document
362
+ expect ( screen . queryByTestId ( 'ockModalOverlay' ) ) . not . toBeInTheDocument ( ) ;
354
363
} ) ;
355
364
} ) ;
356
365
@@ -457,5 +466,49 @@ describe('ConnectWallet', () => {
457
466
458
467
expect ( onConnectMock ) . toHaveBeenCalledTimes ( 1 ) ;
459
468
} ) ;
469
+
470
+ it ( 'should handle hasClickedConnect state correctly when modal is used' , ( ) => {
471
+ const onConnectMock = vi . fn ( ) ;
472
+ const mockUseAccount = vi . mocked ( useAccount ) ;
473
+
474
+ // Configure for modal display
475
+ vi . mocked ( useOnchainKit ) . mockReturnValue ( {
476
+ config : { wallet : { display : 'modal' } } ,
477
+ } ) ;
478
+
479
+ // Initial disconnected state
480
+ mockUseAccount . mockReturnValue ( {
481
+ address : undefined ,
482
+ status : 'disconnected' ,
483
+ } ) ;
484
+
485
+ const { rerender } = render (
486
+ < ConnectWallet text = "Connect Wallet" onConnect = { onConnectMock } /> ,
487
+ ) ;
488
+
489
+ // Click connect button to open modal
490
+ const button = screen . getByTestId ( 'ockConnectButton' ) ;
491
+ fireEvent . click ( button ) ;
492
+
493
+ // Simulate connection success
494
+ mockUseAccount . mockReturnValue ( {
495
+ address : '0x123' ,
496
+ status : 'connected' ,
497
+ } ) ;
498
+
499
+ // Rerender to trigger useEffect
500
+ rerender (
501
+ < ConnectWallet text = "Connect Wallet" onConnect = { onConnectMock } /> ,
502
+ ) ;
503
+
504
+ // onConnect should have been called once
505
+ expect ( onConnectMock ) . toHaveBeenCalledTimes ( 1 ) ;
506
+
507
+ // Rerender again to ensure onConnect isn't called multiple times
508
+ rerender (
509
+ < ConnectWallet text = "Connect Wallet" onConnect = { onConnectMock } /> ,
510
+ ) ;
511
+ expect ( onConnectMock ) . toHaveBeenCalledTimes ( 1 ) ;
512
+ } ) ;
460
513
} ) ;
461
514
} ) ;
0 commit comments