Skip to content

Commit

Permalink
test: Add test fixture for initial background state (#6798)
Browse files Browse the repository at this point in the history
* Add test fixture for initial background state

A test fixture has been added for the initial background state. This
fixture is kept up-to-date with a unit test. This will be used in
future PRs to simplify unit test setup.

* Add one example of this background state being used
  • Loading branch information
Gudahtt authored Jul 18, 2023
1 parent 303afd3 commit 3841fc2
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 5 deletions.
7 changes: 2 additions & 5 deletions app/components/UI/MessageSign/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import NotificationManager from '../../../core/NotificationManager';
import { InteractionManager } from 'react-native';
import AppConstants from '../../../core/AppConstants';
import { strings } from '../../../../locales/i18n';
import initialBackgroundState from '../../../util/test/initial-background-state.json';

jest.mock('../../../core/Engine', () => ({
context: {
Expand All @@ -36,11 +37,7 @@ const mockStore = configureMockStore();

const initialState = {
engine: {
backgroundState: {
PreferencesController: {
selectedAddress: '0x0',
},
},
backgroundState: initialBackgroundState,
},
};

Expand Down
15 changes: 15 additions & 0 deletions app/core/Engine.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Engine from './Engine';
import initialState from '../util/test/initial-background-state.json';

jest.unmock('./Engine');

Expand All @@ -22,15 +23,29 @@ describe('Engine', () => {
expect(engine.context).toHaveProperty('TokenRatesController');
expect(engine.context).toHaveProperty('TokensController');
});

it('calling Engine.init twice returns the same instance', () => {
const engine = Engine.init({});
const newEngine = Engine.init({});
expect(engine).toStrictEqual(newEngine);
});

it('calling Engine.destroy deletes the old instance', async () => {
const engine = Engine.init({});
await engine.destroyEngineInstance();
const newEngine = Engine.init({});
expect(engine).not.toStrictEqual(newEngine);
});

// Use this to keep the unit test initial background state fixture up-to-date
it('matches initial state fixture', () => {
const engine = Engine.init({});
const backgroundState = engine.datamodel.state;
// Replace phishing controller fallback config, as it bloats the test fixture too much
backgroundState.PhishingController.listState.allowlist = [];
backgroundState.PhishingController.listState.blocklist = [];
backgroundState.PhishingController.listState.fuzzylist = [];

expect(engine.datamodel.state).toStrictEqual(initialState);
});
});
165 changes: 165 additions & 0 deletions app/util/test/initial-background-state.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
{
"KeyringController": {
"keyrings": []
},
"AccountTrackerController": {
"accounts": {}
},
"AddressBookController": {
"addressBook": {}
},
"AssetsContractController": {},
"NftController": {
"allNftContracts": {},
"allNfts": {},
"ignoredNfts": []
},
"TokensController": {
"tokens": [],
"ignoredTokens": [],
"detectedTokens": [],
"allTokens": {},
"allIgnoredTokens": {},
"allDetectedTokens": {}
},
"TokenListController": {
"tokenList": {},
"tokensChainsCache": {},
"preventPollingOnNetworkRestart": false
},
"TokenDetectionController": {},
"NftDetectionController": {},
"CurrencyRateController": {
"conversionDate": 0,
"conversionRate": 0,
"currentCurrency": "usd",
"nativeCurrency": "ETH",
"pendingCurrentCurrency": null,
"pendingNativeCurrency": null,
"usdConversionRate": null
},
"NetworkController": {
"network": "loading",
"isCustomNetwork": false,
"providerConfig": {
"type": "mainnet",
"chainId": "1"
},
"networkDetails": {
"isEIP1559Compatible": false
}
},
"PhishingController": {
"listState": {
"allowlist": [],
"blocklist": [],
"fuzzylist": [],
"tolerance": 2,
"version": 2,
"name": "MetaMask",
"lastUpdated": 0
},
"whitelist": [],
"hotlistLastFetched": 0,
"stalelistLastFetched": 0
},
"PreferencesController": {
"featureFlags": {},
"frequentRpcList": [],
"identities": {},
"ipfsGateway": "https://cloudflare-ipfs.com/ipfs/",
"lostIdentities": {},
"selectedAddress": "",
"useTokenDetection": true,
"useNftDetection": false,
"openSeaEnabled": false,
"isMultiAccountBalancesEnabled": true,
"disabledRpcMethodPreferences": {
"eth_sign": false
},
"showTestNetworks": false
},
"TokenBalancesController": {
"contractBalances": {}
},
"TokenRatesController": {
"contractExchangeRates": {}
},
"TransactionController": {
"methodData": {},
"transactions": []
},
"SwapsController": {
"quotes": {},
"quoteValues": {},
"fetchParams": {
"slippage": 0,
"sourceToken": "",
"sourceAmount": 0,
"destinationToken": "",
"walletAddress": ""
},
"fetchParamsMetaData": {
"sourceTokenInfo": {
"decimals": 0,
"address": "",
"symbol": ""
},
"destinationTokenInfo": {
"decimals": 0,
"address": "",
"symbol": ""
}
},
"topAggSavings": null,
"aggregatorMetadata": null,
"tokens": null,
"topAssets": null,
"approvalTransaction": null,
"aggregatorMetadataLastFetched": 0,
"quotesLastFetched": 0,
"topAssetsLastFetched": 0,
"error": {
"key": null,
"description": null
},
"topAggId": null,
"tokensLastFetched": 0,
"isInPolling": false,
"pollingCyclesLeft": 3,
"quoteRefreshSeconds": null,
"usedGasEstimate": null,
"usedCustomGas": null,
"chainCache": {
"1": {
"aggregatorMetadata": null,
"tokens": null,
"topAssets": null,
"aggregatorMetadataLastFetched": 0,
"topAssetsLastFetched": 0,
"tokensLastFetched": 0
}
}
},
"GasFeeController": {
"gasFeeEstimates": {},
"estimatedGasFeeTimeBounds": {},
"gasEstimateType": "none"
},
"ApprovalController": {
"pendingApprovals": {},
"pendingApprovalCount": 0,
"approvalFlows": []
},
"PermissionController": {
"subjects": {}
},
"SignatureController": {
"unapprovedMsgs": {},
"unapprovedPersonalMsgs": {},
"unapprovedTypedMessages": {},
"unapprovedMsgCount": 0,
"unapprovedPersonalMsgCount": 0,
"unapprovedTypedMessagesCount": 0
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
"resolveJsonModule": true /* Allows importing JSON files */,
"baseUrl": "." /* Base directory to resolve non-absolute module names. */,
"paths": {
"images/*": ["./app/images/*"]
Expand Down

0 comments on commit 3841fc2

Please sign in to comment.