From ba07fd8c3aba2093ae00e5be01af07405ad17306 Mon Sep 17 00:00:00 2001 From: legobeat <109787230+legobeat@users.noreply.github.com> Date: Thu, 6 Jun 2024 00:24:35 +0900 Subject: [PATCH] chore(test): Refactor FakeBlockTracker provider injection (#4345) --- .../src/NftDetectionController.test.ts | 5 +++-- .../tests/NetworkController.test.ts | 2 +- packages/network-controller/tests/helpers.ts | 5 +++-- packages/transaction-controller/package.json | 1 + .../src/TransactionController.test.ts | 21 ++++++++++++------- tests/fake-block-tracker.ts | 7 +++---- yarn.lock | 1 + 7 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/assets-controllers/src/NftDetectionController.test.ts b/packages/assets-controllers/src/NftDetectionController.test.ts index 8984134a0e..79e08cee93 100644 --- a/packages/assets-controllers/src/NftDetectionController.test.ts +++ b/packages/assets-controllers/src/NftDetectionController.test.ts @@ -862,6 +862,7 @@ 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, @@ -869,8 +870,8 @@ describe('NftDetectionController', () => { ticker: 'TEST', type: NetworkClientType.Custom, }, - provider: new FakeProvider(), - blockTracker: new FakeBlockTracker(), + provider, + blockTracker: new FakeBlockTracker({ provider }), destroy: () => { // do nothing }, diff --git a/packages/network-controller/tests/NetworkController.test.ts b/packages/network-controller/tests/NetworkController.test.ts index 18e3476d7b..3d7d8639d0 100644 --- a/packages/network-controller/tests/NetworkController.test.ts +++ b/packages/network-controller/tests/NetworkController.test.ts @@ -7574,7 +7574,7 @@ function buildFakeClient( rpcUrl: 'https://test.network', }, provider, - blockTracker: new FakeBlockTracker(), + blockTracker: new FakeBlockTracker({ provider }), destroy: () => { // do nothing }, diff --git a/packages/network-controller/tests/helpers.ts b/packages/network-controller/tests/helpers.ts index 6e63262d4f..c7072665ea 100644 --- a/packages/network-controller/tests/helpers.ts +++ b/packages/network-controller/tests/helpers.ts @@ -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 }, diff --git a/packages/transaction-controller/package.json b/packages/transaction-controller/package.json index 3bc8e97121..583cfdd020 100644 --- a/packages/transaction-controller/package.json +++ b/packages/transaction-controller/package.json @@ -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", diff --git a/packages/transaction-controller/src/TransactionController.test.ts b/packages/transaction-controller/src/TransactionController.test.ts index 5cfbdb33fb..6809b930d6 100644 --- a/packages/transaction-controller/src/TransactionController.test.ts +++ b/packages/transaction-controller/src/TransactionController.test.ts @@ -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 { @@ -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; } @@ -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: { @@ -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: { @@ -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: { @@ -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: { @@ -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: { @@ -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: { diff --git a/tests/fake-block-tracker.ts b/tests/fake-block-tracker.ts index 0c7365b441..55439211f1 100644 --- a/tests/fake-block-tracker.ts +++ b/tests/fake-block-tracker.ts @@ -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 @@ -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 diff --git a/yarn.lock b/yarn.lock index 52d067820f..e8e7eb6b6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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