diff --git a/package.json b/package.json index 76dfb15c1ba7..be9d43ea3f4a 100644 --- a/package.json +++ b/package.json @@ -461,6 +461,7 @@ "@babel/preset-react": "^7.22.15", "@babel/preset-typescript": "^7.23.2", "@babel/register": "^7.22.15", + "@jest/globals": "^29.7.0", "@lavamoat/allow-scripts": "^3.0.4", "@lavamoat/lavadome-core": "0.0.10", "@lavamoat/lavapack": "^6.1.0", diff --git a/shared/modules/selectors/index.test.ts b/shared/modules/selectors/index.test.ts index 850da3d522ca..9f0b1b201a5c 100644 --- a/shared/modules/selectors/index.test.ts +++ b/shared/modules/selectors/index.test.ts @@ -1,3 +1,6 @@ +// Mocha type definitions are conflicting with Jest +import { it as jestIt } from '@jest/globals'; + import { createSwapsMockStore } from '../../../test/jest'; import { CHAIN_IDS } from '../../constants/network'; import { mockNetworkState } from '../../../test/stub/networks'; @@ -77,13 +80,13 @@ describe('Selectors', () => { }; }; describe('getSmartTransactionsOptInStatusForMetrics', () => { - it('should return the smart transactions opt-in status', () => { + jestIt('should return the smart transactions opt-in status', () => { const state = createMockState(); const result = getSmartTransactionsOptInStatusForMetrics(state); expect(result).toBe(true); }); - it.each([ + jestIt.each([ { status: true, expected: true }, { status: false, expected: false }, { status: null, expected: null }, @@ -98,13 +101,16 @@ describe('Selectors', () => { }); describe('getSmartTransactionsPreferenceEnabled', () => { - it('should return the smart transactions preference enabled status', () => { - const state = createMockState(); - const result = getSmartTransactionsPreferenceEnabled(state); - expect(result).toBe(true); - }); + jestIt( + 'should return the smart transactions preference enabled status', + () => { + const state = createMockState(); + const result = getSmartTransactionsPreferenceEnabled(state); + expect(result).toBe(true); + }, + ); - it.each([ + jestIt.each([ { status: true, expected: true }, { status: false, expected: false }, { status: null, expected: true }, @@ -120,106 +126,133 @@ describe('Selectors', () => { }); describe('getCurrentChainSupportsSmartTransactions', () => { - it('should return true if the chain ID is allowed for smart transactions', () => { - const state = createMockState(); - const result = getCurrentChainSupportsSmartTransactions(state); - expect(result).toBe(true); - }); + jestIt( + 'should return true if the chain ID is allowed for smart transactions', + () => { + const state = createMockState(); + const result = getCurrentChainSupportsSmartTransactions(state); + expect(result).toBe(true); + }, + ); - it('should return false if the chain ID is not allowed for smart transactions', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), - }, - }; - const result = getCurrentChainSupportsSmartTransactions(newState); - expect(result).toBe(false); - }); + jestIt( + 'should return false if the chain ID is not allowed for smart transactions', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), + }, + }; + const result = getCurrentChainSupportsSmartTransactions(newState); + expect(result).toBe(false); + }, + ); }); describe('getSmartTransactionsEnabled', () => { - it('returns true if feature flag is enabled, not a HW and is Ethereum network', () => { - const state = createSwapsMockStore(); - expect(getSmartTransactionsEnabled(state)).toBe(true); - }); + jestIt( + 'returns true if feature flag is enabled, not a HW and is Ethereum network', + () => { + const state = createSwapsMockStore(); + expect(getSmartTransactionsEnabled(state)).toBe(true); + }, + ); - it('returns false if feature flag is disabled, not a HW and is Ethereum network', () => { - const state = createSwapsMockStore(); - state.metamask.swapsState.swapsFeatureFlags.smartTransactions.extensionActive = - false; - expect(getSmartTransactionsEnabled(state)).toBe(false); - }); + jestIt( + 'returns false if feature flag is disabled, not a HW and is Ethereum network', + () => { + const state = createSwapsMockStore(); + state.metamask.swapsState.swapsFeatureFlags.smartTransactions.extensionActive = + false; + expect(getSmartTransactionsEnabled(state)).toBe(false); + }, + ); - it('returns false if feature flag is enabled, not a HW, STX liveness is false and is Ethereum network', () => { - const state = createSwapsMockStore(); - state.metamask.smartTransactionsState.liveness = false; - expect(getSmartTransactionsEnabled(state)).toBe(false); - }); + jestIt( + 'returns false if feature flag is enabled, not a HW, STX liveness is false and is Ethereum network', + () => { + const state = createSwapsMockStore(); + state.metamask.smartTransactionsState.liveness = false; + expect(getSmartTransactionsEnabled(state)).toBe(false); + }, + ); - it('returns true if feature flag is enabled, is a HW and is Ethereum network', () => { - const state = createSwapsMockStore(); - const newState = { - ...state, - metamask: { - ...state.metamask, - internalAccounts: { - ...state.metamask.internalAccounts, - selectedAccount: 'account2', - accounts: { - account2: { - metadata: { - keyring: { - type: 'Trezor Hardware', + jestIt( + 'returns true if feature flag is enabled, is a HW and is Ethereum network', + () => { + const state = createSwapsMockStore(); + const newState = { + ...state, + metamask: { + ...state.metamask, + internalAccounts: { + ...state.metamask.internalAccounts, + selectedAccount: 'account2', + accounts: { + account2: { + metadata: { + keyring: { + type: 'Trezor Hardware', + }, }, }, }, }, }, - }, - }; - expect(getSmartTransactionsEnabled(newState)).toBe(true); - }); + }; + expect(getSmartTransactionsEnabled(newState)).toBe(true); + }, + ); - it('returns false if feature flag is enabled, not a HW and is Polygon network', () => { - const state = createSwapsMockStore(); - const newState = { - ...state, - metamask: { - ...state.metamask, - ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), - }, - }; - expect(getSmartTransactionsEnabled(newState)).toBe(false); - }); + jestIt( + 'returns false if feature flag is enabled, not a HW and is Polygon network', + () => { + const state = createSwapsMockStore(); + const newState = { + ...state, + metamask: { + ...state.metamask, + ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), + }, + }; + expect(getSmartTransactionsEnabled(newState)).toBe(false); + }, + ); - it('returns false if feature flag is enabled, not a HW and is BSC network', () => { - const state = createSwapsMockStore(); - const newState = { - ...state, - metamask: { - ...state.metamask, - ...mockNetworkState({ chainId: CHAIN_IDS.BSC }), - }, - }; - expect(getSmartTransactionsEnabled(newState)).toBe(false); - }); + jestIt( + 'returns false if feature flag is enabled, not a HW and is BSC network', + () => { + const state = createSwapsMockStore(); + const newState = { + ...state, + metamask: { + ...state.metamask, + ...mockNetworkState({ chainId: CHAIN_IDS.BSC }), + }, + }; + expect(getSmartTransactionsEnabled(newState)).toBe(false); + }, + ); - it('returns false if feature flag is enabled, not a HW and is Linea network', () => { - const state = createSwapsMockStore(); - const newState = { - ...state, - metamask: { - ...state.metamask, - ...mockNetworkState({ chainId: CHAIN_IDS.LINEA_MAINNET }), - }, - }; - expect(getSmartTransactionsEnabled(newState)).toBe(false); - }); + jestIt( + 'returns false if feature flag is enabled, not a HW and is Linea network', + () => { + const state = createSwapsMockStore(); + const newState = { + ...state, + metamask: { + ...state.metamask, + ...mockNetworkState({ chainId: CHAIN_IDS.LINEA_MAINNET }), + }, + }; + expect(getSmartTransactionsEnabled(newState)).toBe(false); + }, + ); - it('returns false if a snap account is used', () => { + jestIt('returns false if a snap account is used', () => { const state = createSwapsMockStore(); state.metamask.internalAccounts.selectedAccount = '36eb02e0-7925-47f0-859f-076608f09b69'; @@ -228,13 +261,16 @@ describe('Selectors', () => { }); describe('getIsSmartTransaction', () => { - it('should return true if smart transactions are opt-in and enabled', () => { - const state = createMockState(); - const result = getIsSmartTransaction(state); - expect(result).toBe(true); - }); + jestIt( + 'should return true if smart transactions are opt-in and enabled', + () => { + const state = createMockState(); + const result = getIsSmartTransaction(state); + expect(result).toBe(true); + }, + ); - it('should return false if smart transactions are not opt-in', () => { + jestIt('should return false if smart transactions are not opt-in', () => { const state = createMockState(); const newState = { ...state, @@ -250,7 +286,7 @@ describe('Selectors', () => { expect(result).toBe(false); }); - it('should return false if smart transactions are not enabled', () => { + jestIt('should return false if smart transactions are not enabled', () => { const state = createMockState(); const newState = { ...state, @@ -282,103 +318,121 @@ describe('Selectors', () => { }); describe('getIsSmartTransactionsOptInModalAvailable', () => { - it('returns true for Ethereum Mainnet + supported RPC URL + null opt-in status and non-zero balance', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - preferences: { - ...state.metamask.preferences, - smartTransactionsOptInStatus: null, + jestIt( + 'returns true for Ethereum Mainnet + supported RPC URL + null opt-in status and non-zero balance', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + preferences: { + ...state.metamask.preferences, + smartTransactionsOptInStatus: null, + }, }, - }, - }; - expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(true); - }); + }; + expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(true); + }, + ); - it('returns false for Polygon Mainnet + supported RPC URL + null opt-in status and non-zero balance', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - preferences: { - ...state.metamask.preferences, - smartTransactionsOptInStatus: null, + jestIt( + 'returns false for Polygon Mainnet + supported RPC URL + null opt-in status and non-zero balance', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + preferences: { + ...state.metamask.preferences, + smartTransactionsOptInStatus: null, + }, + ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), }, - ...mockNetworkState({ chainId: CHAIN_IDS.POLYGON }), - }, - }; - expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); - }); + }; + expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); + }, + ); - it('returns false for Ethereum Mainnet + unsupported RPC URL + null opt-in status and non-zero balance', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - preferences: { - ...state.metamask.preferences, - smartTransactionsOptInStatus: null, + jestIt( + 'returns false for Ethereum Mainnet + unsupported RPC URL + null opt-in status and non-zero balance', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + preferences: { + ...state.metamask.preferences, + smartTransactionsOptInStatus: null, + }, + ...mockNetworkState({ + chainId: CHAIN_IDS.MAINNET, + rpcUrl: 'https://mainnet.quiknode.pro/', + }), }, - ...mockNetworkState({ - chainId: CHAIN_IDS.MAINNET, - rpcUrl: 'https://mainnet.quiknode.pro/', - }), - }, - }; - expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); - }); + }; + expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); + }, + ); - it('returns false for Ethereum Mainnet + supported RPC URL + true opt-in status and non-zero balance', () => { - const state = createMockState(); - expect(getIsSmartTransactionsOptInModalAvailable(state)).toBe(false); - }); + jestIt( + 'returns false for Ethereum Mainnet + supported RPC URL + true opt-in status and non-zero balance', + () => { + const state = createMockState(); + expect(getIsSmartTransactionsOptInModalAvailable(state)).toBe(false); + }, + ); - it('returns false for Ethereum Mainnet + supported RPC URL + null opt-in status and zero balance (0x0)', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - preferences: { - ...state.metamask.preferences, - smartTransactionsOptInStatus: null, - }, - accounts: { - ...state.metamask.accounts, - '0x123': { - address: '0x123', - balance: '0x0', + jestIt( + 'returns false for Ethereum Mainnet + supported RPC URL + null opt-in status and zero balance (0x0)', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + preferences: { + ...state.metamask.preferences, + smartTransactionsOptInStatus: null, + }, + accounts: { + ...state.metamask.accounts, + '0x123': { + address: '0x123', + balance: '0x0', + }, }, }, - }, - }; - expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); - }); + }; + expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); + }, + ); - it('returns false for Ethereum Mainnet + supported RPC URL + null opt-in status and zero balance (0x00)', () => { - const state = createMockState(); - const newState = { - ...state, - metamask: { - ...state.metamask, - preferences: { - ...state.metamask.preferences, - smartTransactionsOptInStatus: null, - }, - accounts: { - ...state.metamask.accounts, - '0x123': { - address: '0x123', - balance: '0x00', + jestIt( + 'returns false for Ethereum Mainnet + supported RPC URL + null opt-in status and zero balance (0x00)', + () => { + const state = createMockState(); + const newState = { + ...state, + metamask: { + ...state.metamask, + preferences: { + ...state.metamask.preferences, + smartTransactionsOptInStatus: null, + }, + accounts: { + ...state.metamask.accounts, + '0x123': { + address: '0x123', + balance: '0x00', + }, }, }, - }, - }; - expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); - }); + }; + expect(getIsSmartTransactionsOptInModalAvailable(newState)).toBe(false); + }, + ); }); }); diff --git a/yarn.lock b/yarn.lock index 94ecd006df3e..9a1f66991269 100644 --- a/yarn.lock +++ b/yarn.lock @@ -26100,6 +26100,7 @@ __metadata: "@ethersproject/providers": "npm:^5.7.2" "@ethersproject/wallet": "npm:^5.7.0" "@fortawesome/fontawesome-free": "npm:^5.13.0" + "@jest/globals": "npm:^29.7.0" "@keystonehq/bc-ur-registry-eth": "npm:^0.19.1" "@keystonehq/metamask-airgapped-keyring": "npm:^0.13.1" "@lavamoat/allow-scripts": "npm:^3.0.4"