-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: allow custom sorting in perps from explore cp-7.77.0 #29912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
263 changes: 263 additions & 0 deletions
263
app/components/Views/TrendingView/feeds/perps/PerpsToggleBlock.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,263 @@ | ||
| /** | ||
| * PerpsToggleBlock — unit tests | ||
| * | ||
| * Core concerns: | ||
| * 1. Renders title and the default pill's items. | ||
| * 2. "View All" button calls onViewAll with the active pill key and the | ||
| * sortOptionId prop — this is the critical wiring introduced alongside the | ||
| * sort-param feature. | ||
| * 3. Switching pills updates the key passed to onViewAll. | ||
| * 4. Shows skeletons while loading. | ||
| * 5. Analytics: trackExploreInteracted is called when a row is pressed. | ||
| */ | ||
|
|
||
| jest.mock('@shopify/flash-list', () => { | ||
| const RN = jest.requireActual<typeof import('react-native')>('react-native'); | ||
| return { FlashList: RN.FlatList }; | ||
| }); | ||
|
|
||
| jest.mock('../../search/analytics', () => ({ | ||
| trackExploreInteracted: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@metamask/design-system-twrnc-preset', () => { | ||
| const twFn = () => ({}); | ||
| twFn.style = () => ({}); | ||
| return { useTailwind: () => twFn }; | ||
| }); | ||
|
|
||
| import React from 'react'; | ||
| import { Text } from 'react-native'; | ||
| import { render, fireEvent, act } from '@testing-library/react-native'; | ||
| import type { PerpsMarketData, SortOptionId } from '@metamask/perps-controller'; | ||
| import { trackExploreInteracted } from '../../search/analytics'; | ||
| import PerpsToggleBlock, { | ||
| type PerpsToggleBlockProps, | ||
| } from './PerpsToggleBlock'; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Helpers | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| const makeMarket = (symbol: string): PerpsMarketData => | ||
| ({ | ||
| symbol, | ||
| name: `${symbol} Market`, | ||
| price: '$1.00', | ||
| change24h: '+1%', | ||
| change24hPercent: '1', | ||
| volume: '$100M', | ||
| maxLeverage: '10x', | ||
| isHip3: true, | ||
| marketType: 'equity', | ||
| }) as PerpsMarketData; | ||
|
|
||
| // Minimal PerpsRowItem mock — renders the symbol so assertions are simple. | ||
| jest.mock('./PerpsRowItem', () => { | ||
| const { TouchableOpacity, Text: RNText } = jest.requireActual('react-native'); | ||
| return function MockPerpsRowItem({ | ||
| market, | ||
| onCardPress, | ||
| }: { | ||
| market: PerpsMarketData; | ||
| onCardPress?: () => void; | ||
| }) { | ||
| return ( | ||
| <TouchableOpacity | ||
| testID={`perps-row-${market.symbol}`} | ||
| onPress={onCardPress} | ||
| > | ||
| <RNText>{market.symbol}</RNText> | ||
| </TouchableOpacity> | ||
| ); | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock('../../../../UI/Perps/components/PerpsRowSkeleton', () => { | ||
| const { View, Text: RNText } = jest.requireActual('react-native'); | ||
| return function MockPerpsRowSkeleton() { | ||
| return ( | ||
| <View> | ||
| <RNText testID="perps-row-skeleton">skeleton</RNText> | ||
| </View> | ||
| ); | ||
| }; | ||
| }); | ||
|
|
||
| const Skeleton = () => <Text testID="skeleton-item">sk</Text>; | ||
|
juanmigdr marked this conversation as resolved.
|
||
|
|
||
| const STOCKS_MARKETS = [makeMarket('AAPL'), makeMarket('GOOGL')]; | ||
| const COMMODITY_MARKETS = [makeMarket('GOLD')]; | ||
|
|
||
| const DEFAULT_TABS = [ | ||
| { key: 'stocks', name: 'Stocks', items: STOCKS_MARKETS }, | ||
| { key: 'commodities', name: 'Commodities', items: COMMODITY_MARKETS }, | ||
| ]; | ||
|
|
||
| const DEFAULT_PROPS: PerpsToggleBlockProps = { | ||
| title: 'Stocks & Commodities', | ||
| tabs: DEFAULT_TABS, | ||
| isLoading: false, | ||
| defaultPillKey: 'stocks', | ||
| onViewAll: jest.fn(), | ||
| sortOptionId: 'volume', | ||
| tabName: 'Macro', | ||
| sectionName: 'perps_stocks_commodities', | ||
| headerTestID: 'section-header-view-all-test', | ||
| idPrefix: 'test', | ||
| testIdPrefix: 'test-toggle', | ||
| listTestId: 'test-list', | ||
| }; | ||
|
|
||
| const renderBlock = (props = DEFAULT_PROPS) => | ||
| render(<PerpsToggleBlock {...props} />); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Tests | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('PerpsToggleBlock', () => { | ||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('rendering', () => { | ||
| it('renders the section title', () => { | ||
| const { getByText } = renderBlock(); | ||
| expect(getByText('Stocks & Commodities')).toBeTruthy(); | ||
|
juanmigdr marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| it('renders items for the default pill', () => { | ||
| const { getByTestId, queryByTestId } = renderBlock(); | ||
| expect(getByTestId('perps-row-AAPL')).toBeTruthy(); | ||
| expect(getByTestId('perps-row-GOOGL')).toBeTruthy(); | ||
| expect(queryByTestId('perps-row-GOLD')).toBeNull(); | ||
| }); | ||
|
|
||
| it('renders pill buttons for each tab', () => { | ||
| const { getByTestId } = renderBlock(); | ||
| expect(getByTestId('test-toggle-pill-stocks')).toBeTruthy(); | ||
| expect(getByTestId('test-toggle-pill-commodities')).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('loading state', () => { | ||
| it('shows skeletons while isLoading is true', () => { | ||
| const { getAllByTestId } = renderBlock({ | ||
| ...DEFAULT_PROPS, | ||
| isLoading: true, | ||
| }); | ||
| expect(getAllByTestId('perps-row-skeleton').length).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('pill switching', () => { | ||
| it('shows the commodities items after selecting the commodities pill', () => { | ||
| const { getByTestId, queryByTestId } = renderBlock(); | ||
|
|
||
| act(() => { | ||
| fireEvent.press(getByTestId('test-toggle-pill-commodities')); | ||
| }); | ||
|
|
||
| expect(getByTestId('perps-row-GOLD')).toBeTruthy(); | ||
| expect(queryByTestId('perps-row-AAPL')).toBeNull(); | ||
| }); | ||
|
|
||
| it('switching back to stocks shows stocks items again', () => { | ||
| const { getByTestId } = renderBlock(); | ||
|
|
||
| act(() => { | ||
| fireEvent.press(getByTestId('test-toggle-pill-commodities')); | ||
| }); | ||
| act(() => { | ||
| fireEvent.press(getByTestId('test-toggle-pill-stocks')); | ||
| }); | ||
|
|
||
| expect(getByTestId('perps-row-AAPL')).toBeTruthy(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('View All — sort and filter forwarding', () => { | ||
| it('calls onViewAll with the defaultPillKey and sortOptionId before any pill change', () => { | ||
| const onViewAll = jest.fn(); | ||
| const { getByTestId } = renderBlock({ ...DEFAULT_PROPS, onViewAll }); | ||
|
|
||
| fireEvent.press(getByTestId('section-header-view-all-test')); | ||
|
|
||
| expect(onViewAll).toHaveBeenCalledTimes(1); | ||
| expect(onViewAll).toHaveBeenCalledWith('stocks', 'volume'); | ||
| }); | ||
|
|
||
| it('calls onViewAll with the newly active pill key after switching pills', () => { | ||
| const onViewAll = jest.fn(); | ||
| const { getByTestId } = renderBlock({ ...DEFAULT_PROPS, onViewAll }); | ||
|
|
||
| act(() => { | ||
| fireEvent.press(getByTestId('test-toggle-pill-commodities')); | ||
| }); | ||
|
|
||
| fireEvent.press(getByTestId('section-header-view-all-test')); | ||
|
|
||
| expect(onViewAll).toHaveBeenCalledWith('commodities', 'volume'); | ||
| }); | ||
|
|
||
| it('forwards the sortOptionId prop unchanged regardless of active pill', () => { | ||
| const onViewAll = jest.fn(); | ||
| const { getByTestId } = renderBlock({ | ||
| ...DEFAULT_PROPS, | ||
| sortOptionId: 'priceChange', | ||
| onViewAll, | ||
| }); | ||
|
|
||
| act(() => { | ||
| fireEvent.press(getByTestId('test-toggle-pill-commodities')); | ||
| }); | ||
| fireEvent.press(getByTestId('section-header-view-all-test')); | ||
|
|
||
| expect(onViewAll).toHaveBeenCalledWith('commodities', 'priceChange'); | ||
| }); | ||
|
|
||
| it('calls onViewAll only once per press', () => { | ||
| const onViewAll = jest.fn(); | ||
| const { getByTestId } = renderBlock({ ...DEFAULT_PROPS, onViewAll }); | ||
|
|
||
| fireEvent.press(getByTestId('section-header-view-all-test')); | ||
| fireEvent.press(getByTestId('section-header-view-all-test')); | ||
|
|
||
| expect(onViewAll).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
|
|
||
| describe('analytics', () => { | ||
| it('calls trackExploreInteracted with correct context when a row is pressed', () => { | ||
| const mockTrack = trackExploreInteracted as jest.Mock; | ||
| const { getByTestId } = renderBlock(); | ||
|
|
||
| fireEvent.press(getByTestId('perps-row-AAPL')); | ||
|
|
||
| expect(mockTrack).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| interaction_type: 'section_item_tapped', | ||
| tab_name: 'Macro', | ||
| section_name: 'perps_stocks_commodities', | ||
| asset_type: 'perp', | ||
| item_clicked: 'AAPL', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('includes the correct position index in analytics', () => { | ||
| const mockTrack = trackExploreInteracted as jest.Mock; | ||
| const { getByTestId } = renderBlock(); | ||
|
|
||
| fireEvent.press(getByTestId('perps-row-GOOGL')); | ||
|
|
||
| expect(mockTrack).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| position: 1, | ||
| item_clicked: 'GOOGL', | ||
| }), | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.