Skip to content

Commit aabb780

Browse files
committed
test(accounts-controller): add test for setAccountNameAndSelectAccount
1 parent f2d8196 commit aabb780

File tree

2 files changed

+150
-3
lines changed

2 files changed

+150
-3
lines changed

packages/accounts-controller/src/AccountsController.test.ts

+145-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Messenger } from '@metamask/base-controller';
2-
import { InfuraNetworkType } from '@metamask/controller-utils';
32
import type {
43
AccountAssetListUpdatedEventPayload,
54
AccountBalancesUpdatedEventPayload,
@@ -2719,6 +2718,116 @@ describe('AccountsController', () => {
27192718
});
27202719
});
27212720

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+
27222831
describe('setAccountName', () => {
27232832
it('sets the name of an existing account', () => {
27242833
const { accountsController } = setupAccountsController({
@@ -3033,6 +3142,10 @@ describe('AccountsController', () => {
30333142
jest.spyOn(AccountsController.prototype, 'getAccountByAddress');
30343143
jest.spyOn(AccountsController.prototype, 'getSelectedAccount');
30353144
jest.spyOn(AccountsController.prototype, 'getAccount');
3145+
jest.spyOn(
3146+
AccountsController.prototype,
3147+
'setAccountNameAndSelectAccount',
3148+
);
30363149
});
30373150

30383151
describe('setSelectedAccount', () => {
@@ -3142,6 +3255,37 @@ describe('AccountsController', () => {
31423255
});
31433256
});
31443257

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+
31453289
describe('updateAccounts', () => {
31463290
it('update accounts', async () => {
31473291
const messenger = buildMessenger();

packages/accounts-controller/src/AccountsController.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,12 @@ export class AccountsController extends BaseController<
469469
// deep and possibly infinite.ts(2589)" (https://github.com/MetaMask/utils/issues/168)
470470
// Using a type-cast workaround this error and is slightly better than using a @ts-expect-error
471471
// which sometimes fail when compiling locally.
472-
internalAccounts.accounts[accountId] = internalAccount;
472+
internalAccounts.accounts[account.id] = internalAccount;
473473

474-
internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
474+
internalAccounts.accounts[account.id].metadata.lastSelected =
475+
this.#getLastSelectedIndex();
476+
internalAccounts.accounts[account.id].metadata.nameLastUpdatedAt =
477+
Date.now();
475478
internalAccounts.selectedAccount = account.id;
476479
});
477480

0 commit comments

Comments
 (0)