@@ -16,6 +16,7 @@ import { SNAP_MANAGE_ACCOUNTS_CONFIRMATION_TYPES } from '../RPCMethods/RPCMethod
16
16
import { showAccountNameSuggestionDialog } from './utils/showDialog' ;
17
17
import Logger from '../../util/Logger' ;
18
18
import { isSnapPreinstalled } from './utils/snaps' ;
19
+ import { trackSnapAccountEvent } from '../Analytics/helpers/SnapKeyring/trackSnapAccountEvent' ;
19
20
20
21
const mockAddRequest = jest . fn ( ) ;
21
22
const mockStartFlow = jest . fn ( ) ;
@@ -144,6 +145,12 @@ const createSnapKeyringBuilder = () =>
144
145
// Mock the isSnapPreinstalled function
145
146
jest . mock ( './utils/snaps' , ( ) => ( {
146
147
isSnapPreinstalled : jest . fn ( ) ,
148
+ getSnapName : jest . fn ( ) . mockReturnValue ( 'Mock Snap Name' ) ,
149
+ } ) ) ;
150
+
151
+ // Mock the trackSnapAccountEvent function
152
+ jest . mock ( '../Analytics/helpers/SnapKeyring/trackSnapAccountEvent' , ( ) => ( {
153
+ trackSnapAccountEvent : jest . fn ( ) ,
147
154
} ) ) ;
148
155
149
156
describe ( 'Snap Keyring Methods' , ( ) => {
@@ -212,6 +219,12 @@ describe('Snap Keyring Methods', () => {
212
219
expect ( mockGetAccounts ) . toHaveBeenCalledTimes ( 1 ) ;
213
220
expect ( mockSetAccountName ) . not . toHaveBeenCalled ( ) ;
214
221
expect ( mockEndFlow ) . toHaveBeenCalledWith ( [ { id : mockFlowId } ] ) ;
222
+
223
+ // Wait for any pending promises (including the account finalization which tracks the event)
224
+ await waitForAllPromises ( ) ;
225
+
226
+ // Verify trackSnapAccountEvent was called for successful account creation
227
+ expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
215
228
} ) ;
216
229
217
230
it ( 'handles account creation with user defined name' , async ( ) => {
@@ -250,6 +263,12 @@ describe('Snap Keyring Methods', () => {
250
263
] ) ;
251
264
expect ( mockEndFlow ) . toHaveBeenCalledTimes ( 2 ) ;
252
265
expect ( mockEndFlow ) . toHaveBeenCalledWith ( [ { id : mockFlowId } ] ) ;
266
+
267
+ // Wait for any pending promises (including the account finalization which tracks the event)
268
+ await waitForAllPromises ( ) ;
269
+
270
+ // Verify trackSnapAccountEvent was called
271
+ expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
253
272
} ) ;
254
273
255
274
it ( 'throws an error when user denies account creation' , async ( ) => {
@@ -342,6 +361,7 @@ describe('Snap Keyring Methods', () => {
342
361
expect ( mockEndFlow ) . toHaveBeenCalledTimes ( 2 ) ;
343
362
expect ( mockEndFlow ) . toHaveBeenNthCalledWith ( 1 , [ { id : mockFlowId } ] ) ;
344
363
expect ( mockEndFlow ) . toHaveBeenNthCalledWith ( 2 , [ { id : mockFlowId } ] ) ;
364
+ expect ( trackSnapAccountEvent ) . not . toHaveBeenCalled ( ) ;
345
365
} ) ;
346
366
it ( 'skips account name suggestion dialog for preinstalled snaps when displayAccountNameSuggestion is false' , async ( ) => {
347
367
// Mock isSnapPreinstalled to return true for this test
@@ -467,4 +487,95 @@ describe('Snap Keyring Methods', () => {
467
487
] ) ;
468
488
} ) ;
469
489
} ) ;
490
+ describe ( 'removeAccount' , ( ) => {
491
+ beforeEach ( ( ) => {
492
+ mockAddRequest . mockReturnValue ( true ) . mockReturnValue ( { success : true } ) ;
493
+ ( isSnapPreinstalled as jest . Mock ) . mockReset ( ) ;
494
+ } ) ;
495
+ afterEach ( ( ) => {
496
+ jest . resetAllMocks ( ) ;
497
+ } ) ;
498
+
499
+ it ( 'calls removeAccountHelper and persistKeyringHelper when account is deleted' , async ( ) => {
500
+ const builder = createSnapKeyringBuilder ( ) ;
501
+ const snapKeyring = builder ( ) ;
502
+
503
+ // First add an account to the keyring so that it can be removed
504
+ // NOTE: This callback will not be triggered if there are no accounts in the keyring
505
+ await snapKeyring . handleKeyringSnapMessage ( mockSnapId , {
506
+ method : KeyringEvent . AccountCreated ,
507
+ params : {
508
+ account : mockAccount ,
509
+ displayConfirmation : false ,
510
+ } ,
511
+ } ) ;
512
+
513
+ // Reset mocks after account creation
514
+ mockRemoveAccountHelper . mockReset ( ) ;
515
+ mockPersisKeyringHelper . mockReset ( ) ;
516
+
517
+ // Now delete the account
518
+ await snapKeyring . handleKeyringSnapMessage ( mockSnapId , {
519
+ method : KeyringEvent . AccountDeleted ,
520
+ params : {
521
+ id : mockAccount . id ,
522
+ } ,
523
+ } ) ;
524
+
525
+ expect ( mockRemoveAccountHelper ) . toHaveBeenCalledTimes ( 1 ) ;
526
+ expect ( mockRemoveAccountHelper ) . toHaveBeenCalledWith (
527
+ mockAccount . address . toLowerCase ( ) ,
528
+ ) ;
529
+ expect ( mockPersisKeyringHelper ) . toHaveBeenCalledTimes ( 2 ) ;
530
+
531
+ // Verify trackSnapAccountEvent was called
532
+ expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
533
+ } ) ;
534
+
535
+ it ( 'handles errors when removing an account' , async ( ) => {
536
+ const loggerSpy = jest . spyOn ( Logger , 'error' ) . mockImplementation ( ) ;
537
+
538
+ // Set up mock to throw an error
539
+ const errorMessage = 'Failed to remove account' ;
540
+ mockRemoveAccountHelper . mockRejectedValue ( new Error ( errorMessage ) ) ;
541
+
542
+ const builder = createSnapKeyringBuilder ( ) ;
543
+ const snapKeyring = builder ( ) ;
544
+
545
+ // First add an account to the keyring so that it can be removed
546
+ await snapKeyring . handleKeyringSnapMessage ( mockSnapId , {
547
+ method : KeyringEvent . AccountCreated ,
548
+ params : {
549
+ account : mockAccount ,
550
+ displayConfirmation : false ,
551
+ } ,
552
+ } ) ;
553
+
554
+ // Reset mocks after account creation
555
+ mockRemoveAccountHelper . mockReset ( ) ;
556
+ mockPersisKeyringHelper . mockReset ( ) ;
557
+ mockRemoveAccountHelper . mockRejectedValue ( new Error ( errorMessage ) ) ;
558
+
559
+ // Expect the error to be thrown
560
+ await expect (
561
+ snapKeyring . handleKeyringSnapMessage ( mockSnapId , {
562
+ method : KeyringEvent . AccountDeleted ,
563
+ params : {
564
+ id : mockAccount . id ,
565
+ } ,
566
+ } ) ,
567
+ ) . rejects . toThrow ( errorMessage ) ;
568
+
569
+ // Verify error was logged
570
+ expect ( loggerSpy ) . toHaveBeenCalledWith (
571
+ expect . objectContaining ( { message : errorMessage } ) ,
572
+ expect . stringContaining (
573
+ `Error removing snap account: ${ mockAccount . address . toLowerCase ( ) } ` ,
574
+ ) ,
575
+ ) ;
576
+
577
+ // Verify trackSnapAccountEvent was called for error case
578
+ expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
579
+ } ) ;
580
+ } ) ;
470
581
} ) ;
0 commit comments