Skip to content

Commit 63797f3

Browse files
jpuriOGPoyraz
authored andcommitted
test: add unit tests for selectStablecoins selector
1 parent e79595e commit 63797f3

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { CHAIN_IDS } from '@metamask/transaction-controller';
2+
import { Hex } from '@metamask/utils';
3+
import { selectStablecoins, STABLE_TOKENS_FLAG } from '.';
4+
import mockedEngine from '../../../core/__mocks__/MockedEngine';
5+
import { mockedEmptyFlagsState, mockedUndefinedFlagsState } from '../mocks';
6+
7+
jest.mock('../../../core/Engine', () => ({
8+
init: () => mockedEngine.init(),
9+
}));
10+
11+
const getMockedFeatureFlag = (value: unknown) => ({
12+
engine: {
13+
backgroundState: {
14+
RemoteFeatureFlagController: {
15+
remoteFeatureFlags: {
16+
[STABLE_TOKENS_FLAG]: value,
17+
},
18+
cacheTimestamp: 0,
19+
},
20+
},
21+
},
22+
});
23+
24+
describe('selectStablecoins', () => {
25+
it('returns default stablecoins when flag state is undefined', () => {
26+
const result = selectStablecoins(mockedUndefinedFlagsState);
27+
expect(result[CHAIN_IDS.MAINNET as Hex]).toContain(
28+
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
29+
);
30+
});
31+
32+
it('returns default stablecoins when no flags are set', () => {
33+
const result = selectStablecoins(mockedEmptyFlagsState);
34+
expect(result).toHaveProperty(CHAIN_IDS.MAINNET);
35+
expect(result).toHaveProperty(CHAIN_IDS.ARBITRUM);
36+
expect(result).toHaveProperty(CHAIN_IDS.LINEA_MAINNET);
37+
expect(result).toHaveProperty(CHAIN_IDS.POLYGON);
38+
});
39+
40+
it('returns flag value when set to a valid object', () => {
41+
const flagValue = {
42+
'0x1': ['0xaaa', '0xbbb'],
43+
'0xa4b1': ['0xccc'],
44+
};
45+
const result = selectStablecoins(getMockedFeatureFlag(flagValue));
46+
expect(result).toStrictEqual(flagValue);
47+
});
48+
49+
it('normalizes addresses and chain IDs to lowercase', () => {
50+
const result = selectStablecoins(
51+
getMockedFeatureFlag({
52+
'0xA4B1': ['0xAf88d065e77c8cC2239327C5EDb3A432268e5831'],
53+
}),
54+
);
55+
expect(result).toStrictEqual({
56+
'0xa4b1': ['0xaf88d065e77c8cc2239327c5edb3a432268e5831'],
57+
});
58+
});
59+
60+
it('skips non-array entries in flag value', () => {
61+
const result = selectStablecoins(
62+
getMockedFeatureFlag({
63+
'0x1': ['0xaaa'],
64+
'0xa4b1': 'not-an-array',
65+
}),
66+
);
67+
expect(result).toStrictEqual({ '0x1': ['0xaaa'] });
68+
});
69+
70+
it('returns default stablecoins when flag is an array', () => {
71+
const result = selectStablecoins(
72+
getMockedFeatureFlag(['not', 'an', 'object']),
73+
);
74+
expect(result).toHaveProperty(CHAIN_IDS.MAINNET);
75+
});
76+
77+
it('returns default stablecoins when flag is a primitive', () => {
78+
const result = selectStablecoins(getMockedFeatureFlag(true));
79+
expect(result).toHaveProperty(CHAIN_IDS.MAINNET);
80+
});
81+
});

0 commit comments

Comments
 (0)