Skip to content

Commit ec250f9

Browse files
committed
refactor(pera): extract auto-connect logic into private method
Extract auto-connect logic from constructor into a private async method for better error handling and testability. Update tests to use prototype mocking pattern consistent with other wallet implementations. - Move auto-connect logic into private `autoConnect` method - Use async/await with try/catch for better error handling - Update tests to mock method on prototype before wallet creation - Align test patterns with other wallet implementations
1 parent d86cbd3 commit ec250f9

File tree

4 files changed

+116
-18
lines changed

4 files changed

+116
-18
lines changed

packages/use-wallet/src/__tests__/wallets/pera.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,53 @@ describe('PeraWallet', () => {
541541
})
542542
})
543543

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+
544591
describe('signing transactions', () => {
545592
// Connected accounts
546593
const connectedAcct1 = '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q'

packages/use-wallet/src/__tests__/wallets/pera2.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,53 @@ describe('PeraWallet', () => {
334334
})
335335
})
336336

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+
337384
describe('signing transactions', () => {
338385
// Connected accounts
339386
const connectedAcct1 = '7ZUECA7HFLZTXENRV24SHLU4AVPUTMTTDUFUBNBD64C73F3UHRTHAIOF6Q'

packages/use-wallet/src/wallets/pera.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,8 @@ export class PeraWallet extends BaseWallet {
4747

4848
if (typeof window !== 'undefined' && window.navigator) {
4949
const isPeraDiscover = window.navigator.userAgent.includes('pera')
50-
5150
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()
6052
}
6153
}
6254
}
@@ -66,6 +58,16 @@ export class PeraWallet extends BaseWallet {
6658
icon: ICON
6759
}
6860

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+
6971
private async initializeClient(): Promise<PeraWalletConnect> {
7072
this.logger.info('Initializing client...')
7173
const module = await import('@perawallet/connect')

packages/use-wallet/src/wallets/pera2.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,8 @@ export class PeraWallet extends BaseWallet {
5656

5757
if (typeof window !== 'undefined' && window.navigator) {
5858
const isPeraDiscover = window.navigator.userAgent.includes('pera')
59-
6059
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()
6961
}
7062
}
7163
}
@@ -75,6 +67,16 @@ export class PeraWallet extends BaseWallet {
7567
icon: ICON
7668
}
7769

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+
7880
private async initializeClient(): Promise<PeraWalletConnect> {
7981
this.logger.info('Initializing client...')
8082
const module = await import('@perawallet/connect-beta')

0 commit comments

Comments
 (0)