Skip to content

Commit

Permalink
Fix tests to initialize mock eth query using FakeProvider type
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorLift committed Nov 15, 2023
1 parent bb7f348 commit 699e88b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
34 changes: 6 additions & 28 deletions packages/controller-utils/src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import EthQuery, { type Provider } from '@metamask/eth-query';
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 @@ -437,27 +438,17 @@ 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 MockEthQuery extends EthQuery {
getBlockByHash(blockId: any, cb: any) {
cb(null, { id: blockId });
}
}
const result = await util.query(
new MockEthQuery({} as Provider),
new EthQuery(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 MockEthQuery extends EthQuery {
getBlockByHash(_blockId: any, cb: any) {
cb(new Error('uh oh'), null);
}
}
await expect(
util.query(new MockEthQuery({} as Provider), 'getBlockByHash', [
util.query(new EthQuery(new FakeProvider()), 'getBlockByHash', [
'0x1234',
]),
).rejects.toThrow('uh oh');
Expand All @@ -466,30 +457,17 @@ describe('util', () => {

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 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}`);
}
}
const result = await util.query(
new MockEthQuery({} as Provider),
new EthQuery(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 MockEthQuery extends EthQuery {
sendAsync(_args: any, cb: any) {
cb(new Error('uh oh'), null);
}
}
await expect(
util.query(new MockEthQuery({} as Provider), 'eth_getBlockByHash', [
util.query(new EthQuery(new FakeProvider()), 'eth_getBlockByHash', [
'0x1234',
]),
).rejects.toThrow('uh oh');
Expand Down
5 changes: 3 additions & 2 deletions packages/gas-fee-controller/src/fetchBlockFeeHistory.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { query, fromHex, toHex } from '@metamask/controller-utils';
import EthQuery, { type Provider } from '@metamask/eth-query';
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 @@ -34,7 +35,7 @@ function times<T>(n: number, fn: (n: number) => T): T[] {
}

describe('fetchBlockFeeHistory', () => {
const mockEthQuery = new EthQuery({} as Provider);
const mockEthQuery = new EthQuery(new FakeProvider());
describe('with a minimal set of arguments', () => {
const latestBlockNumber = 3;
const numberOfRequestedBlocks = 3;
Expand Down
27 changes: 14 additions & 13 deletions packages/transaction-controller/src/utils/transaction-type.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EthQuery, { type Provider } from '@metamask/eth-query';
import EthQuery from '@metamask/eth-query';

import { FakeProvider } from '../../../../tests/fake-provider';
import { TransactionType } from '../types';
import { determineTransactionType } from './transaction-type';

Expand All @@ -23,7 +24,7 @@ describe('determineTransactionType', () => {
data: '0xa9059cbb0000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C970000000000000000000000000000000000000000000000000000000000000000a',
from: FROM_MOCK,
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);

expect(result).toMatchObject({
Expand All @@ -44,7 +45,7 @@ describe('determineTransactionType', () => {
const resultWithEmptyValue = await determineTransactionType(
txParams,

new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(resultWithEmptyValue).toMatchObject({
type: TransactionType.tokenMethodTransfer,
Expand All @@ -57,7 +58,7 @@ describe('determineTransactionType', () => {
...txParams,
},

new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);

expect(resultWithEmptyValue2).toMatchObject({
Expand All @@ -71,7 +72,7 @@ describe('determineTransactionType', () => {
...txParams,
},

new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(resultWithValue).toMatchObject({
type: TransactionType.contractInteraction,
Expand All @@ -88,7 +89,7 @@ describe('determineTransactionType', () => {
}
const result = await determineTransactionType(
txParams,
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.simpleSend,
Expand All @@ -107,7 +108,7 @@ describe('determineTransactionType', () => {
...txParams,
data: '0x095ea7b30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.tokenMethodApprove,
Expand All @@ -127,7 +128,7 @@ describe('determineTransactionType', () => {
to: '',
data: '0xabd',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.deployContract,
Expand All @@ -146,7 +147,7 @@ describe('determineTransactionType', () => {
...txParams,
data: '0xabd',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.simpleSend,
Expand All @@ -165,7 +166,7 @@ describe('determineTransactionType', () => {
...txParams,
data: '0xabd',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.simpleSend,
Expand All @@ -184,7 +185,7 @@ describe('determineTransactionType', () => {
...txParams,
data: 'abd',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.contractInteraction,
Expand All @@ -203,7 +204,7 @@ describe('determineTransactionType', () => {
...txParams,
data: '',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.contractInteraction,
Expand All @@ -224,7 +225,7 @@ describe('determineTransactionType', () => {
value: '0x5af3107a4000',
data: '0x095ea7b30000000000000000000000002f318C334780961FB129D2a6c30D0763d9a5C9700000000000000000000000000000000000000000000000000000000000000005',
},
new MockEthQuery({} as Provider),
new MockEthQuery(new FakeProvider()),
);
expect(result).toMatchObject({
type: TransactionType.contractInteraction,
Expand Down

0 comments on commit 699e88b

Please sign in to comment.