|
| 1 | +import { renderHook } from '@testing-library/react-hooks'; |
| 2 | +import * as reactRedux from 'react-redux'; |
| 3 | +import { CHAIN_IDS } from '@metamask/transaction-controller'; |
| 4 | +import { useRefreshSmartTransactionsLiveness } from './useRefreshSmartTransactionsLiveness'; |
| 5 | + |
| 6 | +const mockInnerFn = jest.fn(); |
| 7 | + |
| 8 | +jest.mock('react-redux', () => ({ |
| 9 | + useSelector: jest.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('../../../store/actions', () => ({ |
| 13 | + fetchSmartTransactionsLiveness: jest.fn(() => mockInnerFn), |
| 14 | +})); |
| 15 | + |
| 16 | +const mockFetchSmartTransactionsLiveness = jest.requireMock( |
| 17 | + '../../../store/actions', |
| 18 | +).fetchSmartTransactionsLiveness; |
| 19 | + |
| 20 | +describe('useRefreshSmartTransactionsLiveness', () => { |
| 21 | + beforeEach(() => { |
| 22 | + jest.clearAllMocks(); |
| 23 | + (reactRedux.useSelector as jest.Mock).mockReturnValue(true); // opted in by default |
| 24 | + }); |
| 25 | + |
| 26 | + it('does not fetch when chainId is null', () => { |
| 27 | + renderHook(() => useRefreshSmartTransactionsLiveness(null)); |
| 28 | + expect(mockFetchSmartTransactionsLiveness).not.toHaveBeenCalled(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not fetch when chainId is undefined', () => { |
| 32 | + renderHook(() => useRefreshSmartTransactionsLiveness(undefined)); |
| 33 | + expect(mockFetchSmartTransactionsLiveness).not.toHaveBeenCalled(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('does not fetch for non-EVM chains', () => { |
| 37 | + renderHook(() => useRefreshSmartTransactionsLiveness('solana:mainnet')); |
| 38 | + expect(mockFetchSmartTransactionsLiveness).not.toHaveBeenCalled(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('does not fetch for unsupported EVM chains', () => { |
| 42 | + renderHook(() => useRefreshSmartTransactionsLiveness('0x999')); |
| 43 | + expect(mockFetchSmartTransactionsLiveness).not.toHaveBeenCalled(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not fetch when user has not opted in', () => { |
| 47 | + (reactRedux.useSelector as jest.Mock).mockReturnValue(false); |
| 48 | + renderHook(() => useRefreshSmartTransactionsLiveness(CHAIN_IDS.MAINNET)); |
| 49 | + expect(mockFetchSmartTransactionsLiveness).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('fetches smart transactions liveness for mainnet', () => { |
| 53 | + renderHook(() => useRefreshSmartTransactionsLiveness(CHAIN_IDS.MAINNET)); |
| 54 | + expect(mockFetchSmartTransactionsLiveness).toHaveBeenCalledTimes(1); |
| 55 | + expect(mockFetchSmartTransactionsLiveness).toHaveBeenCalledWith({ |
| 56 | + chainId: CHAIN_IDS.MAINNET, |
| 57 | + }); |
| 58 | + expect(mockInnerFn).toHaveBeenCalledTimes(1); |
| 59 | + }); |
| 60 | + |
| 61 | + it('re-fetches when chainId changes to another supported chain', () => { |
| 62 | + const { rerender } = renderHook<{ chainId: string }, void>( |
| 63 | + ({ chainId }) => useRefreshSmartTransactionsLiveness(chainId), |
| 64 | + { initialProps: { chainId: CHAIN_IDS.MAINNET } }, |
| 65 | + ); |
| 66 | + |
| 67 | + expect(mockFetchSmartTransactionsLiveness).toHaveBeenCalledTimes(1); |
| 68 | + expect(mockInnerFn).toHaveBeenCalledTimes(1); |
| 69 | + |
| 70 | + // BSC is in both production and development allowed lists |
| 71 | + rerender({ chainId: CHAIN_IDS.BSC }); |
| 72 | + expect(mockFetchSmartTransactionsLiveness).toHaveBeenCalledTimes(2); |
| 73 | + expect(mockInnerFn).toHaveBeenCalledTimes(2); |
| 74 | + }); |
| 75 | +}); |
0 commit comments