Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Luu committed Mar 22, 2024
1 parent 1b2a74f commit 2f71f02
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
6 changes: 5 additions & 1 deletion packages/web3-eth-ens/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,8 @@ Documentation:

- Added function `setAddress` in ENS and Resolver classes (#5956)

## [Unreleased]
## [Unreleased]

### Add

- Added function getText and getName in ENS and resolver classes
4 changes: 3 additions & 1 deletion packages/web3-eth-ens/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const interfaceIds: { [T: string]: string } = {
};

/**
* An object holding the functionsthat are supported by the ENS resolver contracts/interfaces.
* An object holding the functions that are supported by the ENS resolver contracts/interfaces.
*/
export const methodsInInterface: { [T: string]: string } = {
setAddr: 'addr',
Expand All @@ -38,6 +38,8 @@ export const methodsInInterface: { [T: string]: string } = {
pubkey: 'pubkey',
setContenthash: 'contenthash',
contenthash: 'contenthash',
text: 'text',
name: 'name',
};

/**
Expand Down
15 changes: 14 additions & 1 deletion packages/web3-eth-ens/src/ens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,22 @@ export class ENS extends Web3Context<EthExecutionAPI & Web3NetAPI> {

/**
* ERC-634 - Returns the text content stored in the resolver for the specified key.
* @param ENSName - The ENS name to resolve
* @param key - The key to resolve https://github.com/ethereum/ercs/blob/master/ERCS/erc-634.md#global-keys
* @returns - The value content stored in the resolver for the specified key
*/
public async getText(ENSName: string, key: string): Promise<string> {
return this._resolver.text(ENSName, key);
return this._resolver.getText(ENSName, key);

Check warning on line 175 in packages/web3-eth-ens/src/ens.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth-ens/src/ens.ts#L174-L175

Added lines #L174 - L175 were not covered by tests
}


/**
* Resolves an ENS name to an Ethereum address.
* @param address - The address to resolve
* @returns - The ENS name of the given address
*/
public async getName(address: string): Promise<string> {
return this._resolver.getName(address);

Check warning on line 185 in packages/web3-eth-ens/src/ens.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth-ens/src/ens.ts#L184-L185

Added lines #L184 - L185 were not covered by tests
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/web3-eth-ens/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ export class Resolver {
}

public async getName(
ENSName: string
address: string
) {
const resolverContract = await this.getResolverContractAdapter(ENSName);
const resolverContract = await this.getResolverContractAdapter(address);
await this.checkInterfaceSupport(resolverContract, methodsInInterface.name);

return resolverContract.methods
.name(namehash(ENSName)).call()
.name(namehash(address)).call()
}
}
54 changes: 54 additions & 0 deletions packages/web3-eth-ens/test/unit/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,60 @@ describe('resolver', () => {
});
});

describe('text', () => {
it('getText', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const textMock = jest.spyOn(contract.methods, 'text').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getText(ENS_NAME, "key");
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.text],
);
expect(textMock).toHaveBeenCalledWith(namehash(ENS_NAME), "key");
})
})

describe('name', () => {
it('getName', async () => {
const supportsInterfaceMock = jest
.spyOn(contract.methods, 'supportsInterface')
.mockReturnValue({
call: async () => Promise.resolve(true),
} as unknown as NonPayableMethodObject<any, any>);

const nameMock = jest.spyOn(contract.methods, 'name').mockReturnValue({
call: jest.fn(),
} as unknown as NonPayableMethodObject<any, any>);

jest.spyOn(registry, 'getResolver').mockImplementation(async () => {
return new Promise(resolve => {
resolve(contract);
});
});

await resolver.getName(ENS_NAME);
expect(supportsInterfaceMock).toHaveBeenCalledWith(
interfaceIds[methodsInInterface.name],
);

expect(nameMock).toHaveBeenCalledWith(namehash(ENS_NAME));
})
})


describe('supportsInterface', () => {
it('check supportsInterface for non strict hex id', async () => {
const interfaceId = 'setAddr';
Expand Down

1 comment on commit 2f71f02

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 2f71f02 Previous: 6c075db Ratio
processingTx 9263 ops/sec (±3.60%) 9301 ops/sec (±4.81%) 1.00
processingContractDeploy 36099 ops/sec (±7.54%) 39129 ops/sec (±7.62%) 1.08
processingContractMethodSend 18603 ops/sec (±6.50%) 19443 ops/sec (±5.19%) 1.05
processingContractMethodCall 37034 ops/sec (±5.52%) 38971 ops/sec (±6.34%) 1.05
abiEncode 42534 ops/sec (±6.98%) 44252 ops/sec (±6.92%) 1.04
abiDecode 29041 ops/sec (±6.86%) 30419 ops/sec (±8.89%) 1.05
sign 1576 ops/sec (±0.59%) 1656 ops/sec (±4.08%) 1.05
verify 368 ops/sec (±0.64%) 373 ops/sec (±0.78%) 1.01

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.