Skip to content

Commit

Permalink
update set method to return boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
rekmarks committed Aug 30, 2019
1 parent dceadcd commit 9420e7b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 42 deletions.
13 changes: 11 additions & 2 deletions src/third-party/EnsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,34 @@ export class EnsController extends BaseController<BaseConfig, EnsState> {
* @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;
}
}

Expand Down
102 changes: 62 additions & 40 deletions tests/EnsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand All @@ -53,70 +70,88 @@ 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');
}).toThrowError();
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
}
}
}
Expand All @@ -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();
});
});

0 comments on commit 9420e7b

Please sign in to comment.