Skip to content

Commit c63413b

Browse files
test(perps): use testIDs instead of design-system mocks in provider selector sheet
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent c0e9066 commit c63413b

2 files changed

Lines changed: 41 additions & 70 deletions

File tree

app/components/UI/Perps/components/PerpsProviderSelector/PerpsProviderSelectorSheet.test.tsx

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ jest.mock('../../../../../../locales/i18n', () => ({
1212
}));
1313

1414
jest.mock('@metamask/design-system-react-native', () => {
15+
const actual = jest.requireActual('@metamask/design-system-react-native');
1516
const ReactActual = jest.requireActual('react');
16-
const { View, Text, Pressable } = jest.requireActual('react-native');
17+
const { View } = jest.requireActual('react-native');
1718

1819
const MockBottomSheet = ReactActual.forwardRef(
1920
(
@@ -47,61 +48,8 @@ jest.mock('@metamask/design-system-react-native', () => {
4748
MockBottomSheet.displayName = 'BottomSheet';
4849

4950
return {
51+
...actual,
5052
BottomSheet: MockBottomSheet,
51-
BottomSheetHeader: ({
52-
children,
53-
onClose,
54-
}: {
55-
children?: React.ReactNode;
56-
onClose?: () => void;
57-
}) => (
58-
<View>
59-
<Text>{children}</Text>
60-
<Pressable testID="header-close" onPress={onClose} />
61-
</View>
62-
),
63-
ListItemSelect: ({
64-
title,
65-
description,
66-
titleEndAccessory,
67-
onPress,
68-
testID,
69-
isSelected,
70-
accessibilityRole,
71-
accessibilityState,
72-
}: {
73-
title?: string;
74-
description?: string;
75-
titleEndAccessory?: React.ReactNode;
76-
onPress?: () => void;
77-
testID?: string;
78-
isSelected?: boolean;
79-
accessibilityRole?: string;
80-
accessibilityState?: { selected?: boolean };
81-
}) => (
82-
<Pressable
83-
testID={testID}
84-
onPress={onPress}
85-
accessibilityRole={accessibilityRole}
86-
accessibilityState={accessibilityState}
87-
>
88-
<Text>{title}</Text>
89-
{titleEndAccessory}
90-
<Text>{description}</Text>
91-
{isSelected ? <View testID={`${testID}-selected`} /> : null}
92-
</Pressable>
93-
),
94-
Tag: ({
95-
children,
96-
severity,
97-
}: {
98-
children?: React.ReactNode;
99-
severity?: string;
100-
}) => <Text testID={`tag-${severity}`}>{children}</Text>,
101-
TagSeverity: {
102-
Warning: 'Warning',
103-
Neutral: 'Neutral',
104-
},
10553
};
10654
});
10755

@@ -142,25 +90,37 @@ describe('PerpsProviderSelectorSheet', () => {
14290
availableProviders: ['hyperliquid'],
14391
});
14492

145-
const { getAllByText, queryByText } = render(
93+
const { getByTestId, queryByTestId } = render(
14694
<PerpsProviderSelectorSheet {...defaultProps} />,
14795
);
14896

149-
expect(getAllByText('HyperLiquid').length).toBeGreaterThan(0);
150-
expect(queryByText('MYX')).toBeNull();
97+
expect(
98+
getByTestId('provider-sheet-option-hyperliquid-mainnet'),
99+
).toBeOnTheScreen();
100+
expect(
101+
getByTestId('provider-sheet-option-hyperliquid-testnet'),
102+
).toBeOnTheScreen();
103+
expect(queryByTestId('provider-sheet-option-myx-mainnet')).toBeNull();
104+
expect(queryByTestId('provider-sheet-option-myx-testnet')).toBeNull();
151105
});
152106

153107
it('renders all matching options when all providers available', () => {
154108
mockUsePerpsProvider.mockReturnValue({
155109
availableProviders: ['hyperliquid', 'myx'],
156110
});
157111

158-
const { getAllByText } = render(
112+
const { getByTestId } = render(
159113
<PerpsProviderSelectorSheet {...defaultProps} />,
160114
);
161115

162-
expect(getAllByText('HyperLiquid').length).toBeGreaterThan(0);
163-
expect(getAllByText('MYX').length).toBeGreaterThan(0);
116+
expect(
117+
getByTestId('provider-sheet-option-hyperliquid-mainnet'),
118+
).toBeOnTheScreen();
119+
expect(
120+
getByTestId('provider-sheet-option-hyperliquid-testnet'),
121+
).toBeOnTheScreen();
122+
expect(getByTestId('provider-sheet-option-myx-mainnet')).toBeOnTheScreen();
123+
expect(getByTestId('provider-sheet-option-myx-testnet')).toBeOnTheScreen();
164124
});
165125

166126
it('calls onOptionSelect when an option is pressed', async () => {
@@ -194,19 +154,22 @@ describe('PerpsProviderSelectorSheet', () => {
194154
);
195155

196156
expect(
197-
getByTestId('provider-sheet-option-hyperliquid-mainnet-selected'),
198-
).toBeOnTheScreen();
157+
getByTestId('provider-sheet-option-hyperliquid-mainnet').props
158+
.accessibilityState?.selected,
159+
).toBe(true);
199160
});
200161

201162
it('renders Mainnet and Testnet tags', () => {
202-
const { getAllByText, getAllByTestId } = render(
163+
const { getByTestId } = render(
203164
<PerpsProviderSelectorSheet {...defaultProps} />,
204165
);
205166

206-
expect(getAllByText('Testnet').length).toBeGreaterThan(0);
207-
expect(getAllByText('Mainnet').length).toBeGreaterThan(0);
208-
expect(getAllByTestId('tag-Warning').length).toBeGreaterThan(0);
209-
expect(getAllByTestId('tag-Neutral').length).toBeGreaterThan(0);
167+
expect(
168+
getByTestId('provider-sheet-option-hyperliquid-mainnet-tag'),
169+
).toHaveTextContent('Mainnet');
170+
expect(
171+
getByTestId('provider-sheet-option-hyperliquid-testnet-tag'),
172+
).toHaveTextContent('Testnet');
210173
});
211174

212175
it('closes the sheet when header close is pressed', () => {
@@ -215,7 +178,7 @@ describe('PerpsProviderSelectorSheet', () => {
215178
<PerpsProviderSelectorSheet {...defaultProps} onClose={onClose} />,
216179
);
217180

218-
fireEvent.press(getByTestId('header-close'));
181+
fireEvent.press(getByTestId('provider-sheet-close-button'));
219182

220183
expect(onClose).toHaveBeenCalled();
221184
});

app/components/UI/Perps/components/PerpsProviderSelector/PerpsProviderSelectorSheet.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ const PerpsProviderSelectorSheet: React.FC<PerpsProviderSelectorSheetProps> = ({
5151

5252
return (
5353
<BottomSheet ref={bottomSheetRef} goBack={onClose} testID={testID}>
54-
<BottomSheetHeader onClose={handleClose}>
54+
<BottomSheetHeader
55+
onClose={handleClose}
56+
closeButtonProps={{
57+
testID: testID ? `${testID}-close-button` : undefined,
58+
}}
59+
>
5560
{strings('perps.provider_selector.title')}
5661
</BottomSheetHeader>
5762
{filteredOptions.map((option) => {
@@ -68,6 +73,9 @@ const PerpsProviderSelectorSheet: React.FC<PerpsProviderSelectorSheetProps> = ({
6873
option.isTestnet ? TagSeverity.Warning : TagSeverity.Neutral
6974
}
7075
twClassName="self-center"
76+
testID={
77+
testID ? `${testID}-option-${option.id}-tag` : undefined
78+
}
7179
>
7280
{option.network}
7381
</Tag>

0 commit comments

Comments
 (0)