|
| 1 | +import { act } from 'react-dom/test-utils'; |
| 2 | +import { useDispatch } from 'react-redux'; |
| 3 | +import { CHAIN_IDS, TransactionMeta } from '@metamask/transaction-controller'; |
| 4 | +import { Hex } from '@metamask/utils'; |
| 5 | +import { |
| 6 | + fetchSmartTransactionsLiveness, |
| 7 | + setSwapsFeatureFlags, |
| 8 | +} from '../../../store/actions'; |
| 9 | +import { renderHookWithConfirmContextProvider } from '../../../../test/lib/confirmations/render-helpers'; |
| 10 | +import { genUnapprovedContractInteractionConfirmation } from '../../../../test/data/confirmations/contract-interaction'; |
| 11 | +import { getMockConfirmStateForTransaction } from '../../../../test/data/confirmations/helper'; |
| 12 | +import { mockNetworkState } from '../../../../test/stub/networks'; |
| 13 | +import { fetchSwapsFeatureFlags } from '../../swaps/swaps.util'; |
| 14 | +import { useSmartTransactionFeatureFlags } from './useSmartTransactionFeatureFlags'; |
| 15 | + |
| 16 | +jest.mock('react-redux', () => ({ |
| 17 | + ...jest.requireActual('react-redux'), |
| 18 | + useDispatch: jest.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +jest.mock('../../../store/actions', () => ({ |
| 22 | + ...jest.requireActual('../../../store/actions'), |
| 23 | + setSwapsFeatureFlags: jest.fn(), |
| 24 | + fetchSmartTransactionsLiveness: jest.fn(), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('../../swaps/swaps.util', () => ({ |
| 28 | + ...jest.requireActual('../../swaps/swaps.util'), |
| 29 | + fetchSwapsFeatureFlags: jest.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +async function runHook({ |
| 33 | + smartTransactionsOptInStatus, |
| 34 | + chainId, |
| 35 | + confirmation, |
| 36 | +}: { |
| 37 | + smartTransactionsOptInStatus: boolean; |
| 38 | + chainId: Hex; |
| 39 | + confirmation?: Partial<TransactionMeta>; |
| 40 | +}) { |
| 41 | + const transaction = |
| 42 | + (confirmation as TransactionMeta) ?? |
| 43 | + genUnapprovedContractInteractionConfirmation({ |
| 44 | + chainId, |
| 45 | + }); |
| 46 | + |
| 47 | + const state = getMockConfirmStateForTransaction(transaction, { |
| 48 | + metamask: { |
| 49 | + ...mockNetworkState({ chainId, id: 'Test' }), |
| 50 | + selectedNetworkClientId: 'Test', |
| 51 | + preferences: { |
| 52 | + smartTransactionsOptInStatus, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }); |
| 56 | + |
| 57 | + renderHookWithConfirmContextProvider( |
| 58 | + () => useSmartTransactionFeatureFlags(), |
| 59 | + state, |
| 60 | + ); |
| 61 | + |
| 62 | + await act(async () => { |
| 63 | + // Intentionally empty |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +describe('useSmartTransactionFeatureFlags', () => { |
| 68 | + const setSwapsFeatureFlagsMock = jest.mocked(setSwapsFeatureFlags); |
| 69 | + const fetchSwapsFeatureFlagsMock = jest.mocked(fetchSwapsFeatureFlags); |
| 70 | + const fetchSmartTransactionsLivenessMock = jest.mocked( |
| 71 | + fetchSmartTransactionsLiveness, |
| 72 | + ); |
| 73 | + const useDispatchMock = jest.mocked(useDispatch); |
| 74 | + |
| 75 | + beforeEach(() => { |
| 76 | + jest.resetAllMocks(); |
| 77 | + useDispatchMock.mockReturnValue(jest.fn()); |
| 78 | + fetchSwapsFeatureFlagsMock.mockResolvedValue({}); |
| 79 | + fetchSmartTransactionsLivenessMock.mockReturnValue(() => Promise.resolve()); |
| 80 | + }); |
| 81 | + |
| 82 | + it('updates feature flags', async () => { |
| 83 | + await runHook({ |
| 84 | + smartTransactionsOptInStatus: true, |
| 85 | + chainId: CHAIN_IDS.MAINNET, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(setSwapsFeatureFlagsMock).toHaveBeenCalledTimes(1); |
| 89 | + expect(setSwapsFeatureFlagsMock).toHaveBeenCalledWith({}); |
| 90 | + }); |
| 91 | + |
| 92 | + it('does not update feature flags if smart transactions disabled', async () => { |
| 93 | + await runHook({ |
| 94 | + smartTransactionsOptInStatus: false, |
| 95 | + chainId: CHAIN_IDS.MAINNET, |
| 96 | + }); |
| 97 | + |
| 98 | + expect(setSwapsFeatureFlagsMock).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('does not update feature flags if chain not supported', async () => { |
| 102 | + await runHook({ |
| 103 | + smartTransactionsOptInStatus: true, |
| 104 | + chainId: CHAIN_IDS.ARBITRUM, |
| 105 | + }); |
| 106 | + |
| 107 | + expect(setSwapsFeatureFlagsMock).not.toHaveBeenCalled(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('does not update feature flags if confirmation is not transaction', async () => { |
| 111 | + await runHook({ |
| 112 | + smartTransactionsOptInStatus: true, |
| 113 | + chainId: CHAIN_IDS.MAINNET, |
| 114 | + confirmation: {}, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(setSwapsFeatureFlagsMock).not.toHaveBeenCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments