@@ -29,7 +29,6 @@ const mockSetSelectedAccount = jest.fn();
2929const mockRemoveAccountHelper = jest . fn ( ) ;
3030const mockGetAccountByAddress = jest . fn ( ) ;
3131const mockSetAccountName = jest . fn ( ) ;
32- const mockSetAccountNameAndSelectAccount = jest . fn ( ) ;
3332const mockSnapControllerHandleRequest = jest . fn ( ) ;
3433const mockListMultichainAccounts = jest . fn ( ) ;
3534
@@ -85,7 +84,6 @@ const createControllerMessenger = ({
8584 'AccountsController:getAccountByAddress' ,
8685 'AccountsController:listMultichainAccounts' ,
8786 'AccountsController:setAccountName' ,
88- 'AccountsController:setAccountNameAndSelectAccount' ,
8987 ] ,
9088 allowedEvents : [ ] ,
9189 } ) ;
@@ -110,8 +108,6 @@ const createControllerMessenger = ({
110108 return mockSetSelectedAccount ( params ) ;
111109 case 'AccountsController:setAccountName' :
112110 return mockSetAccountName . mockReturnValue ( null ) ( params ) ;
113- case 'AccountsController:setAccountNameAndSelectAccount' :
114- return mockSetAccountNameAndSelectAccount . mockReturnValue ( null ) ( params ) ;
115111 case 'AccountsController:listMultichainAccounts' :
116112 return mockListMultichainAccounts . mockReturnValue ( [ ] ) ( ) ;
117113 case 'SnapController:handleRequest' :
@@ -221,12 +217,16 @@ describe('Snap Keyring Methods', () => {
221217 ] ) ;
222218 expect ( mockPersisKeyringHelper ) . toHaveBeenCalledTimes ( 1 ) ;
223219 expect ( mockGetAccounts ) . toHaveBeenCalledTimes ( 1 ) ;
224- expect ( mockSetAccountNameAndSelectAccount ) . not . toHaveBeenCalled ( ) ;
225220 expect ( mockEndFlow ) . toHaveBeenCalledWith ( [ { id : mockFlowId } ] ) ;
226221
227222 // Wait for any pending promises (including the account finalization which tracks the event)
228223 await waitForAllPromises ( ) ;
229224
225+ // Verify that setSelectedAccount is called but setAccountName is not called
226+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
227+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
228+ expect ( mockSetAccountName ) . not . toHaveBeenCalled ( ) ;
229+
230230 // Verify trackSnapAccountEvent was called for successful account creation
231231 expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
232232 } ) ;
@@ -260,17 +260,21 @@ describe('Snap Keyring Methods', () => {
260260 true ,
261261 ] ) ;
262262 expect ( mockGetAccounts ) . toHaveBeenCalledTimes ( 1 ) ;
263- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledTimes ( 1 ) ;
264- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledWith ( [
263+
264+ // Wait for any pending promises (including the account finalization which tracks the event)
265+ await waitForAllPromises ( ) ;
266+
267+ // Verify that setSelectedAccount and setAccountName are called separately
268+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
269+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
270+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
271+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
265272 mockAccount . id ,
266273 mockNameSuggestion ,
267274 ] ) ;
268275 expect ( mockEndFlow ) . toHaveBeenCalledTimes ( 2 ) ;
269276 expect ( mockEndFlow ) . toHaveBeenCalledWith ( [ { id : mockFlowId } ] ) ;
270277
271- // Wait for any pending promises (including the account finalization which tracks the event)
272- await waitForAllPromises ( ) ;
273-
274278 // Verify trackSnapAccountEvent was called
275279 expect ( trackSnapAccountEvent ) . toHaveBeenCalled ( ) ;
276280 } ) ;
@@ -402,8 +406,15 @@ describe('Snap Keyring Methods', () => {
402406
403407 // Verify that the account was created and named
404408 expect ( mockPersisKeyringHelper ) . toHaveBeenCalledTimes ( 1 ) ;
405- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledTimes ( 1 ) ;
406- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledWith ( [
409+
410+ // Wait for any pending promises (including the account finalization)
411+ await waitForAllPromises ( ) ;
412+
413+ // Verify that setSelectedAccount and setAccountName are called separately
414+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
415+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
416+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
417+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
407418 mockAccount . id ,
408419 mockNameSuggestion ,
409420 ] ) ;
@@ -437,8 +448,15 @@ describe('Snap Keyring Methods', () => {
437448
438449 // Verify that the account was created and named
439450 expect ( mockPersisKeyringHelper ) . toHaveBeenCalledTimes ( 1 ) ;
440- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledTimes ( 1 ) ;
441- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledWith ( [
451+
452+ // Wait for any pending promises (including the account finalization)
453+ await waitForAllPromises ( ) ;
454+
455+ // Verify that setSelectedAccount and setAccountName are called separately
456+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
457+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
458+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
459+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
442460 mockAccount . id ,
443461 mockNameSuggestion ,
444462 ] ) ;
@@ -484,8 +502,90 @@ describe('Snap Keyring Methods', () => {
484502
485503 // Verify that the account was created and named
486504 expect ( mockPersisKeyringHelper ) . toHaveBeenCalledTimes ( 1 ) ;
487- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledTimes ( 1 ) ;
488- expect ( mockSetAccountNameAndSelectAccount ) . toHaveBeenCalledWith ( [
505+
506+ // Wait for any pending promises (including the account finalization)
507+ await waitForAllPromises ( ) ;
508+
509+ // Verify that setSelectedAccount and setAccountName are called separately
510+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
511+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
512+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
513+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
514+ mockAccount . id ,
515+ mockNameSuggestion ,
516+ ] ) ;
517+ } ) ;
518+
519+ it ( 'always sets selected account for both preinstalled and non-preinstalled snaps with default options' , async ( ) => {
520+ // Test with preinstalled snap first
521+ ( isSnapPreinstalled as jest . Mock ) . mockReturnValue ( true ) ;
522+
523+ const mockNameSuggestion = 'suggested name' ;
524+ mockAddRequest . mockReturnValueOnce ( {
525+ success : true ,
526+ name : mockNameSuggestion ,
527+ } ) ;
528+
529+ const builder = createSnapKeyringBuilder ( ) ;
530+ await builder ( ) . handleKeyringSnapMessage ( mockSnapId , {
531+ method : KeyringEvent . AccountCreated ,
532+ params : {
533+ account : mockAccount ,
534+ displayConfirmation : false ,
535+ accountNameSuggestion : mockNameSuggestion ,
536+ } ,
537+ } ) ;
538+
539+ // Wait for any pending promises (including the account finalization)
540+ await waitForAllPromises ( ) ;
541+
542+ // Verify that both setSelectedAccount and setAccountName are called for preinstalled snap
543+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
544+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
545+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
546+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
547+ mockAccount . id ,
548+ mockNameSuggestion ,
549+ ] ) ;
550+
551+ // Reset mocks for second test and set them up again
552+ mockSetSelectedAccount . mockReset ( ) ;
553+ mockSetAccountName . mockReset ( ) ;
554+ mockAddRequest . mockReset ( ) ;
555+ mockPersisKeyringHelper . mockReset ( ) ;
556+ mockStartFlow . mockReset ( ) ;
557+ mockEndFlow . mockReset ( ) ;
558+ mockGetAccounts . mockReset ( ) ;
559+
560+ // Set up mocks for second test
561+ mockAddRequest . mockReturnValueOnce ( {
562+ success : true ,
563+ name : mockNameSuggestion ,
564+ } ) ;
565+ mockStartFlow . mockReturnValue ( { id : mockFlowId } ) ;
566+ mockEndFlow . mockReturnValue ( true ) ;
567+ mockGetAccounts . mockResolvedValue ( [ ] ) ;
568+
569+ // Test with non-preinstalled snap
570+ ( isSnapPreinstalled as jest . Mock ) . mockReturnValue ( false ) ;
571+
572+ await builder ( ) . handleKeyringSnapMessage ( mockSnapId , {
573+ method : KeyringEvent . AccountCreated ,
574+ params : {
575+ account : mockAccount ,
576+ displayConfirmation : false ,
577+ accountNameSuggestion : mockNameSuggestion ,
578+ } ,
579+ } ) ;
580+
581+ // Wait for any pending promises (including the account finalization)
582+ await waitForAllPromises ( ) ;
583+
584+ // Verify that both setSelectedAccount and setAccountName are called for non-preinstalled snap too
585+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledTimes ( 1 ) ;
586+ expect ( mockSetSelectedAccount ) . toHaveBeenCalledWith ( [ mockAccount . id ] ) ;
587+ expect ( mockSetAccountName ) . toHaveBeenCalledTimes ( 1 ) ;
588+ expect ( mockSetAccountName ) . toHaveBeenCalledWith ( [
489589 mockAccount . id ,
490590 mockNameSuggestion ,
491591 ] ) ;
0 commit comments