|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react-native'; |
| 3 | +import { NavigationContainer } from '@react-navigation/native'; |
| 4 | + |
| 5 | +const mockNavigate = jest.fn(); |
| 6 | + |
| 7 | +jest.mock('@react-navigation/native', () => ({ |
| 8 | + ...jest.requireActual('@react-navigation/native'), |
| 9 | + useNavigation: () => ({ navigate: mockNavigate }), |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('react-redux', () => ({ |
| 13 | + ...jest.requireActual('react-redux'), |
| 14 | + useSelector: jest.fn(), |
| 15 | +})); |
| 16 | + |
| 17 | +// Feed hooks — return empty/not-loading so NowTab renders without network calls. |
| 18 | +jest.mock('../feeds/tokens/useTokensFeed', () => ({ |
| 19 | + useTokensFeed: jest.fn(() => ({ data: [], isLoading: false })), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('../feeds/perps/usePerpsFeed', () => ({ |
| 23 | + usePerpsFeed: jest.fn(() => ({ data: [], isLoading: false })), |
| 24 | +})); |
| 25 | + |
| 26 | +jest.mock('../feeds/predictions/usePredictionsFeed', () => ({ |
| 27 | + usePredictionsFeed: jest.fn(() => ({ data: [], isLoading: false })), |
| 28 | +})); |
| 29 | + |
| 30 | +jest.mock('../feeds/stocks/useStocksFeed', () => ({ |
| 31 | + useStocksFeed: jest.fn(() => ({ data: [], isLoading: false })), |
| 32 | +})); |
| 33 | + |
| 34 | +// Mock PerpsSectionProvider as a transparent passthrough. |
| 35 | +jest.mock('../feeds/perps/PerpsSectionProvider', () => { |
| 36 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 37 | + const { createElement } = require('react'); |
| 38 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 39 | + const { View } = require('react-native'); |
| 40 | + return ({ children }: { children: unknown }) => |
| 41 | + createElement(View, null, children); |
| 42 | +}); |
| 43 | + |
| 44 | +// Mock WhatsHappeningSection to keep its transitive deps (Engine, analytics) |
| 45 | +// out of this unit test. We control rendering via mockWhatsHappeningImpl. |
| 46 | +const mockWhatsHappeningImpl = jest.fn<React.ReactElement | null, [unknown]>( |
| 47 | + () => null, |
| 48 | +); |
| 49 | + |
| 50 | +jest.mock('../../Homepage/Sections/WhatsHappening', () => { |
| 51 | + // eslint-disable-next-line @typescript-eslint/no-require-imports |
| 52 | + const { forwardRef } = require('react'); |
| 53 | + return { |
| 54 | + __esModule: true, |
| 55 | + default: forwardRef((_props: unknown, ref: unknown) => |
| 56 | + mockWhatsHappeningImpl(ref), |
| 57 | + ), |
| 58 | + }; |
| 59 | +}); |
| 60 | + |
| 61 | +import { useSelector } from 'react-redux'; |
| 62 | +import { selectPerpsEnabledFlag } from '../../../UI/Perps'; |
| 63 | +import { selectPredictEnabledFlag } from '../../../UI/Predict'; |
| 64 | +import { selectWhatsHappeningEnabled } from '../../../../selectors/featureFlagController/whatsHappening'; |
| 65 | +import NowTab from './NowTab'; |
| 66 | +import type { RefreshConfig } from '../hooks/useExploreRefresh'; |
| 67 | + |
| 68 | +const defaultRefresh: RefreshConfig = { trigger: 0, silentRefresh: true }; |
| 69 | +const defaultTabProps = { |
| 70 | + refresh: defaultRefresh, |
| 71 | + refreshing: false, |
| 72 | + onRefresh: jest.fn(), |
| 73 | +}; |
| 74 | + |
| 75 | +const renderNowTab = (props = defaultTabProps) => |
| 76 | + render( |
| 77 | + <NavigationContainer> |
| 78 | + <NowTab {...props} /> |
| 79 | + </NavigationContainer>, |
| 80 | + ); |
| 81 | + |
| 82 | +describe('NowTab — WhatsHappeningSection integration', () => { |
| 83 | + const mockUseSelector = useSelector as jest.MockedFunction< |
| 84 | + typeof useSelector |
| 85 | + >; |
| 86 | + |
| 87 | + const mockSelectorBase = (selector: unknown) => { |
| 88 | + if (selector === selectPerpsEnabledFlag) return false; |
| 89 | + if (selector === selectPredictEnabledFlag) return false; |
| 90 | + return undefined; |
| 91 | + }; |
| 92 | + |
| 93 | + beforeEach(() => { |
| 94 | + jest.clearAllMocks(); |
| 95 | + mockUseSelector.mockImplementation(mockSelectorBase); |
| 96 | + // Default: section mock renders nothing; individual tests override as needed. |
| 97 | + mockWhatsHappeningImpl.mockReturnValue(null); |
| 98 | + }); |
| 99 | + |
| 100 | + it('mounts WhatsHappeningSection and renders it when the feature flag is enabled', () => { |
| 101 | + mockUseSelector.mockImplementation((selector) => { |
| 102 | + if (selector === selectWhatsHappeningEnabled) return true; |
| 103 | + return mockSelectorBase(selector); |
| 104 | + }); |
| 105 | + (mockWhatsHappeningImpl as jest.Mock).mockReturnValue( |
| 106 | + React.createElement('View', { |
| 107 | + testID: 'homepage-whats-happening-carousel', |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + renderNowTab(); |
| 112 | + |
| 113 | + expect( |
| 114 | + screen.getByTestId('homepage-whats-happening-carousel'), |
| 115 | + ).toBeOnTheScreen(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('does not mount WhatsHappeningSection when the feature flag is disabled', () => { |
| 119 | + mockUseSelector.mockImplementation((selector) => { |
| 120 | + if (selector === selectWhatsHappeningEnabled) return false; |
| 121 | + return mockSelectorBase(selector); |
| 122 | + }); |
| 123 | + |
| 124 | + renderNowTab(); |
| 125 | + |
| 126 | + // Section is not even mounted, so the mock should never have been called. |
| 127 | + expect(mockWhatsHappeningImpl).not.toHaveBeenCalled(); |
| 128 | + expect( |
| 129 | + screen.queryByTestId('homepage-whats-happening-carousel'), |
| 130 | + ).toBeNull(); |
| 131 | + }); |
| 132 | + |
| 133 | + it('passes a ref to WhatsHappeningSection so pull-to-refresh can trigger it', () => { |
| 134 | + mockUseSelector.mockImplementation((selector) => { |
| 135 | + if (selector === selectWhatsHappeningEnabled) return true; |
| 136 | + return mockSelectorBase(selector); |
| 137 | + }); |
| 138 | + |
| 139 | + renderNowTab(); |
| 140 | + |
| 141 | + // The mock's first argument is the forwarded ref (we dropped props in the mock). |
| 142 | + // It should be a React ref object so the useEffect bridge can call .refresh(). |
| 143 | + expect(mockWhatsHappeningImpl).toHaveBeenCalled(); |
| 144 | + const [forwardedRef] = mockWhatsHappeningImpl.mock.calls[0]; |
| 145 | + expect(forwardedRef).not.toBeNull(); |
| 146 | + }); |
| 147 | +}); |
0 commit comments