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