Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmire committed Nov 8, 2024
1 parent ae321f1 commit eef95f6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 101 deletions.
154 changes: 58 additions & 96 deletions app/scripts/controllers/swaps/swaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { ChainId, InfuraNetworkType } from '@metamask/controller-utils';
import BigNumberjs from 'bignumber.js';
import { mapValues } from 'lodash';
import * as ethersProviders from '@ethersproject/providers';
import { Hex } from '@metamask/utils';
import { SafeEventEmitterProvider } from '@metamask/eth-json-rpc-provider';
import { NetworkClientId } from '@metamask/network-controller';
import { GasEstimateTypes } from '../../../../shared/constants/gas';
import { CHAIN_IDS } from '../../../../shared/constants/network';
import { ETH_SWAPS_TOKEN_OBJECT } from '../../../../shared/constants/swaps';
Expand Down Expand Up @@ -150,6 +153,28 @@ messengerMock.call.mockImplementation((actionName, ...args) => {
return undefined;
});

function mockNetworkControllerGetNetworkClientById(
networkClientsById: Record<
NetworkClientId,
{
provider: SafeEventEmitterProvider;
configuration: {
chainId: Hex;
};
}
>,
) {
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(networkClientId) => {
const foundNetworkClient = networkClientsById[networkClientId];
if (foundNetworkClient === undefined) {
throw new Error(`Unknown network client ID '${networkClientId}'`);
}
return foundNetworkClient;
},
);
}

describe('SwapsController', function () {
const getSwapsController = () => {
return new SwapsController(
Expand Down Expand Up @@ -321,18 +346,11 @@ describe('SwapsController', function () {
it('returns an empty object if passed an empty set of quotes', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

expect(
await swapsController.getTopQuoteWithCalculatedSavings({
Expand All @@ -345,18 +363,11 @@ describe('SwapsController', function () {
it('returns the top aggId and quotes with savings and fee values if passed necessary data and an even number of quotes', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

const topQuoteAndSavings =
await swapsController.getTopQuoteWithCalculatedSavings({
Expand Down Expand Up @@ -430,18 +441,11 @@ describe('SwapsController', function () {
it('returns the top aggId, without best quote flagged, and quotes with fee values if passed necessary data but no custom convert rate exists', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

const testInput = mapValues(
getTopQuoteAndSavingsMockQuotes(),
Expand Down Expand Up @@ -491,18 +495,11 @@ describe('SwapsController', function () {
it('returns the top aggId and quotes with savings and fee values if passed necessary data and the source token is ETH', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

const testInput = mapValues(
getTopQuoteAndSavingsMockQuotes(),
Expand Down Expand Up @@ -576,18 +573,11 @@ describe('SwapsController', function () {
it('returns the top aggId and quotes with savings and fee values if passed necessary data and the source token is ETH and an ETH fee is included in the trade value of what would be the best quote', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

const testInput = mapValues(
getTopQuoteAndSavingsMockQuotes(),
Expand Down Expand Up @@ -677,18 +667,11 @@ describe('SwapsController', function () {
it('returns the top aggId and quotes with savings and fee values if passed necessary data and the source token is not ETH and an ETH fee is included in the trade value of what would be the best quote', async function () {
const chainId = CHAIN_IDS.MAINNET;
const networkClientId = InfuraNetworkType.mainnet;
const provider = createTestProviderTools({ chainId });
const { provider } = createTestProviderTools({ chainId });
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

const testInput = getTopQuoteAndSavingsMockQuotes();
// 0.04 ETH fee included in trade value
Expand Down Expand Up @@ -756,16 +739,9 @@ describe('SwapsController', function () {
chainId: chainId as ChainId,
});
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

swapsController = getSwapsController();

Expand Down Expand Up @@ -849,16 +825,9 @@ describe('SwapsController', function () {
chainId: chainId as ChainId,
});
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});

swapsController = getSwapsController();

Expand Down Expand Up @@ -935,16 +904,9 @@ describe('SwapsController', function () {
chainId: chainId as ChainId,
});
const networkClient = { provider, configuration: { chainId } };
networkControllerGetNetworkClientByIdCallbackMock.mockImplementation(
(givenNetworkClientId) => {
if (givenNetworkClientId === networkClientId) {
return networkClient;
}
throw new Error(
`Unknown network client ID '${givenNetworkClientId}'`,
);
},
);
mockNetworkControllerGetNetworkClientById({
[networkClientId]: networkClient,
});
const ethersProvider = new ethersProviders.Web3Provider(provider);
jest
.spyOn(ethersProviders, 'Web3Provider')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
"@metamask/eslint-config-nodejs": "^9.0.0",
"@metamask/eslint-config-typescript": "^9.0.1",
"@metamask/eslint-plugin-design-tokens": "^1.1.0",
"@metamask/eth-json-rpc-provider": "^4.1.6",
"@metamask/forwarder": "^1.1.0",
"@metamask/phishing-warning": "^4.1.0",
"@metamask/preferences-controller": "^13.0.2",
Expand Down
6 changes: 1 addition & 5 deletions test/stub/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
createScaffoldMiddleware,
} from '@metamask/json-rpc-engine';
import { providerAsMiddleware } from '@metamask/eth-json-rpc-middleware';
import { providerFromEngine } from '@metamask/eth-json-rpc-provider';
import Ganache from 'ganache';
import { CHAIN_IDS } from '../../shared/constants/network';

Expand Down Expand Up @@ -40,11 +41,6 @@ export function createEngineForTestData() {
return new JsonRpcEngine();
}

export function providerFromEngine(engine) {
const provider = { sendAsync: engine.handle.bind(engine) };
return provider;
}

export function createTestProviderTools(opts = {}) {
const engine = createEngineForTestData();
// handle provided hooks
Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5212,6 +5212,19 @@ __metadata:
languageName: node
linkType: hard

"@metamask/eth-json-rpc-provider@npm:^4.1.6":
version: 4.1.6
resolution: "@metamask/eth-json-rpc-provider@npm:4.1.6"
dependencies:
"@metamask/json-rpc-engine": "npm:^10.0.1"
"@metamask/rpc-errors": "npm:^7.0.1"
"@metamask/safe-event-emitter": "npm:^3.0.0"
"@metamask/utils": "npm:^10.0.0"
uuid: "npm:^8.3.2"
checksum: 10/aeec2c362a5386357e9f8c707da9baa4326e83889633723656b6801b6461ea8ab8f020b0d9ed0bbc2d8fd6add4af4c99cc9c9a1cbedca267a033a9f19da41200
languageName: node
linkType: hard

"@metamask/eth-ledger-bridge-keyring@npm:^3.0.1":
version: 3.0.1
resolution: "@metamask/eth-ledger-bridge-keyring@npm:3.0.1"
Expand Down Expand Up @@ -26535,6 +26548,7 @@ __metadata:
"@metamask/eslint-plugin-design-tokens": "npm:^1.1.0"
"@metamask/eth-json-rpc-filters": "npm:^9.0.0"
"@metamask/eth-json-rpc-middleware": "patch:@metamask/eth-json-rpc-middleware@npm%3A14.0.1#~/.yarn/patches/@metamask-eth-json-rpc-middleware-npm-14.0.1-b6c2ccbe8c.patch"
"@metamask/eth-json-rpc-provider": "npm:^4.1.6"
"@metamask/eth-ledger-bridge-keyring": "npm:^3.0.1"
"@metamask/eth-query": "npm:^4.0.0"
"@metamask/eth-sig-util": "npm:^7.0.1"
Expand Down

0 comments on commit eef95f6

Please sign in to comment.