From 9420e7b7d49fb0d0c7f742767f25c62e34097d5c Mon Sep 17 00:00:00 2001 From: Erik Marks Date: Fri, 30 Aug 2019 10:16:13 -0700 Subject: [PATCH] update set method to return boolean --- src/third-party/EnsController.ts | 13 +++- tests/EnsController.test.ts | 102 +++++++++++++++++++------------ 2 files changed, 73 insertions(+), 42 deletions(-) diff --git a/src/third-party/EnsController.ts b/src/third-party/EnsController.ts index ff444dc99e4..f7d8fa14227 100644 --- a/src/third-party/EnsController.ts +++ b/src/third-party/EnsController.ts @@ -97,25 +97,34 @@ export class EnsController extends BaseController { * @param chainId - Id of the associated chain * @param ensName - The ENS name * @param address - Associated address to add or update + * @returns - Boolean indicating whether the entry was set */ - set(chainId: number, ensName: string, address: string) { + set(chainId: number, ensName: string, address: string): boolean { if (!Number.isInteger(chainId) || !ensName || typeof ensName !== 'string' || !isValidAddress(address)) { throw new Error(`Invalid ENS entry: { chainId:${chainId}, ensName:${ensName}, address:${address}}`); } + const normalizedAddress = toChecksumAddress(address); + const subState = this.state.ensEntries[chainId]; + + if (subState && subState[ensName] && subState[ensName].address === normalizedAddress) { + return false; + } + this.update({ ensEntries: { ...this.state.ensEntries, [chainId]: { ...this.state.ensEntries[chainId], [ensName]: { - address: toChecksumAddress(address), + address: normalizedAddress, chainId, ensName } } } }); + return true; } } diff --git a/tests/EnsController.test.ts b/tests/EnsController.test.ts index a0925e83dbd..f8f54cab6c9 100644 --- a/tests/EnsController.test.ts +++ b/tests/EnsController.test.ts @@ -18,33 +18,50 @@ describe('EnsController', () => { expect(controller.state).toEqual({ ensEntries: {} }); }); - it('should add a new ENS entry', () => { + it('should add a new ENS entry and return true', () => { const controller = new EnsController(); - controller.set(1, name1, address1); + expect(controller.set(1, name1, address1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address1Checksum, chainId: 1, - name: name1 + ensName: name1 } } } }); }); - it('should update an ENS entry', () => { + it('should update an ENS entry and return true', () => { const controller = new EnsController(); - controller.set(1, name1, address1); - controller.set(1, name1, address2); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set(1, name1, address2)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address2Checksum, chainId: 1, - name: name1 + ensName: name1 + } + } + } + }); + }); + + it('should not update an ENS entry if the address is the same and return false', () => { + const controller = new EnsController(); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set(1, name1, address1)).toBeFalsy(); + expect(controller.state).toEqual({ + ensEntries: { + 1: { + [name1]: { + address: address1Checksum, + chainId: 1, + ensName: name1 } } } @@ -53,36 +70,36 @@ describe('EnsController', () => { it('should add multiple ENS entries and update without side effects', () => { const controller = new EnsController(); - controller.set(1, name1, address1); - controller.set(1, name2, address2); - controller.set(2, name1, address1); - controller.set(1, name1, address3); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set(1, name2, address2)).toBeTruthy(); + expect(controller.set(2, name1, address1)).toBeTruthy(); + expect(controller.set(1, name1, address3)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name1]: { address: address3Checksum, chainId: 1, - name: name1 + ensName: name1 }, [name2]: { address: address2Checksum, chainId: 1, - name: name2 + ensName: name2 } }, 2: { [name1]: { address: address1Checksum, chainId: 2, - name: name1 + ensName: name1 } } } }); }); - it('should throw on attempt to add invalid ENS entry', () => { + it('should throw on attempt to set invalid ENS entry', () => { const controller = new EnsController(); expect(() => { controller.set(1, '1337', 'foo'); @@ -90,33 +107,51 @@ describe('EnsController', () => { expect(controller.state).toEqual({ ensEntries: {} }); }); - it('should remove a ENS entry', () => { + it('should remove an ENS entry and return true', () => { const controller = new EnsController(); - controller.set(1, name1, address1); - controller.delete(1, name1); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.delete(1, name1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: {} }); }); - it('should add multiple ENS entries and remove without side effects', () => { + it('should return false if an ENS entry was NOT deleted', () => { const controller = new EnsController(); controller.set(1, name1, address1); - controller.set(1, name2, address2); - controller.set(2, name1, address1); - controller.delete(1, name1); + expect(controller.delete(1, 'bar')).toBeFalsy(); + expect(controller.delete(2, 'bar')).toBeFalsy(); + expect(controller.state).toEqual({ + ensEntries: { + 1: { + [name1]: { + address: address1Checksum, + chainId: 1, + ensName: name1 + } + } + } + }); + }); + + it('should add multiple ENS entries and remove without side effects', () => { + const controller = new EnsController(); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set(1, name2, address2)).toBeTruthy(); + expect(controller.set(2, name1, address1)).toBeTruthy(); + expect(controller.delete(1, name1)).toBeTruthy(); expect(controller.state).toEqual({ ensEntries: { 1: { [name2]: { address: address2Checksum, chainId: 1, - name: name2 + ensName: name2 } }, 2: { [name1]: { address: address1Checksum, chainId: 2, - name: name1 + ensName: name1 } } } @@ -125,23 +160,10 @@ describe('EnsController', () => { it('should clear all ENS entries', () => { const controller = new EnsController(); - controller.set(1, name1, address1); - controller.set(1, name2, address2); - controller.set(2, name1, address1); + expect(controller.set(1, name1, address1)).toBeTruthy(); + expect(controller.set(1, name2, address2)).toBeTruthy(); + expect(controller.set(2, name1, address1)).toBeTruthy(); controller.clear(); expect(controller.state).toEqual({ ensEntries: {} }); }); - - it('should return true to indicate an ENS entry has been deleted', () => { - const controller = new EnsController(); - controller.set(1, name1, address1); - expect(controller.delete(1, name1)).toBeTruthy(); - }); - - it('should return false to indicate an ENS entry has NOT been deleted', () => { - const controller = new EnsController(); - controller.set(1, name1, address1); - expect(controller.delete(1, 'bar')).toBeFalsy(); - expect(controller.delete(2, 'bar')).toBeFalsy(); - }); });