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

fix: use listMultichainAccounts and set lastSelected for initial account #4494

Merged
108 changes: 83 additions & 25 deletions packages/accounts-controller/src/AccountsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,45 @@ describe('AccountsController', () => {
});

describe('onKeyringStateChange', () => {
it('uses listMultichainAccount', async () => {
montelaidev marked this conversation as resolved.
Show resolved Hide resolved
const messenger = buildMessenger();
mockUUID
.mockReturnValueOnce('mock-id') // call to check if its a new account
.mockReturnValueOnce('mock-id2'); // call to add account

const mockNewKeyringState = {
isUnlocked: true,
keyrings: [
{
type: KeyringTypes.hd,
accounts: [mockAccount.address],
},
],
};

montelaidev marked this conversation as resolved.
Show resolved Hide resolved
const { accountsController } = setupAccountsController({
initialState: {
internalAccounts: {
accounts: {},
selectedAccount: '',
},
},
messenger,
});

const listMultichainAccountsSpy = jest.spyOn(
accountsController,
'listMultichainAccounts',
);

messenger.publish(
'KeyringController:stateChange',
mockNewKeyringState,
[],
);

expect(listMultichainAccountsSpy).toHaveBeenCalled();
});
it('not update state when only keyring is unlocked without any keyrings', async () => {
const messenger = buildMessenger();
const { accountsController } = setupAccountsController({
Expand All @@ -463,7 +502,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([]);
});
Expand Down Expand Up @@ -496,7 +535,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([]);
});
Expand Down Expand Up @@ -537,7 +576,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -595,7 +634,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -661,7 +700,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -709,7 +748,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -774,7 +813,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts.map(setLastSelectedAsAny)).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -872,7 +911,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccount,
Expand Down Expand Up @@ -915,7 +954,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([setLastSelectedAsAny(mockAccount2)]);
expect(accountsController.getSelectedAccount()).toStrictEqual(
Expand Down Expand Up @@ -968,7 +1007,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
setLastSelectedAsAny(mockAccount),
Expand Down Expand Up @@ -1031,7 +1070,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
setLastSelectedAsAny(mockAccount),
Expand All @@ -1043,6 +1082,7 @@ describe('AccountsController', () => {
});

it('delete the account and select the account with the most recent lastSelected', async () => {
const currentTime = Date.now();
montelaidev marked this conversation as resolved.
Show resolved Hide resolved
const messenger = buildMessenger();
mockUUID.mockReturnValueOnce('mock-id').mockReturnValueOnce('mock-id2');

Expand Down Expand Up @@ -1097,14 +1137,22 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();

expect(accounts).toStrictEqual([
mockAccountWithoutLastSelected,
{
...mockAccountWithoutLastSelected,
metadata: {
...mockAccountWithoutLastSelected.metadata,
lastSelected: expect.any(Number),
},
},
mockAccount2WithoutLastSelected,
]);
expect(accountsController.getSelectedAccount()).toStrictEqual(
mockAccountWithoutLastSelected,

const selectedAccount = accountsController.getSelectedAccount();
expect(selectedAccount.metadata.lastSelected).toBeGreaterThanOrEqual(
currentTime,
);
});
});
Expand Down Expand Up @@ -1155,7 +1203,7 @@ describe('AccountsController', () => {
);

const selectedAccount = accountsController.getSelectedAccount();
const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();
const expectedAccount = setLastSelectedAsAny(mockReinitialisedAccount);

expect(selectedAccount).toStrictEqual(expectedAccount);
Expand Down Expand Up @@ -1340,7 +1388,9 @@ describe('AccountsController', () => {

await accountsController.updateAccounts();

expect(accountsController.listAccounts()).toStrictEqual(expectedAccounts);
expect(accountsController.listMultichainAccounts()).toStrictEqual(
expectedAccounts,
);
});

it('update accounts with Snap accounts when snap keyring is defined and has accounts', async () => {
Expand Down Expand Up @@ -1395,7 +1445,7 @@ describe('AccountsController', () => {
await accountsController.updateAccounts();

expect(
accountsController.listAccounts().map(setLastSelectedAsAny),
accountsController.listMultichainAccounts().map(setLastSelectedAsAny),
).toStrictEqual(expectedAccounts);
});

Expand Down Expand Up @@ -1425,7 +1475,9 @@ describe('AccountsController', () => {

await accountsController.updateAccounts();

expect(accountsController.listAccounts()).toStrictEqual(expectedAccounts);
expect(accountsController.listMultichainAccounts()).toStrictEqual(
expectedAccounts,
);
});

it('set the account with the correct index', async () => {
Expand Down Expand Up @@ -1474,7 +1526,9 @@ describe('AccountsController', () => {

await accountsController.updateAccounts();

expect(accountsController.listAccounts()).toStrictEqual(expectedAccounts);
expect(accountsController.listMultichainAccounts()).toStrictEqual(
expectedAccounts,
);
});

it('filter Snap accounts from normalAccounts', async () => {
Expand Down Expand Up @@ -1529,7 +1583,9 @@ describe('AccountsController', () => {

await accountsController.updateAccounts();

expect(accountsController.listAccounts()).toStrictEqual(expectedAccounts);
expect(accountsController.listMultichainAccounts()).toStrictEqual(
expectedAccounts,
);
});

it('filter Snap accounts from normalAccounts even if the snap account is listed before normal accounts', async () => {
Expand Down Expand Up @@ -1585,7 +1641,9 @@ describe('AccountsController', () => {

await accountsController.updateAccounts();

expect(accountsController.listAccounts()).toStrictEqual(expectedAccounts);
expect(accountsController.listMultichainAccounts()).toStrictEqual(
expectedAccounts,
);
});

it.each([
Expand Down Expand Up @@ -1641,7 +1699,7 @@ describe('AccountsController', () => {
await accountsController.updateAccounts();

expect(
accountsController.listAccounts().map(setLastSelectedAsAny),
accountsController.listMultichainAccounts().map(setLastSelectedAsAny),
).toStrictEqual(expectedAccounts);
});

Expand Down Expand Up @@ -2253,7 +2311,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();
expect(accounts).toStrictEqual([
mockAccount,
setLastSelectedAsAny(mockSimpleKeyring1),
Expand Down Expand Up @@ -2310,7 +2368,7 @@ describe('AccountsController', () => {
[],
);

const accounts = accountsController.listAccounts();
const accounts = accountsController.listMultichainAccounts();
expect(accounts).toStrictEqual([
mockAccount,
setLastSelectedAsAny(mockSimpleKeyring2),
Expand Down
Loading
Loading