Skip to content

Commit ff778ed

Browse files
committed
fix: Bump unit test coverage
1 parent 53b9df4 commit ff778ed

File tree

1 file changed

+168
-53
lines changed

1 file changed

+168
-53
lines changed

app/selectors/multichain/evm.test.tsx

+168-53
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
selectedAccountNativeTokenCachedBalanceByChainId,
66
selectAccountTokensAcrossChains,
77
selectNativeEvmAsset,
8+
selectStakedEvmAsset,
89
} from './evm';
910
import { SolScope } from '@metamask/keyring-api';
1011
import { GetByQuery } from '@testing-library/react-native/build/queries/makeQueries';
@@ -27,10 +28,7 @@ import {
2728
POLYGON_CHAIN_ID,
2829
} from '@metamask/swaps-controller/dist/constants';
2930
import { AccountsControllerState } from '@metamask/accounts-controller';
30-
import { renderFromWei, weiToFiat } from '../../util/number';
31-
import { hexToBN } from '@metamask/controller-utils';
3231
import { zeroAddress } from 'ethereumjs-util';
33-
import { PreferencesController } from '@metamask/preferences-controller';
3432

3533
describe('Multichain Selectors', () => {
3634
const mockState: RootState = {
@@ -42,11 +40,13 @@ describe('Multichain Selectors', () => {
4240
chainId: '0x1',
4341
name: 'Ethereum Mainnet',
4442
nativeCurrency: 'ETH',
43+
rpcEndpoints: [{ networkClientId: '0x1' }],
4544
},
4645
'0x89': {
4746
chainId: '0x89',
4847
name: 'Polygon',
49-
nativeCurrency: 'MATIC',
48+
nativeCurrency: 'POL',
49+
rpcEndpoints: [{ networkClientId: '0x89' }],
5050
},
5151
},
5252
},
@@ -98,9 +98,13 @@ describe('Multichain Selectors', () => {
9898
},
9999
CurrencyRateController: {
100100
currentCurrency: 'USD',
101-
conversionRates: {
102-
ETH: 2000,
103-
MATIC: 1,
101+
currencyRates: {
102+
ETH: {
103+
conversionRate: 2000,
104+
},
105+
POL: {
106+
conversionRate: 1,
107+
},
104108
},
105109
},
106110
AccountsController: {
@@ -202,17 +206,88 @@ describe('Multichain Selectors', () => {
202206

203207
it('should handle multiple chains correctly', () => {
204208
const result = selectAccountTokensAcrossChains(mockState);
209+
console.log('RESULT: ', result);
205210
expect(result).toHaveProperty('0x89');
206211
const polygonTokens = result['0x89'];
207212
expect(polygonTokens.length).toBeGreaterThan(0);
208-
expect(polygonTokens.some((token) => token.symbol === 'MATIC')).toBe(
209-
true,
210-
);
213+
expect(polygonTokens.some((token) => token.symbol === 'POL')).toBe(true);
211214
});
212215
});
213216

214217
describe('selectNativeEvmAsset', () => {
215-
it.only('should return undefined if accountBalanceByChainId is not provided', () => {
218+
const testState: RootState = {
219+
engine: {
220+
backgroundState: {
221+
...mockState.engine.backgroundState,
222+
AccountTrackerController: {
223+
accountsByChainId: {},
224+
},
225+
NetworkController: {
226+
...mockState.engine.backgroundState.NetworkController,
227+
selectedNetworkClientId: '0x1',
228+
},
229+
AccountsController: {
230+
internalAccounts: {
231+
selectedAccount: '',
232+
accounts: {},
233+
},
234+
},
235+
},
236+
},
237+
settings: {
238+
showFiatOnTestnets: true,
239+
},
240+
} as unknown as RootState;
241+
it('should return undefined if accountBalanceByChainId is not provided', () => {
242+
const result = selectNativeEvmAsset(testState);
243+
expect(result).toBeUndefined();
244+
});
245+
246+
it('should return the correct native EVM asset structure', () => {
247+
const result = selectNativeEvmAsset(mockState);
248+
249+
expect(result).toEqual({
250+
decimals: 18,
251+
name: 'Ethereum',
252+
symbol: 'ETH',
253+
isETH: true,
254+
balance: '< 0.00001',
255+
balanceFiat: '$0',
256+
logo: '../images/eth-logo-new.png',
257+
address: zeroAddress(),
258+
});
259+
});
260+
261+
it('should return asset name as ticker if not ETH', () => {
262+
const testStateOverride = {
263+
...testState,
264+
engine: {
265+
backgroundState: {
266+
...mockState.engine.backgroundState,
267+
NetworkController: {
268+
...mockState.engine.backgroundState.NetworkController,
269+
selectedNetworkClientId: '0x89',
270+
},
271+
},
272+
},
273+
} as unknown as RootState;
274+
const result = selectNativeEvmAsset(testStateOverride);
275+
276+
expect(result).toEqual({
277+
decimals: 18,
278+
name: 'POL',
279+
symbol: 'POL',
280+
isETH: true,
281+
balance: '< 0.00001',
282+
balanceFiat: '$0',
283+
logo: '../images/eth-logo-new.png',
284+
address: zeroAddress(),
285+
});
286+
});
287+
});
288+
289+
describe('selectStakedEvmAsset', () => {
290+
it('should return undefined if accountBalanceByChainId is not provided', () => {
216291
const testState: RootState = {
217292
engine: {
218293
backgroundState: {
@@ -232,51 +307,91 @@ describe('Multichain Selectors', () => {
232307
showFiatOnTestnets: true,
233308
},
234309
} as unknown as RootState;
235-
const result = selectNativeEvmAsset(testState);
310+
311+
const result = selectStakedEvmAsset(testState);
236312
expect(result).toBeUndefined();
237313
});
238314

239-
// it('should return the correct native EVM asset structure', () => {
240-
// const accountBalanceByChainId = { balance: '1000000000000000000' }; // 1 ETH in wei
241-
// const result = selectNativeEvmAsset.resultFunc(
242-
// accountBalanceByChainId,
243-
// 'ETH',
244-
// 3000,
245-
// 'USD',
246-
// );
247-
248-
// expect(result).toEqual({
249-
// decimals: 18,
250-
// name: 'Ethereum',
251-
// symbol: 'ETH',
252-
// isETH: true,
253-
// balance: '1000000000000000000 ETH', // Mocked `renderFromWei`
254-
// balanceFiat: '3000000000000000000000 USD', // Mocked `weiToFiat`
255-
// logo: '../images/eth-logo-new.png',
256-
// address: zeroAddress(),
257-
// });
258-
// });
259-
260-
// it('should return asset name as ticker if not ETH', () => {
261-
// const accountBalanceByChainId = { balance: '500000000000000000' }; // 0.5 XYZ
262-
// const result = selectNativeEvmAsset.resultFunc(
263-
// accountBalanceByChainId,
264-
// 'XYZ',
265-
// 2000,
266-
// 'EUR',
267-
// );
268-
269-
// expect(result).toEqual({
270-
// decimals: 18,
271-
// name: 'XYZ',
272-
// symbol: 'XYZ',
273-
// isETH: true,
274-
// balance: '500000000000000000 ETH',
275-
// balanceFiat: '1000000000000000000000 EUR',
276-
// logo: '../images/eth-logo-new.png',
277-
// address: zeroAddress(),
278-
// });
279-
// });
315+
it('should return undefined if stakedBalance is missing', () => {
316+
const testState: RootState = {
317+
...mockState,
318+
engine: {
319+
backgroundState: {
320+
...mockState.engine.backgroundState,
321+
AccountTrackerController: {
322+
accountsByChainId: {
323+
'0x1': {
324+
balance: '0x1', // 1 ETH
325+
},
326+
},
327+
},
328+
},
329+
},
330+
} as unknown as RootState;
331+
332+
const result = selectStakedEvmAsset(testState);
333+
expect(result).toBeUndefined();
334+
});
335+
336+
it('should return undefined if stakedBalance is zero', () => {
337+
const testState: RootState = {
338+
...mockState,
339+
engine: {
340+
backgroundState: {
341+
...mockState.engine.backgroundState,
342+
AccountTrackerController: {
343+
accountsByChainId: {
344+
'0x1': {
345+
balance: '0x1',
346+
stakedBalance: '0x2',
347+
},
348+
},
349+
},
350+
},
351+
},
352+
} as unknown as RootState;
353+
354+
const result = selectStakedEvmAsset(testState);
355+
expect(result).toBeUndefined();
356+
});
357+
358+
it('should return undefined if nativeAsset is missing', () => {
359+
const testState: RootState = {
360+
...mockState,
361+
engine: {
362+
backgroundState: {
363+
...mockState.engine.backgroundState,
364+
AccountTrackerController: {
365+
accountsByChainId: {
366+
'0x1': {
367+
balance: '0x1',
368+
stakedBalance: '0x2',
369+
},
370+
},
371+
},
372+
},
373+
},
374+
} as unknown as RootState;
375+
376+
const result = selectStakedEvmAsset(testState);
377+
expect(result).toBeUndefined();
378+
});
379+
380+
it('should return the correct staked EVM asset structure', () => {
381+
const result = selectStakedEvmAsset(mockState);
382+
383+
expect(result).toEqual({
384+
decimals: 18,
385+
name: 'Staked Ethereum',
386+
symbol: 'ETH',
387+
isETH: true,
388+
isStaked: true,
389+
balance: '< 0.00001',
390+
balanceFiat: '$0',
391+
logo: '../images/eth-logo-new.png',
392+
address: zeroAddress(),
393+
});
394+
});
280395
});
281396
});
282397

0 commit comments

Comments
 (0)