@@ -2719,6 +2719,116 @@ describe('AccountsController', () => {
2719
2719
} ) ;
2720
2720
} ) ;
2721
2721
2722
+ describe ( 'setAccountNameAndSelect' , ( ) => {
2723
+ const newAccountName = 'New Account Name' ;
2724
+ const mockState = {
2725
+ initialState : {
2726
+ internalAccounts : {
2727
+ accounts : { [ mockAccount . id ] : mockAccount } ,
2728
+ selectedAccount : mockAccount . id ,
2729
+ } ,
2730
+ } ,
2731
+ } ;
2732
+
2733
+ it ( 'sets the name of an existing account' , ( ) => {
2734
+ const { accountsController } = setupAccountsController ( mockState ) ;
2735
+
2736
+ accountsController . setAccountNameAndSelectAccount (
2737
+ mockAccount . id ,
2738
+ newAccountName ,
2739
+ ) ;
2740
+
2741
+ expect (
2742
+ accountsController . getAccountExpect ( mockAccount . id ) . metadata . name ,
2743
+ ) . toBe ( newAccountName ) ;
2744
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
2745
+ mockAccount . id ,
2746
+ ) ;
2747
+ } ) ;
2748
+
2749
+ it ( 'sets the name of an existing account and select the account' , ( ) => {
2750
+ const { accountsController } = setupAccountsController ( {
2751
+ initialState : {
2752
+ internalAccounts : {
2753
+ accounts : {
2754
+ [ mockAccount . id ] : mockAccount ,
2755
+ [ mockAccount2 . id ] : mockAccount2 ,
2756
+ } ,
2757
+ selectedAccount : mockAccount . id ,
2758
+ } ,
2759
+ } ,
2760
+ } ) ;
2761
+
2762
+ accountsController . setAccountNameAndSelectAccount (
2763
+ mockAccount2 . id ,
2764
+ newAccountName ,
2765
+ ) ;
2766
+
2767
+ expect (
2768
+ accountsController . getAccountExpect ( mockAccount2 . id ) . metadata . name ,
2769
+ ) . toBe ( newAccountName ) ;
2770
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
2771
+ mockAccount2 . id ,
2772
+ ) ;
2773
+ } ) ;
2774
+
2775
+ it ( 'sets the nameLastUpdatedAt timestamp when setting the name of an existing account' , ( ) => {
2776
+ const expectedTimestamp = Number ( new Date ( '2024-01-02' ) ) ;
2777
+
2778
+ jest . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => expectedTimestamp ) ;
2779
+
2780
+ const { accountsController } = setupAccountsController ( mockState ) ;
2781
+
2782
+ accountsController . setAccountNameAndSelectAccount (
2783
+ mockAccount . id ,
2784
+ newAccountName ,
2785
+ ) ;
2786
+
2787
+ expect (
2788
+ accountsController . getAccountExpect ( mockAccount . id ) . metadata
2789
+ . nameLastUpdatedAt ,
2790
+ ) . toBe ( expectedTimestamp ) ;
2791
+ } ) ;
2792
+
2793
+ it ( 'publishes the accountRenamed event' , ( ) => {
2794
+ const { accountsController, messenger } =
2795
+ setupAccountsController ( mockState ) ;
2796
+
2797
+ const messengerSpy = jest . spyOn ( messenger , 'publish' ) ;
2798
+
2799
+ accountsController . setAccountNameAndSelectAccount (
2800
+ mockAccount . id ,
2801
+ newAccountName ,
2802
+ ) ;
2803
+
2804
+ expect ( messengerSpy ) . toHaveBeenCalledWith (
2805
+ 'AccountsController:accountRenamed' ,
2806
+ accountsController . getAccountExpect ( mockAccount . id ) ,
2807
+ ) ;
2808
+ } ) ;
2809
+
2810
+ it ( 'throw an error if the account name already exists' , ( ) => {
2811
+ const { accountsController } = setupAccountsController ( {
2812
+ initialState : {
2813
+ internalAccounts : {
2814
+ accounts : {
2815
+ [ mockAccount . id ] : mockAccount ,
2816
+ [ mockAccount2 . id ] : mockAccount2 ,
2817
+ } ,
2818
+ selectedAccount : mockAccount . id ,
2819
+ } ,
2820
+ } ,
2821
+ } ) ;
2822
+
2823
+ expect ( ( ) =>
2824
+ accountsController . setAccountNameAndSelectAccount (
2825
+ mockAccount . id ,
2826
+ mockAccount2 . metadata . name ,
2827
+ ) ,
2828
+ ) . toThrow ( 'Account name already exists' ) ;
2829
+ } ) ;
2830
+ } ) ;
2831
+
2722
2832
describe ( 'setAccountName' , ( ) => {
2723
2833
it ( 'sets the name of an existing account' , ( ) => {
2724
2834
const { accountsController } = setupAccountsController ( {
@@ -3033,6 +3143,10 @@ describe('AccountsController', () => {
3033
3143
jest . spyOn ( AccountsController . prototype , 'getAccountByAddress' ) ;
3034
3144
jest . spyOn ( AccountsController . prototype , 'getSelectedAccount' ) ;
3035
3145
jest . spyOn ( AccountsController . prototype , 'getAccount' ) ;
3146
+ jest . spyOn (
3147
+ AccountsController . prototype ,
3148
+ 'setAccountNameAndSelectAccount' ,
3149
+ ) ;
3036
3150
} ) ;
3037
3151
3038
3152
describe ( 'setSelectedAccount' , ( ) => {
@@ -3142,6 +3256,37 @@ describe('AccountsController', () => {
3142
3256
} ) ;
3143
3257
} ) ;
3144
3258
3259
+ describe ( 'setAccountNameAndSelectAccount' , ( ) => {
3260
+ it ( 'set the account name and select the account' , async ( ) => {
3261
+ const messenger = buildMessenger ( ) ;
3262
+ const { accountsController } = setupAccountsController ( {
3263
+ initialState : {
3264
+ internalAccounts : {
3265
+ accounts : {
3266
+ [ mockAccount . id ] : mockAccount ,
3267
+ [ mockAccount2 . id ] : mockAccount2 ,
3268
+ } ,
3269
+ selectedAccount : mockAccount . id ,
3270
+ } ,
3271
+ } ,
3272
+ messenger,
3273
+ } ) ;
3274
+
3275
+ const newAccountName = 'New Account Name' ;
3276
+ messenger . call (
3277
+ 'AccountsController:setAccountNameAndSelectAccount' ,
3278
+ mockAccount2 . id ,
3279
+ newAccountName ,
3280
+ ) ;
3281
+ expect (
3282
+ accountsController . setAccountNameAndSelectAccount ,
3283
+ ) . toHaveBeenCalledWith ( mockAccount2 . id , newAccountName ) ;
3284
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
3285
+ mockAccount2 . id ,
3286
+ ) ;
3287
+ } ) ;
3288
+ } ) ;
3289
+
3145
3290
describe ( 'updateAccounts' , ( ) => {
3146
3291
it ( 'update accounts' , async ( ) => {
3147
3292
const messenger = buildMessenger ( ) ;
0 commit comments