Skip to content

Commit

Permalink
chore(test): Refactor FakeBlockTracker provider injection (#4345)
Browse files Browse the repository at this point in the history
  • Loading branch information
legobeat authored Jun 5, 2024
1 parent d96e045 commit ba07fd8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -862,15 +862,16 @@ describe('NftDetectionController', () => {

it('should return true if mainnet is detected', async () => {
const mockAddNft = jest.fn();
const provider = new FakeProvider();
const mockNetworkClient: NetworkClient = {
configuration: {
chainId: ChainId.mainnet,
rpcUrl: 'https://test.network',
ticker: 'TEST',
type: NetworkClientType.Custom,
},
provider: new FakeProvider(),
blockTracker: new FakeBlockTracker(),
provider,
blockTracker: new FakeBlockTracker({ provider }),
destroy: () => {
// do nothing
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7574,7 +7574,7 @@ function buildFakeClient(
rpcUrl: 'https://test.network',
},
provider,
blockTracker: new FakeBlockTracker(),
blockTracker: new FakeBlockTracker({ provider }),
destroy: () => {
// do nothing
},
Expand Down
5 changes: 3 additions & 2 deletions packages/network-controller/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ function buildFakeNetworkClient({
configuration: NetworkClientConfiguration;
providerStubs?: FakeProviderStub[];
}): NetworkClient {
const provider = new FakeProvider({ stubs: providerStubs });
return {
configuration,
provider: new FakeProvider({ stubs: providerStubs }),
blockTracker: new FakeBlockTracker(),
provider,
blockTracker: new FakeBlockTracker({ provider }),
destroy: () => {
// do nothing
},
Expand Down
1 change: 1 addition & 0 deletions packages/transaction-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"devDependencies": {
"@babel/runtime": "^7.23.9",
"@metamask/auto-changelog": "^3.4.4",
"@metamask/eth-json-rpc-provider": "^4.0.0",
"@metamask/ethjs-provider-http": "^0.3.0",
"@types/bn.js": "^5.1.5",
"@types/jest": "^27.4.1",
Expand Down
21 changes: 13 additions & 8 deletions packages/transaction-controller/src/TransactionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
BUILT_IN_NETWORKS,
ORIGIN_METAMASK,
} from '@metamask/controller-utils';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import EthQuery from '@metamask/eth-query';
import HttpProvider from '@metamask/ethjs-provider-http';
import type {
Expand Down Expand Up @@ -246,10 +247,14 @@ function buildMockEthQuery(): EthQuery {
*
* @param latestBlockNumber - The block number that the block tracker should
* always return.
* @param provider - json rpc provider
* @returns The mocked block tracker.
*/
function buildMockBlockTracker(latestBlockNumber: string): BlockTracker {
const fakeBlockTracker = new FakeBlockTracker();
function buildMockBlockTracker(
latestBlockNumber: string,
provider: SafeEventEmitterProvider,
): BlockTracker {
const fakeBlockTracker = new FakeBlockTracker({ provider });
fakeBlockTracker.mockLatestBlockNumber(latestBlockNumber);
return fakeBlockTracker;
}
Expand Down Expand Up @@ -313,7 +318,7 @@ type MockNetwork = {

const MOCK_NETWORK: MockNetwork = {
provider: MAINNET_PROVIDER,
blockTracker: buildMockBlockTracker('0x102833C'),
blockTracker: buildMockBlockTracker('0x102833C', MAINNET_PROVIDER),
state: {
selectedNetworkClientId: NetworkType.goerli,
networksMetadata: {
Expand All @@ -333,7 +338,7 @@ const MOCK_NETWORK: MockNetwork = {
};
const MOCK_NETWORK_WITHOUT_CHAIN_ID: MockNetwork = {
provider: GOERLI_PROVIDER,
blockTracker: buildMockBlockTracker('0x102833C'),
blockTracker: buildMockBlockTracker('0x102833C', GOERLI_PROVIDER),
state: {
selectedNetworkClientId: NetworkType.goerli,
networksMetadata: {
Expand All @@ -351,7 +356,7 @@ const MOCK_NETWORK_WITHOUT_CHAIN_ID: MockNetwork = {
};
const MOCK_MAINNET_NETWORK: MockNetwork = {
provider: MAINNET_PROVIDER,
blockTracker: buildMockBlockTracker('0x102833C'),
blockTracker: buildMockBlockTracker('0x102833C', MAINNET_PROVIDER),
state: {
selectedNetworkClientId: NetworkType.mainnet,
networksMetadata: {
Expand All @@ -372,7 +377,7 @@ const MOCK_MAINNET_NETWORK: MockNetwork = {

const MOCK_LINEA_MAINNET_NETWORK: MockNetwork = {
provider: PALM_PROVIDER,
blockTracker: buildMockBlockTracker('0xA6EDFC'),
blockTracker: buildMockBlockTracker('0xA6EDFC', PALM_PROVIDER),
state: {
selectedNetworkClientId: NetworkType['linea-mainnet'],
networksMetadata: {
Expand All @@ -393,7 +398,7 @@ const MOCK_LINEA_MAINNET_NETWORK: MockNetwork = {

const MOCK_LINEA_GOERLI_NETWORK: MockNetwork = {
provider: PALM_PROVIDER,
blockTracker: buildMockBlockTracker('0xA6EDFC'),
blockTracker: buildMockBlockTracker('0xA6EDFC', PALM_PROVIDER),
state: {
selectedNetworkClientId: NetworkType['linea-goerli'],
networksMetadata: {
Expand All @@ -414,7 +419,7 @@ const MOCK_LINEA_GOERLI_NETWORK: MockNetwork = {

const MOCK_CUSTOM_NETWORK: MockNetwork = {
provider: PALM_PROVIDER,
blockTracker: buildMockBlockTracker('0xA6EDFC'),
blockTracker: buildMockBlockTracker('0xA6EDFC', PALM_PROVIDER),
state: {
selectedNetworkClientId: 'uuid-1',
networksMetadata: {
Expand Down
7 changes: 3 additions & 4 deletions tests/fake-block-tracker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PollingBlockTracker } from '@metamask/eth-block-tracker';
import { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import type { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';

/**
* Acts like a PollingBlockTracker, but doesn't start the polling loop or
Expand All @@ -9,9 +8,9 @@ import { JsonRpcEngine } from '@metamask/json-rpc-engine';
export class FakeBlockTracker extends PollingBlockTracker {
#latestBlockNumber = '0x0';

constructor() {
constructor({ provider }: { provider: SafeEventEmitterProvider }) {
super({
provider: new SafeEventEmitterProvider({ engine: new JsonRpcEngine() }),
provider,
});
// Don't start the polling loop
// TODO: Replace `any` with type
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3146,6 +3146,7 @@ __metadata:
"@metamask/auto-changelog": ^3.4.4
"@metamask/base-controller": ^6.0.0
"@metamask/controller-utils": ^11.0.0
"@metamask/eth-json-rpc-provider": ^4.0.0
"@metamask/eth-query": ^4.0.0
"@metamask/ethjs-provider-http": ^0.3.0
"@metamask/gas-fee-controller": ^17.0.0
Expand Down

0 comments on commit ba07fd8

Please sign in to comment.