Skip to content

Commit

Permalink
Add jest test
Browse files Browse the repository at this point in the history
  • Loading branch information
darkwing committed Mar 19, 2024
1 parent 288c5b5 commit a01f3dc
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions ui/selectors/selectors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import {
import { SURVEY_DATE, SURVEY_GMT } from '../helpers/constants/survey';
import * as selectors from './selectors';

jest.mock('../../app/scripts/lib/util', () => ({
...jest.requireActual('../../app/scripts/lib/util'),
getEnvironmentType: jest.fn().mockReturnValue('popup'),
}));

jest.mock('../../shared/modules/network.utils', () => {
const actual = jest.requireActual('../../shared/modules/network.utils');
return {
Expand Down Expand Up @@ -204,6 +209,59 @@ describe('Selectors', () => {
});
});

describe('#getAutomaticSwitchNetwork', () => {
const SELECTED_ORIGIN = 'https://portfolio.metamask.io';
const SELECTED_ORIGIN_NETWORK_ID = 'linea-goerli';
const state = {
activeTab: {
origin: SELECTED_ORIGIN,
},
metamask: {
isUnlocked: true,
useRequestQueue: true,
selectedTabOrigin: SELECTED_ORIGIN,
unapprovedMsgs: [],
unapprovedDecryptMsgs: [],
unapprovedPersonalMsgs: [],
unapprovedEncryptionPublicKeyMsgs: [],
unapprovedTypedMessages: [],
domains: {
[SELECTED_ORIGIN]: SELECTED_ORIGIN_NETWORK_ID,
},
providerConfig: {
...mockState.metamask.networkConfigurations
.testNetworkConfigurationId,
chainId: '0x1',
type: 'rpc',
id: 'mainnet',
},
},
};

it('should return the network to switch to', () => {
process.env.MULTICHAIN = 1;
const networkToSwitchTo = selectors.getAutomaticSwitchNetwork(state);
expect(networkToSwitchTo).toBe(SELECTED_ORIGIN_NETWORK_ID);
delete process.env.MULTICHAIN;
});

it('should return no network to switch to because we are already on it', () => {
process.env.MULTICHAIN = 1;
const networkToSwitchTo = selectors.getAutomaticSwitchNetwork({
...state,
metamask: {
...state.metamask,
providerConfig: {
...state.metamask.providerConfig,
id: 'linea-goerli',
},
},
});
expect(networkToSwitchTo).toBe(null);
delete process.env.MULTICHAIN;
});
});

describe('#getSuggestedTokens', () => {
it('returns an empty array if pendingApprovals is undefined', () => {
expect(selectors.getSuggestedTokens({ metamask: {} })).toStrictEqual([]);
Expand Down

0 comments on commit a01f3dc

Please sign in to comment.