Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misaligned mock eth query types in controller tests #2037

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import EthQuery from '@metamask/eth-query';
import { BN } from 'ethereumjs-util';
import nock from 'nock';

import { FakeProvider } from '../../../tests/fake-provider';
import { MAX_SAFE_CHAIN_ID } from './constants';
import * as util from './util';

Expand Down Expand Up @@ -436,57 +438,63 @@ 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(new FakeProvider()),
'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(new FakeProvider()), '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(new FakeProvider()),
'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(new FakeProvider()),
'eth_getBlockByHash',
['0x1234'],
),
).rejects.toThrow('uh oh');
});
});
Expand Down
59 changes: 24 additions & 35 deletions packages/gas-fee-controller/src/fetchBlockFeeHistory.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { query, fromHex, toHex } from '@metamask/controller-utils';
import EthQuery from '@metamask/eth-query';
import { BN } from 'ethereumjs-util';
import { when } from 'jest-when';

import { FakeProvider } from '../../../tests/fake-provider';
import fetchBlockFeeHistory from './fetchBlockFeeHistory';

jest.mock('@metamask/controller-utils', () => {
Expand Down Expand Up @@ -33,23 +35,20 @@ function times<T>(n: number, fn: (n: number) => T): T[] {
}

describe('fetchBlockFeeHistory', () => {
const ethQuery = { eth: 'query' };

const mockEthQuery = new EthQuery(new FakeProvider());
describe('with a minimal set of arguments', () => {
const latestBlockNumber = 3;
const numberOfRequestedBlocks = 3;

beforeEach(() => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'blockNumber')
.calledWith(mockEthQuery, 'blockNumber')
.mockResolvedValue(new BN(latestBlockNumber));
});

it('should return a representation of fee history from the Ethereum network, organized by block rather than type of data', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[],
Expand All @@ -70,7 +69,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
});

Expand Down Expand Up @@ -98,8 +97,7 @@ describe('fetchBlockFeeHistory', () => {

it('should be able to handle an "empty" response from eth_feeHistory', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[],
Expand All @@ -111,7 +109,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
});

Expand All @@ -120,8 +118,7 @@ describe('fetchBlockFeeHistory', () => {

it('should be able to handle an response with undefined baseFeePerGas from eth_feeHistory', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[],
Expand All @@ -132,7 +129,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
});

Expand All @@ -158,8 +155,7 @@ describe('fetchBlockFeeHistory', () => {
});

when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'blockNumber')
.calledWith(mockEthQuery, 'blockNumber')
.mockResolvedValue(new BN(latestBlockNumber));

expectedChunks.forEach(({ startBlockNumber, endBlockNumber }) => {
Expand All @@ -171,8 +167,7 @@ describe('fetchBlockFeeHistory', () => {
.map((block) => block.gasUsedRatio);

when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(endBlockNumber - startBlockNumber + 1),
toHex(endBlockNumber),
[],
Expand All @@ -185,7 +180,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
});

Expand All @@ -208,8 +203,7 @@ describe('fetchBlockFeeHistory', () => {
const numberOfRequestedBlocks = 3;
const endBlock = new BN(latestBlockNumber);
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(endBlock),
[],
Expand All @@ -221,7 +215,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
endBlock,
});
Expand All @@ -236,15 +230,13 @@ describe('fetchBlockFeeHistory', () => {

beforeEach(() => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'blockNumber')
.calledWith(mockEthQuery, 'blockNumber')
.mockResolvedValue(new BN(latestBlockNumber));
});

it('should match each item in the "reward" key from the response to its percentile', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[10, 20, 30],
Expand Down Expand Up @@ -278,7 +270,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
percentiles: [10, 20, 30],
});
Expand Down Expand Up @@ -319,8 +311,7 @@ describe('fetchBlockFeeHistory', () => {

it('should be able to handle an "empty" response from eth_feeHistory including an empty "reward" array', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[10, 20, 30],
Expand All @@ -333,7 +324,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
percentiles: [10, 20, 30],
});
Expand All @@ -348,8 +339,7 @@ describe('fetchBlockFeeHistory', () => {

it('includes an extra block with an estimated baseFeePerGas', async () => {
when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(numberOfRequestedBlocks),
toHex(latestBlockNumber),
[],
Expand All @@ -370,7 +360,7 @@ describe('fetchBlockFeeHistory', () => {
});

const feeHistory = await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
includeNextBlock: true,
});
Expand Down Expand Up @@ -415,8 +405,7 @@ describe('fetchBlockFeeHistory', () => {
const endBlock = new BN(latestBlockNumber);

when(mockedQuery)
// @ts-expect-error Mock eth query does not fulfill type requirements
.calledWith(ethQuery, 'eth_feeHistory', [
.calledWith(mockEthQuery, 'eth_feeHistory', [
toHex(latestBlockNumber),
toHex(latestBlockNumber),
[],
Expand All @@ -429,7 +418,7 @@ describe('fetchBlockFeeHistory', () => {
});

await fetchBlockFeeHistory({
ethQuery,
ethQuery: mockEthQuery,
numberOfBlocks: numberOfRequestedBlocks,
endBlock,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import EthQuery from '@metamask/eth-query';
import { BN } from 'ethereumjs-util';
import { when } from 'jest-when';

Expand Down Expand Up @@ -31,7 +32,8 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => {
number: new BN(1),
baseFeePerGas: new BN(100_000_000_000),
};
const ethQuery = {
const mockEthQuery = {
sendAsync: EthQuery.prototype.sendAsync,
blockNumber: async () => latestBlock.number,
getBlockByNumber: async () => latestBlock,
};
mcmire marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -73,7 +75,7 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => {
mockedFetchLatestBlock.mockResolvedValue(latestBlock);
when(mockedFetchBlockFeeHistory)
.calledWith({
ethQuery,
ethQuery: mockEthQuery,
endBlock: latestBlock.number,
numberOfBlocks: 5,
percentiles: [10, 20, 30],
Expand All @@ -84,8 +86,9 @@ describe('fetchGasEstimatesViaEthFeeHistory', () => {
.calledWith(blocks)
.mockReturnValue(levelSpecificEstimates);

// @ts-expect-error Mock eth query does not fulfill type requirements
const gasFeeEstimates = await fetchGasEstimatesViaEthFeeHistory(ethQuery);
const gasFeeEstimates = await fetchGasEstimatesViaEthFeeHistory(
mockEthQuery,
);

expect(gasFeeEstimates).toStrictEqual({
...levelSpecificEstimates,
Expand Down
Loading
Loading