File tree Expand file tree Collapse file tree 4 files changed +116
-18
lines changed Expand file tree Collapse file tree 4 files changed +116
-18
lines changed Original file line number Diff line number Diff line change @@ -541,6 +541,53 @@ describe('PeraWallet', () => {
541
541
} )
542
542
} )
543
543
544
+ describe ( 'autoConnect' , ( ) => {
545
+ let mockUserAgent : string
546
+
547
+ beforeEach ( ( ) => {
548
+ mockUserAgent = ''
549
+ vi . clearAllMocks ( )
550
+
551
+ vi . stubGlobal ( 'window' , {
552
+ navigator : {
553
+ get userAgent ( ) {
554
+ return mockUserAgent
555
+ }
556
+ }
557
+ } )
558
+ } )
559
+
560
+ afterEach ( ( ) => {
561
+ vi . unstubAllGlobals ( )
562
+ } )
563
+
564
+ it ( 'should attempt auto-connect in Pera browser' , ( ) => {
565
+ mockUserAgent = 'pera/1.0.0'
566
+
567
+ // Mock the private method before creating the wallet
568
+ vi . spyOn ( PeraWallet . prototype as any , 'autoConnect' )
569
+ . mockReset ( )
570
+ . mockImplementation ( ( ) => Promise . resolve ( ) )
571
+
572
+ createWalletWithStore ( store )
573
+
574
+ expect ( PeraWallet . prototype [ 'autoConnect' ] ) . toHaveBeenCalled ( )
575
+ } )
576
+
577
+ it ( 'should not attempt auto-connect in other browsers' , ( ) => {
578
+ mockUserAgent = 'chrome/1.0.0'
579
+
580
+ // Mock the private method before creating the wallet
581
+ vi . spyOn ( PeraWallet . prototype as any , 'autoConnect' )
582
+ . mockReset ( )
583
+ . mockImplementation ( ( ) => Promise . resolve ( ) )
584
+
585
+ createWalletWithStore ( store )
586
+
587
+ expect ( PeraWallet . prototype [ 'autoConnect' ] ) . not . toHaveBeenCalled ( )
588
+ } )
589
+ } )
590
+
544
591
describe ( 'signing transactions' , ( ) => {
545
592
// Connected accounts
546
593
const connectedAcct1 = '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q'
Original file line number Diff line number Diff line change @@ -334,6 +334,53 @@ describe('PeraWallet', () => {
334
334
} )
335
335
} )
336
336
337
+ describe ( 'autoConnect' , ( ) => {
338
+ let mockUserAgent : string
339
+
340
+ beforeEach ( ( ) => {
341
+ mockUserAgent = ''
342
+ vi . clearAllMocks ( )
343
+
344
+ vi . stubGlobal ( 'window' , {
345
+ navigator : {
346
+ get userAgent ( ) {
347
+ return mockUserAgent
348
+ }
349
+ }
350
+ } )
351
+ } )
352
+
353
+ afterEach ( ( ) => {
354
+ vi . unstubAllGlobals ( )
355
+ } )
356
+
357
+ it ( 'should attempt auto-connect in Pera browser' , ( ) => {
358
+ mockUserAgent = 'pera/1.0.0'
359
+
360
+ // Mock the private method before creating the wallet
361
+ vi . spyOn ( PeraWallet . prototype as any , 'autoConnect' )
362
+ . mockReset ( )
363
+ . mockImplementation ( ( ) => Promise . resolve ( ) )
364
+
365
+ createWalletWithStore ( store )
366
+
367
+ expect ( PeraWallet . prototype [ 'autoConnect' ] ) . toHaveBeenCalled ( )
368
+ } )
369
+
370
+ it ( 'should not attempt auto-connect in other browsers' , ( ) => {
371
+ mockUserAgent = 'chrome/1.0.0'
372
+
373
+ // Mock the private method before creating the wallet
374
+ vi . spyOn ( PeraWallet . prototype as any , 'autoConnect' )
375
+ . mockReset ( )
376
+ . mockImplementation ( ( ) => Promise . resolve ( ) )
377
+
378
+ createWalletWithStore ( store )
379
+
380
+ expect ( PeraWallet . prototype [ 'autoConnect' ] ) . not . toHaveBeenCalled ( )
381
+ } )
382
+ } )
383
+
337
384
describe ( 'signing transactions' , ( ) => {
338
385
// Connected accounts
339
386
const connectedAcct1 = '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q'
Original file line number Diff line number Diff line change @@ -47,16 +47,8 @@ export class PeraWallet extends BaseWallet {
47
47
48
48
if ( typeof window !== 'undefined' && window . navigator ) {
49
49
const isPeraDiscover = window . navigator . userAgent . includes ( 'pera' )
50
-
51
50
if ( isPeraDiscover ) {
52
- this . logger . info ( 'Pera Discover browser detected, auto connecting...' )
53
- void this . connect ( )
54
- . then ( ( ) => {
55
- this . logger . info ( 'Auto-connect successful' )
56
- } )
57
- . catch ( ( error ) => {
58
- this . logger . warn ( 'Auto-connect failed:' , error . message )
59
- } )
51
+ void this . autoConnect ( )
60
52
}
61
53
}
62
54
}
@@ -66,6 +58,16 @@ export class PeraWallet extends BaseWallet {
66
58
icon : ICON
67
59
}
68
60
61
+ private async autoConnect ( ) : Promise < void > {
62
+ this . logger . info ( 'Pera Discover browser detected, auto connecting...' )
63
+ try {
64
+ await this . connect ( )
65
+ this . logger . info ( 'Auto-connect successful' )
66
+ } catch ( error : any ) {
67
+ this . logger . warn ( 'Auto-connect failed:' , error . message )
68
+ }
69
+ }
70
+
69
71
private async initializeClient ( ) : Promise < PeraWalletConnect > {
70
72
this . logger . info ( 'Initializing client...' )
71
73
const module = await import ( '@perawallet/connect' )
Original file line number Diff line number Diff line change @@ -56,16 +56,8 @@ export class PeraWallet extends BaseWallet {
56
56
57
57
if ( typeof window !== 'undefined' && window . navigator ) {
58
58
const isPeraDiscover = window . navigator . userAgent . includes ( 'pera' )
59
-
60
59
if ( isPeraDiscover ) {
61
- this . logger . info ( 'Pera Discover browser detected, auto connecting...' )
62
- void this . connect ( )
63
- . then ( ( ) => {
64
- this . logger . info ( 'Auto-connect successful' )
65
- } )
66
- . catch ( ( error ) => {
67
- this . logger . warn ( 'Auto-connect failed:' , error . message )
68
- } )
60
+ void this . autoConnect ( )
69
61
}
70
62
}
71
63
}
@@ -75,6 +67,16 @@ export class PeraWallet extends BaseWallet {
75
67
icon : ICON
76
68
}
77
69
70
+ private async autoConnect ( ) : Promise < void > {
71
+ this . logger . info ( 'Pera Discover browser detected, auto connecting...' )
72
+ try {
73
+ await this . connect ( )
74
+ this . logger . info ( 'Auto-connect successful' )
75
+ } catch ( error : any ) {
76
+ this . logger . warn ( 'Auto-connect failed:' , error . message )
77
+ }
78
+ }
79
+
78
80
private async initializeClient ( ) : Promise < PeraWalletConnect > {
79
81
this . logger . info ( 'Initializing client...' )
80
82
const module = await import ( '@perawallet/connect-beta' )
You can’t perform that action at this time.
0 commit comments