diff --git a/app/components/UI/MessageSign/index.test.tsx b/app/components/UI/MessageSign/index.test.tsx index 64d2f5fa3ab..324b6f011a3 100644 --- a/app/components/UI/MessageSign/index.test.tsx +++ b/app/components/UI/MessageSign/index.test.tsx @@ -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: { @@ -36,11 +37,7 @@ const mockStore = configureMockStore(); const initialState = { engine: { - backgroundState: { - PreferencesController: { - selectedAddress: '0x0', - }, - }, + backgroundState: initialBackgroundState, }, }; diff --git a/app/core/Engine.test.js b/app/core/Engine.test.js index edb0d09da93..dcc33fbe995 100644 --- a/app/core/Engine.test.js +++ b/app/core/Engine.test.js @@ -1,4 +1,5 @@ import Engine from './Engine'; +import initialState from '../util/test/initial-background-state.json'; jest.unmock('./Engine'); @@ -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); + }); }); diff --git a/app/util/test/initial-background-state.json b/app/util/test/initial-background-state.json new file mode 100644 index 00000000000..19b68e56835 --- /dev/null +++ b/app/util/test/initial-background-state.json @@ -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 + } +} diff --git a/tsconfig.json b/tsconfig.json index 279de46702c..52c4852e866 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/*"]