|
| 1 | +/** |
| 2 | + * usePerpsFeed — unit tests |
| 3 | + * |
| 4 | + * Focuses on the sorting/ordering logic that lives inside the useMemo: |
| 5 | + * 1. No-query path: items sorted by the variant's comparator. |
| 6 | + * 2. Query path (non-macro): Fuse.js relevance order is preserved. |
| 7 | + * 3. Query path (macro): sorted by volume even when a query is present. |
| 8 | + * 4. defaultSortOptionId matches PERPS_VARIANT_SORT_OPTION for each variant. |
| 9 | + */ |
| 10 | + |
| 11 | +import { renderHook } from '@testing-library/react-hooks'; |
| 12 | +import type { PerpsMarketData } from '@metamask/perps-controller'; |
| 13 | +import { usePerpsFeed, PERPS_VARIANT_SORT_OPTION } from './usePerpsFeed'; |
| 14 | + |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | +// Core dependency mocks |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | + |
| 19 | +jest.mock('react-redux', () => ({ |
| 20 | + useSelector: jest.fn(() => []), |
| 21 | +})); |
| 22 | + |
| 23 | +const mockMarkets: PerpsMarketData[] = []; |
| 24 | +const mockRefetch = jest.fn(); |
| 25 | + |
| 26 | +jest.mock('../../../../UI/Perps/hooks', () => ({ |
| 27 | + usePerpsMarkets: jest.fn(() => ({ |
| 28 | + markets: mockMarkets, |
| 29 | + isLoading: false, |
| 30 | + refresh: mockRefetch, |
| 31 | + isRefreshing: false, |
| 32 | + })), |
| 33 | +})); |
| 34 | + |
| 35 | +jest.mock('../../../../UI/Perps/providers/PerpsConnectionProvider', () => ({ |
| 36 | + PerpsConnectionContext: { _currentValue: null }, |
| 37 | +})); |
| 38 | + |
| 39 | +jest.mock('../../../../UI/Perps/selectors/perpsController', () => ({ |
| 40 | + selectPerpsWatchlistMarkets: jest.fn(), |
| 41 | +})); |
| 42 | + |
| 43 | +jest.mock( |
| 44 | + '../../../Homepage/Sections/Perpetuals/hooks/useHomepageSparklines', |
| 45 | + () => ({ |
| 46 | + useHomepageSparklines: jest.fn(() => ({ sparklines: {} })), |
| 47 | + }), |
| 48 | +); |
| 49 | + |
| 50 | +jest.mock('../../hooks/useFeedRefresh', () => ({ |
| 51 | + useFeedRefresh: jest.fn(), |
| 52 | +})); |
| 53 | + |
| 54 | +// --------------------------------------------------------------------------- |
| 55 | +// fuseSearch mock — controllable so we can verify order is preserved |
| 56 | +// --------------------------------------------------------------------------- |
| 57 | + |
| 58 | +const mockFuseSearch = jest.fn(); |
| 59 | +jest.mock('../search-utils', () => ({ |
| 60 | + fuseSearch: (...args: unknown[]) => mockFuseSearch(...args), |
| 61 | + PERPS_FUSE_OPTIONS: {}, |
| 62 | +})); |
| 63 | + |
| 64 | +jest.mock('@metamask/perps-controller', () => ({ |
| 65 | + filterMarketsByQuery: jest.fn((items: unknown[]) => items), |
| 66 | +})); |
| 67 | + |
| 68 | +// --------------------------------------------------------------------------- |
| 69 | +// Helpers |
| 70 | +// --------------------------------------------------------------------------- |
| 71 | + |
| 72 | +import { usePerpsMarkets } from '../../../../UI/Perps/hooks'; |
| 73 | + |
| 74 | +const makeMarket = ( |
| 75 | + symbol: string, |
| 76 | + change24hPercent: string, |
| 77 | + volumeNumber: number, |
| 78 | +): PerpsMarketData => |
| 79 | + ({ |
| 80 | + symbol, |
| 81 | + name: symbol, |
| 82 | + change24hPercent, |
| 83 | + volumeNumber, |
| 84 | + marketType: 'equity', |
| 85 | + isHip3: false, |
| 86 | + }) as unknown as PerpsMarketData; |
| 87 | + |
| 88 | +const renderFeed = (options: Parameters<typeof usePerpsFeed>[0] = {}) => |
| 89 | + renderHook(() => usePerpsFeed(options)); |
| 90 | + |
| 91 | +// --------------------------------------------------------------------------- |
| 92 | +// Tests |
| 93 | +// --------------------------------------------------------------------------- |
| 94 | + |
| 95 | +describe('usePerpsFeed', () => { |
| 96 | + beforeEach(() => { |
| 97 | + jest.clearAllMocks(); |
| 98 | + // Default: fuseSearch returns items as-is |
| 99 | + mockFuseSearch.mockImplementation((items: unknown[]) => items); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('no-query path', () => { |
| 103 | + it('sorts all/crypto/rwa variants by 24h price change descending', () => { |
| 104 | + const markets = [ |
| 105 | + makeMarket('LOW', '1', 100), |
| 106 | + makeMarket('HIGH', '5', 50), |
| 107 | + makeMarket('MID', '3', 75), |
| 108 | + ]; |
| 109 | + (usePerpsMarkets as jest.Mock).mockReturnValue({ |
| 110 | + markets, |
| 111 | + isLoading: false, |
| 112 | + refresh: mockRefetch, |
| 113 | + isRefreshing: false, |
| 114 | + }); |
| 115 | + |
| 116 | + for (const variant of ['all', 'crypto', 'rwa'] as const) { |
| 117 | + const { result } = renderFeed({ variant }); |
| 118 | + const symbols = result.current.data.map((d) => d.market.symbol); |
| 119 | + expect(symbols).toEqual(['HIGH', 'MID', 'LOW']); |
| 120 | + } |
| 121 | + }); |
| 122 | + |
| 123 | + it('sorts macro variant by volume descending', () => { |
| 124 | + const markets = [ |
| 125 | + makeMarket('LOW_VOL', '5', 10), |
| 126 | + makeMarket('HIGH_VOL', '1', 200), |
| 127 | + makeMarket('MID_VOL', '3', 100), |
| 128 | + ].map((m) => ({ ...m, marketType: 'equity' as const })); |
| 129 | + |
| 130 | + (usePerpsMarkets as jest.Mock).mockReturnValue({ |
| 131 | + markets, |
| 132 | + isLoading: false, |
| 133 | + refresh: mockRefetch, |
| 134 | + isRefreshing: false, |
| 135 | + }); |
| 136 | + |
| 137 | + const { result } = renderFeed({ variant: 'macro' }); |
| 138 | + const symbols = result.current.data.map((d) => d.market.symbol); |
| 139 | + expect(symbols).toEqual(['HIGH_VOL', 'MID_VOL', 'LOW_VOL']); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe('query path', () => { |
| 144 | + it('preserves Fuse.js relevance order for non-macro variants', () => { |
| 145 | + const markets = [ |
| 146 | + makeMarket('BTC', '1', 100), |
| 147 | + makeMarket('ETH', '5', 50), |
| 148 | + makeMarket('SOL', '3', 75), |
| 149 | + ]; |
| 150 | + (usePerpsMarkets as jest.Mock).mockReturnValue({ |
| 151 | + markets, |
| 152 | + isLoading: false, |
| 153 | + refresh: mockRefetch, |
| 154 | + isRefreshing: false, |
| 155 | + }); |
| 156 | + |
| 157 | + // Fuse returns a specific relevance order (SOL first, then ETH, then BTC) |
| 158 | + const fuseRelevanceOrder = [ |
| 159 | + makeMarket('SOL', '3', 75), |
| 160 | + makeMarket('ETH', '5', 50), |
| 161 | + makeMarket('BTC', '1', 100), |
| 162 | + ]; |
| 163 | + mockFuseSearch.mockReturnValue(fuseRelevanceOrder); |
| 164 | + |
| 165 | + for (const variant of ['all', 'crypto', 'rwa'] as const) { |
| 166 | + const { result } = renderFeed({ variant, query: 'S' }); |
| 167 | + const symbols = result.current.data.map((d) => d.market.symbol); |
| 168 | + // Must match fuse order, NOT sorted by price change (which would be ETH→SOL→BTC) |
| 169 | + expect(symbols).toEqual(['SOL', 'ETH', 'BTC']); |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + it('sorts macro fuse results by volume, overriding relevance order', () => { |
| 174 | + const markets = [ |
| 175 | + makeMarket('AAPL', '1', 10), |
| 176 | + makeMarket('MSFT', '5', 200), |
| 177 | + makeMarket('NVDA', '3', 100), |
| 178 | + ].map((m) => ({ ...m, marketType: 'equity' as const })); |
| 179 | + |
| 180 | + (usePerpsMarkets as jest.Mock).mockReturnValue({ |
| 181 | + markets, |
| 182 | + isLoading: false, |
| 183 | + refresh: mockRefetch, |
| 184 | + isRefreshing: false, |
| 185 | + }); |
| 186 | + |
| 187 | + // Fuse returns relevance order: AAPL first |
| 188 | + mockFuseSearch.mockReturnValue([ |
| 189 | + markets[0], // AAPL — low volume, but top relevance match |
| 190 | + markets[1], // MSFT |
| 191 | + markets[2], // NVDA |
| 192 | + ]); |
| 193 | + |
| 194 | + const { result } = renderFeed({ variant: 'macro', query: 'A' }); |
| 195 | + const symbols = result.current.data.map((d) => d.market.symbol); |
| 196 | + // Must be sorted by volume desc, NOT fuse order |
| 197 | + expect(symbols).toEqual(['MSFT', 'NVDA', 'AAPL']); |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + describe('defaultSortOptionId', () => { |
| 202 | + it.each([ |
| 203 | + ['all', 'priceChange'], |
| 204 | + ['crypto', 'priceChange'], |
| 205 | + ['rwa', 'priceChange'], |
| 206 | + ['macro', 'volume'], |
| 207 | + ] as const)( |
| 208 | + 'returns "%s" for variant "%s"', |
| 209 | + (variant, expectedSortOptionId) => { |
| 210 | + (usePerpsMarkets as jest.Mock).mockReturnValue({ |
| 211 | + markets: [], |
| 212 | + isLoading: false, |
| 213 | + refresh: mockRefetch, |
| 214 | + isRefreshing: false, |
| 215 | + }); |
| 216 | + |
| 217 | + const { result } = renderFeed({ variant }); |
| 218 | + expect(result.current.defaultSortOptionId).toBe(expectedSortOptionId); |
| 219 | + // Also verify it matches the canonical map |
| 220 | + expect(result.current.defaultSortOptionId).toBe( |
| 221 | + PERPS_VARIANT_SORT_OPTION[variant], |
| 222 | + ); |
| 223 | + }, |
| 224 | + ); |
| 225 | + }); |
| 226 | +}); |
0 commit comments