Skip to content

Commit

Permalink
test: Add test case for detectTokens where token detection is disab…
Browse files Browse the repository at this point in the history
…led on mainnet
  • Loading branch information
MajorLift committed Feb 6, 2024
1 parent 0e3d51f commit 4594d8e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/assets-controllers/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = merge(baseConfig, {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 88.63,
branches: 88.93,
functions: 96.71,
lines: 97.34,
statements: 97.4,
Expand Down
70 changes: 69 additions & 1 deletion packages/assets-controllers/src/TokenDetectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type {
TokenDetectionControllerMessenger,
} from './TokenDetectionController';
import {
STATIC_MAINNET_TOKEN_LIST,
TokenDetectionController,
controllerName,
} from './TokenDetectionController';
Expand All @@ -40,7 +41,7 @@ import {
type TokenListState,
type TokenListToken,
} from './TokenListController';
import type { TokensState } from './TokensController';
import type { TokensController, TokensState } from './TokensController';
import { getDefaultTokensState } from './TokensController';

const DEFAULT_INTERVAL = 180000;
Expand Down Expand Up @@ -1734,6 +1735,64 @@ describe('TokenDetectionController', () => {
},
);
});

it('should detect and add tokens from the `@metamask/contract-metadata` legacy token list if token detection is disabled and current network is mainnet', async () => {
const mockGetBalancesInSingleCall = jest.fn().mockResolvedValue(
Object.keys(STATIC_MAINNET_TOKEN_LIST).reduce<Record<string, BN>>(
(acc, address) => {
acc[address] = new BN(1);
return acc;
},
{},
),
);
const selectedAddress = '0x0000000000000000000000000000000000000001';
await withController(
{
options: {
disabled: false,
getBalancesInSingleCall: mockGetBalancesInSingleCall,
networkClientId: NetworkType.mainnet,
selectedAddress,
},
},
async ({
controller,
triggerPreferencesStateChange,
callActionSpy,
}) => {
triggerPreferencesStateChange({
...getDefaultPreferencesState(),
useTokenDetection: false,
});
await controller.detectTokens({
networkClientId: NetworkType.mainnet,
accountAddress: selectedAddress,
});
expect(callActionSpy).toHaveBeenLastCalledWith(
'TokensController:addDetectedTokens',
Object.values(STATIC_MAINNET_TOKEN_LIST).map((token) => {
const newToken = {
...token,
image: token.iconUrl,
isERC721: false,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (newToken as any).erc20;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (newToken as any).erc721;
delete newToken.iconUrl;
return newToken;
}),
{
selectedAddress,
chainId: ChainId.mainnet,
},
);
},
);
});

it('should detect and add tokens by networkClientId correctly', async () => {
const mockGetBalancesInSingleCall = jest.fn().mockResolvedValue({
[sampleTokenA.address]: new BN(1),
Expand Down Expand Up @@ -1890,6 +1949,15 @@ async function withController<ReturnValue>(
...getDefaultPreferencesState(),
}),
);
controllerMessenger.registerActionHandler(
'TokensController:addDetectedTokens',
jest
.fn<
ReturnType<TokensController['addDetectedTokens']>,
Parameters<TokensController['addDetectedTokens']>
>()
.mockResolvedValue(undefined),
);
const callActionSpy = jest.spyOn(controllerMessenger, 'call');

const controller = new TokenDetectionController({
Expand Down

0 comments on commit 4594d8e

Please sign in to comment.