feat: remove updateIdentities and make syncIdentities private in preferences controller#3976
Conversation
…erences controller
| 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'); | ||
| }); | ||
|
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Gudahtt
left a comment
There was a problem hiding this comment.
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.
Explanation
After completion of #3794 and #3699, the method
syncIdentitiesis now only used internally onKeyringController:stateChangeevent andupdateIdentitiesis no longer being used.References
Changelog
@metamask@preferences-controllersyncIdentitiesis now private as it's only used internally to update state on KeyringController:stateChange event.updateIdentitieshas been removed, as it's not in use anymore.Checklist