Skip to content

feat: remove updateIdentities and make syncIdentities private in preferences controller#3976

Merged
cryptodev-2s merged 8 commits into
mainfrom
refacto/remove-preferences-controller-unused-methods
Feb 27, 2024
Merged

feat: remove updateIdentities and make syncIdentities private in preferences controller#3976
cryptodev-2s merged 8 commits into
mainfrom
refacto/remove-preferences-controller-unused-methods

Conversation

@cryptodev-2s
Copy link
Copy Markdown
Contributor

@cryptodev-2s cryptodev-2s commented Feb 26, 2024

Explanation

After completion of #3794 and #3699, the method syncIdentities is now only used internally on KeyringController:stateChange event and updateIdentities is no longer being used.

References

Changelog

@metamask@preferences-controller

  • REMOVED: syncIdentities is now private as it's only used internally to update state on KeyringController:stateChange event.
  • REMOVED: updateIdentities has been removed, as it's not in use anymore.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate

@cryptodev-2s cryptodev-2s self-assigned this Feb 26, 2024
@cryptodev-2s cryptodev-2s requested a review from a team as a code owner February 26, 2024 15:23
@cryptodev-2s cryptodev-2s changed the title feat: remove updateIdentities and make syncIdentities private in preference controller feat: remove updateIdentities and make syncIdentities private in preferences controller Feb 26, 2024
mcmire
mcmire previously approved these changes Feb 26, 2024
Copy link
Copy Markdown
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense!

Comment on lines -308 to -418
it('should sync identities', () => {
const controller = setupPreferencesController();
controller.addIdentities(['0x00', '0x01']);
controller.syncIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
expect(controller.state.identities['0x00'].importTime).toBeLessThanOrEqual(
Date.now(),
);
expect(controller.state.identities['0x01'].address).toBe('0x01');
expect(controller.state.identities['0x01'].name).toBe('Account 2');
expect(controller.state.identities['0x01'].importTime).toBeLessThanOrEqual(
Date.now(),
);
controller.syncIdentities(['0x00']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
expect(controller.state.selectedAddress).toBe('0x00');
});

it('should throw error when syncing identities with empty array', () => {
const controller = setupPreferencesController();
expect(() => {
controller.syncIdentities([]);
}).toThrow('Expected non-empty array of addresses');
});

it('should add new identities', () => {
const controller = setupPreferencesController();
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
expect(controller.state.identities['0x00'].importTime).toBeLessThanOrEqual(
Date.now(),
);
expect(controller.state.identities['0x01'].address).toBe('0x01');
expect(controller.state.identities['0x01'].name).toBe('Account 2');
expect(controller.state.identities['0x01'].importTime).toBeLessThanOrEqual(
Date.now(),
);
});

it('should not update existing identities', () => {
const controller = setupPreferencesController({
options: {
state: {
identities: { '0x01': { address: '0x01', name: 'Custom name' } },
},
},
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.identities['0x00'].address).toBe('0x00');
expect(controller.state.identities['0x00'].name).toBe('Account 1');
expect(controller.state.identities['0x00'].importTime).toBeLessThanOrEqual(
Date.now(),
);
expect(controller.state.identities['0x01'].address).toBe('0x01');
expect(controller.state.identities['0x01'].name).toBe('Custom name');
expect(controller.state.identities['0x01'].importTime).toBeUndefined();
});

it('should remove identities', () => {
const controller = setupPreferencesController({
options: {
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x00': { address: '0x00', name: 'Account 1' },
},
},
},
});
controller.updateIdentities(['0x00']);
expect(controller.state.identities).toStrictEqual({
'0x00': { address: '0x00', name: 'Account 1' },
});
});

it('should not update selected address if it is still among identities', () => {
const controller = setupPreferencesController({
options: {
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x00': { address: '0x00', name: 'Account 1' },
},
selectedAddress: '0x01',
},
},
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.selectedAddress).toBe('0x01');
});

it('should update selected address to first identity if it was removed from identities', () => {
const controller = setupPreferencesController({
options: {
state: {
identities: {
'0x01': { address: '0x01', name: 'Account 2' },
'0x02': { address: '0x02', name: 'Account 3' },
'0x00': { address: '0x00', name: 'Account 1' },
},
selectedAddress: '0x02',
},
},
});
controller.updateIdentities(['0x00', '0x01']);
expect(controller.state.selectedAddress).toBe('0x00');
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syncIdentities has been made private, but we still need to cover the logic in it. Perhaps we should substitute some of these test cases that has been removed, triggering what we have to test using the onKeyringStateChangeMock listener

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the behaviour intended by onKeyringStateChangeMock to sync identities is already covered in theses tests https://github.com/MetaMask/core/blob/main/packages/preferences-controller/src/PreferencesController.test.ts#L42

Comment thread packages/preferences-controller/src/PreferencesController.ts Outdated
@cryptodev-2s cryptodev-2s requested a review from mcmire February 27, 2024 13:28
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

For the changelog, I think we can describe both as removed. It's not relevant to users of the package whether syncIdentities still exists as a private method or not; private methods are private.

@cryptodev-2s cryptodev-2s merged commit 969dfd5 into main Feb 27, 2024
@cryptodev-2s cryptodev-2s deleted the refacto/remove-preferences-controller-unused-methods branch February 27, 2024 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[preferences-controller] Remove unused PreferencesController methods

4 participants