Skip to content

Commit

Permalink
Merge branch 'main' into fix/add-threshold-for-update-nft-metadata-fct
Browse files Browse the repository at this point in the history
  • Loading branch information
sahar-fehri authored Jan 16, 2025
2 parents 023dd64 + 875e8a1 commit 60437d3
Showing 1 changed file with 65 additions and 70 deletions.
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 @@ -133,9 +133,6 @@ const mockAccount4: InternalAccount = {
},
};

/**
* 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`

Expand All @@ -158,6 +155,18 @@ class MockNormalAccountUUID {
}
}

/**
* 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('AccountsController', () => {
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('AccountsController', () => {
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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {
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('AccountsController', () => {
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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {
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 @@ describe('AccountsController', () => {
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 @@ describe('AccountsController', () => {
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 @@ describe('AccountsController', () => {
});
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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
keyringType: KeyringTypes.hd,
}),
];
mockUUIDWithNormalAccounts(expectedAccounts);

await accountsController.updateAccounts();

Expand Down Expand Up @@ -1588,7 +1581,8 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
keyringType: KeyringTypes.hd,
}),
];
mockUUIDWithNormalAccounts(expectedAccounts);

await accountsController.updateAccounts();

Expand All @@ -1639,7 +1634,8 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
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 @@ describe('AccountsController', () => {
});

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 @@ describe('AccountsController', () => {
});
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 @@ describe('AccountsController', () => {

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 @@ describe('AccountsController', () => {

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

0 comments on commit 60437d3

Please sign in to comment.