@@ -2783,6 +2783,116 @@ describe('AccountsController', () => {
2783
2783
} ) ;
2784
2784
} ) ;
2785
2785
2786
+ describe ( 'setAccountNameAndSelect' , ( ) => {
2787
+ const newAccountName = 'New Account Name' ;
2788
+ const mockState = {
2789
+ initialState : {
2790
+ internalAccounts : {
2791
+ accounts : { [ mockAccount . id ] : mockAccount } ,
2792
+ selectedAccount : mockAccount . id ,
2793
+ } ,
2794
+ } ,
2795
+ } ;
2796
+
2797
+ it ( 'sets the name of an existing account' , ( ) => {
2798
+ const { accountsController } = setupAccountsController ( mockState ) ;
2799
+
2800
+ accountsController . setAccountNameAndSelectAccount (
2801
+ mockAccount . id ,
2802
+ newAccountName ,
2803
+ ) ;
2804
+
2805
+ expect (
2806
+ accountsController . getAccountExpect ( mockAccount . id ) . metadata . name ,
2807
+ ) . toBe ( newAccountName ) ;
2808
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
2809
+ mockAccount . id ,
2810
+ ) ;
2811
+ } ) ;
2812
+
2813
+ it ( 'sets the name of an existing account and select the account' , ( ) => {
2814
+ const { accountsController } = setupAccountsController ( {
2815
+ initialState : {
2816
+ internalAccounts : {
2817
+ accounts : {
2818
+ [ mockAccount . id ] : mockAccount ,
2819
+ [ mockAccount2 . id ] : mockAccount2 ,
2820
+ } ,
2821
+ selectedAccount : mockAccount . id ,
2822
+ } ,
2823
+ } ,
2824
+ } ) ;
2825
+
2826
+ accountsController . setAccountNameAndSelectAccount (
2827
+ mockAccount2 . id ,
2828
+ newAccountName ,
2829
+ ) ;
2830
+
2831
+ expect (
2832
+ accountsController . getAccountExpect ( mockAccount2 . id ) . metadata . name ,
2833
+ ) . toBe ( newAccountName ) ;
2834
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
2835
+ mockAccount2 . id ,
2836
+ ) ;
2837
+ } ) ;
2838
+
2839
+ it ( 'sets the nameLastUpdatedAt timestamp when setting the name of an existing account' , ( ) => {
2840
+ const expectedTimestamp = Number ( new Date ( '2024-01-02' ) ) ;
2841
+
2842
+ jest . spyOn ( Date , 'now' ) . mockImplementation ( ( ) => expectedTimestamp ) ;
2843
+
2844
+ const { accountsController } = setupAccountsController ( mockState ) ;
2845
+
2846
+ accountsController . setAccountNameAndSelectAccount (
2847
+ mockAccount . id ,
2848
+ newAccountName ,
2849
+ ) ;
2850
+
2851
+ expect (
2852
+ accountsController . getAccountExpect ( mockAccount . id ) . metadata
2853
+ . nameLastUpdatedAt ,
2854
+ ) . toBe ( expectedTimestamp ) ;
2855
+ } ) ;
2856
+
2857
+ it ( 'publishes the accountRenamed event' , ( ) => {
2858
+ const { accountsController, messenger } =
2859
+ setupAccountsController ( mockState ) ;
2860
+
2861
+ const messengerSpy = jest . spyOn ( messenger , 'publish' ) ;
2862
+
2863
+ accountsController . setAccountNameAndSelectAccount (
2864
+ mockAccount . id ,
2865
+ newAccountName ,
2866
+ ) ;
2867
+
2868
+ expect ( messengerSpy ) . toHaveBeenCalledWith (
2869
+ 'AccountsController:accountRenamed' ,
2870
+ accountsController . getAccountExpect ( mockAccount . id ) ,
2871
+ ) ;
2872
+ } ) ;
2873
+
2874
+ it ( 'throw an error if the account name already exists' , ( ) => {
2875
+ const { accountsController } = setupAccountsController ( {
2876
+ initialState : {
2877
+ internalAccounts : {
2878
+ accounts : {
2879
+ [ mockAccount . id ] : mockAccount ,
2880
+ [ mockAccount2 . id ] : mockAccount2 ,
2881
+ } ,
2882
+ selectedAccount : mockAccount . id ,
2883
+ } ,
2884
+ } ,
2885
+ } ) ;
2886
+
2887
+ expect ( ( ) =>
2888
+ accountsController . setAccountNameAndSelectAccount (
2889
+ mockAccount . id ,
2890
+ mockAccount2 . metadata . name ,
2891
+ ) ,
2892
+ ) . toThrow ( 'Account name already exists' ) ;
2893
+ } ) ;
2894
+ } ) ;
2895
+
2786
2896
describe ( 'setAccountName' , ( ) => {
2787
2897
it ( 'sets the name of an existing account' , ( ) => {
2788
2898
const { accountsController } = setupAccountsController ( {
@@ -3097,6 +3207,10 @@ describe('AccountsController', () => {
3097
3207
jest . spyOn ( AccountsController . prototype , 'getAccountByAddress' ) ;
3098
3208
jest . spyOn ( AccountsController . prototype , 'getSelectedAccount' ) ;
3099
3209
jest . spyOn ( AccountsController . prototype , 'getAccount' ) ;
3210
+ jest . spyOn (
3211
+ AccountsController . prototype ,
3212
+ 'setAccountNameAndSelectAccount' ,
3213
+ ) ;
3100
3214
} ) ;
3101
3215
3102
3216
describe ( 'setSelectedAccount' , ( ) => {
@@ -3206,6 +3320,37 @@ describe('AccountsController', () => {
3206
3320
} ) ;
3207
3321
} ) ;
3208
3322
3323
+ describe ( 'setAccountNameAndSelectAccount' , ( ) => {
3324
+ it ( 'set the account name and select the account' , async ( ) => {
3325
+ const messenger = buildMessenger ( ) ;
3326
+ const { accountsController } = setupAccountsController ( {
3327
+ initialState : {
3328
+ internalAccounts : {
3329
+ accounts : {
3330
+ [ mockAccount . id ] : mockAccount ,
3331
+ [ mockAccount2 . id ] : mockAccount2 ,
3332
+ } ,
3333
+ selectedAccount : mockAccount . id ,
3334
+ } ,
3335
+ } ,
3336
+ messenger,
3337
+ } ) ;
3338
+
3339
+ const newAccountName = 'New Account Name' ;
3340
+ messenger . call (
3341
+ 'AccountsController:setAccountNameAndSelectAccount' ,
3342
+ mockAccount2 . id ,
3343
+ newAccountName ,
3344
+ ) ;
3345
+ expect (
3346
+ accountsController . setAccountNameAndSelectAccount ,
3347
+ ) . toHaveBeenCalledWith ( mockAccount2 . id , newAccountName ) ;
3348
+ expect ( accountsController . state . internalAccounts . selectedAccount ) . toBe (
3349
+ mockAccount2 . id ,
3350
+ ) ;
3351
+ } ) ;
3352
+ } ) ;
3353
+
3209
3354
describe ( 'updateAccounts' , ( ) => {
3210
3355
it ( 'update accounts' , async ( ) => {
3211
3356
const messenger = buildMessenger ( ) ;
0 commit comments