Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(accounts-controller): improve uuid mocks #5005

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 65 additions & 70 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import type { SnapControllerState } from '@metamask/snaps-controllers';
import { SnapStatus } from '@metamask/snaps-utils';
import type { CaipChainId } from '@metamask/utils';
import * as uuid from 'uuid';

Check warning on line 18 in packages/accounts-controller/src/AccountsController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

No exported names found in module 'uuid'
import type { V4Options } from 'uuid';

import type {
Expand Down Expand Up @@ -133,11 +133,8 @@
},
};

/**
* Mock generated normal account ID to an actual "hard-coded" one.
*/
class MockNormalAccountUUID {
#accountIds: Record<string, string> = {};

Check warning on line 137 in packages/accounts-controller/src/AccountsController.test.ts

View workflow job for this annotation

GitHub Actions / Lint, build, and test / Lint (20.x)

Member '#accountIds' is never reassigned; mark it as `readonly`

constructor(accounts: InternalAccount[]) {
for (const account of accounts) {
Expand All @@ -158,6 +155,18 @@
}
}

/**
* Mock generated normal account ID to their actual mock ID. This function will
* automatically attaches those accounts to `mockUUID`. A random UUID will be
* generated if an account has not been registered. See {@link MockNormalAccountUUID}.
*
* @param accounts - List of normal accounts to map with their mock ID.
*/
function mockUUIDWithNormalAccounts(accounts: InternalAccount[]) {
const mockAccountUUIDs = new MockNormalAccountUUID(accounts);
mockUUID.mockImplementation(mockAccountUUIDs.mock.bind(mockAccountUUIDs));
}

/**
* Creates an `InternalAccount` object from the given normal account properties.
*
Expand Down Expand Up @@ -466,9 +475,8 @@
describe('onKeyringStateChange', () => {
it('uses listMultichainAccounts', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const { accountsController } = setupAccountsController({
initialState: {
Expand Down Expand Up @@ -562,10 +570,8 @@
describe('adding accounts', () => {
it('add new accounts', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2, mockAccount3]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -604,7 +610,7 @@
});

it('add Snap accounts', async () => {
mockUUID.mockReturnValueOnce('mock-id'); // call to check if its a new account
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
Expand Down Expand Up @@ -671,7 +677,8 @@
});

it('handle the event when a Snap deleted the account before the it was added', async () => {
mockUUID.mockReturnValueOnce('mock-id'); // call to check if its a new account
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
'KeyringController:getKeyringsByType',
Expand Down Expand Up @@ -729,11 +736,8 @@

it('increment the default account number when adding an account', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id3') // call to check if its a new account
.mockReturnValueOnce('mock-id3'); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2, mockAccount3]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -785,11 +789,8 @@

it('use the next number after the total number of accounts of a keyring when adding an account, if the index is lower', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id3') // call to check if its a new account
.mockReturnValueOnce('mock-id3'); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2, mockAccount3]);

const mockAccount2WithCustomName = createExpectedInternalAccount({
id: 'mock-id2',
Expand Down Expand Up @@ -847,7 +848,7 @@
});

it('handle when the account to set as selectedAccount is undefined', async () => {
mockUUID.mockReturnValueOnce('mock-id'); // call to check if its a new account
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
Expand Down Expand Up @@ -897,10 +898,8 @@

it('selectedAccount remains the same after adding a new account', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2, mockAccount3]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -942,10 +941,8 @@
it('publishes accountAdded event', async () => {
const messenger = buildMessenger();
const messengerSpy = jest.spyOn(messenger, 'publish');
mockUUID
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

setupAccountsController({
initialState: {
Expand Down Expand Up @@ -987,7 +984,8 @@
describe('deleting account', () => {
it('delete accounts if its gone from the keyring state', async () => {
const messenger = buildMessenger();
mockUUID.mockReturnValueOnce('mock-id2');

mockUUIDWithNormalAccounts([mockAccount2]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -1027,11 +1025,8 @@

it('delete accounts and set the most recent lastSelected account', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id')
.mockReturnValueOnce('mock-id2')
.mockReturnValueOnce('mock-id')
.mockReturnValueOnce('mock-id2');

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -1083,11 +1078,8 @@

it('delete accounts and set the most recent lastSelected account when there are accounts that have never been selected', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id')
.mockReturnValueOnce('mock-id2')
.mockReturnValueOnce('mock-id')
.mockReturnValueOnce('mock-id2');

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const mockAccount2WithoutLastSelected = {
...mockAccount2,
Expand Down Expand Up @@ -1147,7 +1139,8 @@
it('delete the account and select the account with the most recent lastSelected', async () => {
const currentTime = Date.now();
const messenger = buildMessenger();
mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2');

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const mockAccountWithoutLastSelected = {
...mockAccount,
Expand Down Expand Up @@ -1222,10 +1215,8 @@
it('publishes accountRemoved event', async () => {
const messenger = buildMessenger();
const messengerSpy = jest.spyOn(messenger, 'publish');
mockUUID
.mockReturnValueOnce(mockAccount.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id) // call to check if its a new account
.mockReturnValueOnce(mockAccount2.id); // call to add account

mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

setupAccountsController({
initialState: {
Expand Down Expand Up @@ -1278,9 +1269,11 @@
address: '0x456',
keyringType: KeyringTypes.hd,
});
mockUUID
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to add account

mockUUIDWithNormalAccounts([
mockInitialAccount,
mockReinitialisedAccount,
]);

const mockNewKeyringState = {
isUnlocked: true,
Expand Down Expand Up @@ -1361,9 +1354,7 @@
});
mockExistingAccount2.metadata.lastSelected = lastSelectedForAccount2;

mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to check if its a new account
mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const { accountsController } = setupAccountsController({
initialState: {
Expand Down Expand Up @@ -1447,7 +1438,8 @@
});

it('update accounts with normal accounts', async () => {
mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2');
mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

const messenger = buildMessenger();
messenger.registerActionHandler(
'KeyringController:getAccounts',
Expand Down Expand Up @@ -1492,6 +1484,7 @@
keyringType: KeyringTypes.hd,
}),
];
mockUUIDWithNormalAccounts(expectedAccounts);

await accountsController.updateAccounts();

Expand Down Expand Up @@ -1588,7 +1581,8 @@
});

it('set the account with the correct index', async () => {
mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2');
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
'KeyringController:getAccounts',
Expand Down Expand Up @@ -1630,6 +1624,7 @@
keyringType: KeyringTypes.hd,
}),
];
mockUUIDWithNormalAccounts(expectedAccounts);

await accountsController.updateAccounts();

Expand All @@ -1639,7 +1634,8 @@
});

it('filter Snap accounts from normalAccounts', async () => {
mockUUID.mockReturnValueOnce('mock-id');
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
'KeyringController:getKeyringsByType',
Expand Down Expand Up @@ -1696,7 +1692,8 @@
});

it('filter Snap accounts from normalAccounts even if the snap account is listed before normal accounts', async () => {
mockUUID.mockReturnValue('mock-id');
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
'KeyringController:getKeyringsByType',
Expand Down Expand Up @@ -1762,7 +1759,7 @@
KeyringTypes.qr,
'Custody - JSON - RPC',
])('should add accounts for %s type', async (keyringType) => {
mockUUID.mockReturnValue('mock-id');
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
Expand Down Expand Up @@ -1811,7 +1808,7 @@
});

it('throw an error if the keyring type is unknown', async () => {
mockUUID.mockReturnValue('mock-id');
mockUUIDWithNormalAccounts([mockAccount]);

const messenger = buildMessenger();
messenger.registerActionHandler(
Expand Down Expand Up @@ -1892,9 +1889,7 @@
});
mockExistingAccount2.metadata.lastSelected = lastSelectedForAccount2;

mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to check if its a new account
mockUUIDWithNormalAccounts([mockAccount, mockAccount2]);

messenger.registerActionHandler(
'KeyringController:getKeyringsByType',
Expand Down Expand Up @@ -2544,12 +2539,12 @@

it('return the next account number', async () => {
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to check if its a new account
.mockReturnValueOnce('mock-id3') // call to check if its a new account
.mockReturnValueOnce('mock-id2') // call to add account
.mockReturnValueOnce('mock-id3'); // call to add account

mockUUIDWithNormalAccounts([
mockAccount,
mockSimpleKeyring1,
mockSimpleKeyring2,
]);

const { accountsController } = setupAccountsController({
initialState: {
Expand Down Expand Up @@ -2582,13 +2577,13 @@

it('return the next account number even with an index gap', async () => {
const messenger = buildMessenger();
const mockAccountUUIDs = new MockNormalAccountUUID([

mockUUIDWithNormalAccounts([
mockAccount,
mockSimpleKeyring1,
mockSimpleKeyring2,
mockSimpleKeyring3,
]);
mockUUID.mockImplementation(mockAccountUUIDs.mock.bind(mockAccountUUIDs));

const { accountsController } = setupAccountsController({
initialState: {
Expand Down
Loading