Skip to content

Commit

Permalink
Fix misaligned mock eth query types in controller-utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Nov 14, 2023
1 parent bf898f0 commit c9c9507
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EthQuery, { type Provider } from '@metamask/eth-query';
import { BN } from 'ethereumjs-util';
import nock from 'nock';

Expand Down Expand Up @@ -436,57 +437,61 @@ describe('util', () => {
describe('query', () => {
describe('when the given method exists directly on the EthQuery', () => {
it('should call the method on the EthQuery and, if it is successful, return a promise that resolves to the result', async () => {
class EthQuery {
class MockEthQuery extends EthQuery {
getBlockByHash(blockId: any, cb: any) {
cb(null, { id: blockId });
}
}
// @ts-expect-error Mock eth query does not fulfill type requirements
const result = await util.query(new EthQuery(), 'getBlockByHash', [
'0x1234',
]);
const result = await util.query(
new MockEthQuery({} as Provider),
'getBlockByHash',
['0x1234'],
);
expect(result).toStrictEqual({ id: '0x1234' });
});

it('should call the method on the EthQuery and, if it errors, return a promise that is rejected with the error', async () => {
class EthQuery {
class MockEthQuery extends EthQuery {
getBlockByHash(_blockId: any, cb: any) {
cb(new Error('uh oh'), null);
}
}
await expect(
// @ts-expect-error Mock eth query does not fulfill type requirements
util.query(new EthQuery(), 'getBlockByHash', ['0x1234']),
util.query(new MockEthQuery({} as Provider), 'getBlockByHash', [
'0x1234',
]),
).rejects.toThrow('uh oh');
});
});

describe('when the given method does not exist directly on the EthQuery', () => {
it('should use sendAsync to call the RPC endpoint and, if it is successful, return a promise that resolves to the result', async () => {
class EthQuery {
class MockEthQuery extends EthQuery {
sendAsync({ method, params }: any, cb: any) {
if (method === 'eth_getBlockByHash') {
return cb(null, { id: params[0] });
}
throw new Error(`Unsupported method ${method}`);
}
}
// @ts-expect-error Mock eth query does not fulfill type requirements
const result = await util.query(new EthQuery(), 'eth_getBlockByHash', [
'0x1234',
]);
const result = await util.query(
new MockEthQuery({} as Provider),
'eth_getBlockByHash',
['0x1234'],
);
expect(result).toStrictEqual({ id: '0x1234' });
});

it('should use sendAsync to call the RPC endpoint and, if it errors, return a promise that is rejected with the error', async () => {
class EthQuery {
class MockEthQuery extends EthQuery {
sendAsync(_args: any, cb: any) {
cb(new Error('uh oh'), null);
}
}
await expect(
// @ts-expect-error Mock eth query does not fulfill type requirements
util.query(new EthQuery(), 'eth_getBlockByHash', ['0x1234']),
util.query(new MockEthQuery({} as Provider), 'eth_getBlockByHash', [
'0x1234',
]),
).rejects.toThrow('uh oh');
});
});
Expand Down

0 comments on commit c9c9507

Please sign in to comment.