Skip to content

Commit 84fdd8e

Browse files
authored
feat: remove derived testIDs and add missing testIDs (#5099)
1 parent b3d0dea commit 84fdd8e

68 files changed

Lines changed: 20598 additions & 1846 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/6.x/docs/guides/migration.md‎

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,61 +71,41 @@ You can use the component's color prop where available, or override the correspo
7171

7272
### Test IDs
7373

74-
Hardcoded default test IDs have been removed for the components listed below. Many of these components also derive test IDs for their internal parts by appending a suffix to the `testID` prop (e.g. `${testID}-container`). Since `testID` is no longer defaulted to a hardcoded value, none of these derived test IDs are set either unless you pass a `testID` explicitly — so all queries by the IDs below will stop matching:
74+
Hardcoded default test IDs have been removed for the components listed below:
7575

7676
- `Appbar.Content`: `appbar-content`
77-
- `appbar-content-title-text`
7877
- `Appbar.Header`: `appbar-header`
79-
- `appbar-header-root-layer`
8078
- `BottomNavigation`: `bottom-navigation`
81-
- `bottom-navigation-bar`
8279
- `BottomNavigation.Bar`: `bottom-navigation-bar`
83-
- `bottom-navigation-bar-content`
84-
- `bottom-navigation-bar-content-wrapper`
8580
- `Button`: `button`
86-
- `button-container`
87-
- `button-icon-container`
88-
- `button-text`
8981
- `Card`: `card`
90-
- `card-container`
91-
- `card-outline`
9282
- `Chip`: `chip`
93-
- `chip-container`
9483
- `Drawer.CollapsedItem`: `drawer-collapsed-item`
95-
- `drawer-collapsed-item-outline`
96-
- `drawer-collapsed-item-container`
9784
- `FAB`: `floating-action-button`
98-
- `floating-action-button-container`
99-
- `floating-action-button-text`
10085
- `FAB.Extended`: `extended-floating-action-button`
101-
- `extended-floating-action-button-container`
102-
- `extended-floating-action-button-text`
10386
- `FAB.Menu`: `floating-action-button-menu`
10487
- `IconButton`: `icon-button`
105-
- `icon-button-container`
106-
- `icon-button-icon` (and `icon-button-icon-previous` / `icon-button-icon-current` when `animated`)
10788
- `Menu`: `menu`
108-
- `menu-view`
109-
- `menu-surface`
11089
- `Menu.Item`: `menu-item`
111-
- `menu-item-title`
11290
- `Modal`: `modal`
113-
- `modal-backdrop`
114-
- `modal-wrapper`
115-
- `modal-surface`
11691
- `ProgressBar`: `progress-bar`
117-
- `progress-bar-fill`
11892
- `Searchbar`: `search-bar`
119-
- `search-bar-container`
120-
- `search-bar-icon`
121-
- `search-bar-icon-wrapper`
122-
- `search-bar-clear-icon`
123-
- `search-bar-trailering-icon`
124-
- `search-bar-divider`
12593
- `Surface`: `surface`
126-
- `surface-outer-layer`
12794

128-
You can specify a `testID` explicitly to restore both the component's own test ID and all of its derived test IDs above, using the same suffixes.
95+
You can specify a `testID` explicitly to restore each component's own test ID.
96+
97+
These components used to also derive test IDs for internal, implementation-only elements by appending a suffix to the `testID` prop (e.g. `${testID}-container`, `${testID}-icon`, `${testID}-outline`). They have been removed entirely.
98+
99+
If you were relying on internal test IDs, update your tests not to rely on internal implementation details and only interact with elements or assert content your users can reach, e.g.: query by role, label, text etc., or `testID` props accepted by the component.
100+
101+
Some components now accept explicit `testID` props for their interactable elements:
102+
103+
- `BottomNavigation`: `barTestID` for the internal `BottomNavigation.Bar`, replacing the previous `${testID}-bar` derivation.
104+
- `Chip`: `closeIconTestID` for the close icon button.
105+
- `Dialog` and `Modal`: `overlayTestID` for the overlay displayed behind the content.
106+
- `Menu`: `overlayTestID` for the overlay displayed behind the menu.
107+
- `Searchbar`: `searchTestID`, `clearTestID`, and `trailingTestID` for the search, clear, and trailing icon buttons.
108+
- `Snackbar`: `iconTestID` for the icon button.
129109

130110
## Components
131111

‎src/components/Appbar/AppbarContent.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ const AppbarContent = ({
135135
numberOfLines={1}
136136
accessible
137137
role={onPress ? 'none' : 'heading'}
138-
testID={testID ? `${testID}-title-text` : undefined}
139138
maxFontSizeMultiplier={titleMaxFontSizeMultiplier}
140139
>
141140
{title}

‎src/components/BottomNavigation/BottomNavigation.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ export type Props<Route extends BaseRoute> = {
252252
* TestID used for testing purposes
253253
*/
254254
testID?: string;
255+
/**
256+
* testID for the underlying `BottomNavigation.Bar`.
257+
*/
258+
barTestID?: string;
255259
};
256260

257261
const FAR_FAR_AWAY = Platform.OS === 'web' ? 0 : 9999;
@@ -336,6 +340,7 @@ const BottomNavigation = <Route extends BaseRoute>({
336340
labelMaxFontSizeMultiplier = 1,
337341
compact: compactProp,
338342
testID,
343+
barTestID,
339344
theme: themeOverrides,
340345
getLazy = ({ route }: { route: Route }) => route.lazy,
341346
}: Props<Route>) => {
@@ -579,7 +584,7 @@ const BottomNavigation = <Route extends BaseRoute>({
579584
safeAreaInsets={safeAreaInsets}
580585
labelMaxFontSizeMultiplier={labelMaxFontSizeMultiplier}
581586
compact={compact}
582-
testID={testID ? `${testID}-bar` : undefined}
587+
testID={barTestID}
583588
theme={theme}
584589
/>
585590
</View>

‎src/components/BottomNavigation/BottomNavigationBar.tsx‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,10 +493,7 @@ const BottomNavigationBar = <Route extends BaseRoute>({
493493
]}
494494
onLayout={onLayout}
495495
>
496-
<Animated.View
497-
style={[styles.barContent, { backgroundColor }]}
498-
testID={testID ? `${testID}-content` : undefined}
499-
>
496+
<Animated.View style={[styles.barContent, { backgroundColor }]}>
500497
<View
501498
style={[
502499
styles.items,
@@ -509,7 +506,6 @@ const BottomNavigationBar = <Route extends BaseRoute>({
509506
},
510507
]}
511508
role={'tablist'}
512-
testID={testID ? `${testID}-content-wrapper` : undefined}
513509
>
514510
{routes.map((route, index) => {
515511
const focused = navigationState.index === index;

‎src/components/Button/Button.tsx‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,6 @@ const Button = ({
292292
<Surface
293293
{...rest}
294294
ref={ref}
295-
testID={testID ? `${testID}-container` : undefined}
296295
backgroundColor={backgroundOpacity < 1 ? 'transparent' : backgroundColor}
297296
{...touchableStyle}
298297
style={[
@@ -342,10 +341,7 @@ const Button = ({
342341
>
343342
<View style={[styles.content, { opacity: textOpacity }, contentStyle]}>
344343
{icon && loading !== true ? (
345-
<View
346-
style={iconStyle}
347-
testID={testID ? `${testID}-icon-container` : undefined}
348-
>
344+
<View style={iconStyle}>
349345
<Icon
350346
source={icon}
351347
size={customLabelSize ?? iconSize}
@@ -372,7 +368,6 @@ const Button = ({
372368
variant="labelLarge"
373369
selectable={false}
374370
numberOfLines={1}
375-
testID={testID ? `${testID}-text` : undefined}
376371
style={[
377372
styles.label,
378373
isMode('text')

‎src/components/Card/Card.tsx‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ const Card = ({
204204
const borderRadius = theme.shapes.corner.medium;
205205

206206
const content = (
207-
<View style={[styles.innerContainer, contentStyle]} testID={testID}>
207+
<View style={[styles.innerContainer, contentStyle]}>
208208
{React.Children.map(children, (child, index) =>
209209
React.isValidElement(child)
210210
? React.cloneElement(child as React.ReactElement<any>, {
@@ -225,13 +225,12 @@ const Card = ({
225225
style={[{ borderColor }, style]}
226226
theme={theme}
227227
elevation={elevation}
228-
testID={testID ? `${testID}-container` : undefined}
228+
testID={hasPassedTouchHandler ? undefined : testID}
229229
{...rest}
230230
>
231231
{isMode('outlined') && (
232232
<View
233233
pointerEvents="none"
234-
testID={testID ? `${testID}-outline` : undefined}
235234
style={[
236235
{
237236
borderColor,
@@ -252,6 +251,7 @@ const Card = ({
252251
onPress={onPress}
253252
onPressIn={handlePressIn}
254253
onPressOut={handlePressOut}
254+
testID={testID}
255255
>
256256
{content}
257257
</Pressable>

‎src/components/Card/CardTitle.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export type Props = ViewProps & {
102102
* @optional
103103
*/
104104
theme?: ThemeProp;
105+
/**
106+
* testID to be used on tests.
107+
*/
108+
testID?: string;
105109
};
106110

107111
const LEFT_SIZE = 40;
@@ -143,14 +147,15 @@ const CardTitle = ({
143147
rightStyle,
144148
style,
145149
theme: themeOverrides,
150+
testID,
146151
}: Props) => {
147152
useInternalTheme(themeOverrides);
148153

149154
const minHeight = subtitle || left || right ? 72 : 50;
150155
const marginBottom = subtitle ? 0 : 2;
151156

152157
return (
153-
<View style={[styles.container, { minHeight }, style]}>
158+
<View style={[styles.container, { minHeight }, style]} testID={testID}>
154159
{left ? (
155160
<View style={[styles.left, leftStyle]}>
156161
{left({

‎src/components/Checkbox/CheckboxItem.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ const CheckboxItem = ({
174174
{isLeading && checkbox}
175175
<Text
176176
variant={labelVariant}
177-
testID={testID ? `${testID}-text` : undefined}
178177
maxFontSizeMultiplier={labelMaxFontSizeMultiplier}
179178
style={[styles.label, computedStyle, labelStyle]}
180179
>

‎src/components/Chip/Chip.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export type Props = Omit<ViewProps, 'style'> & {
8787
* Accessibility label for the close icon. This is read by the screen reader when the user taps the close icon.
8888
*/
8989
closeIconAccessibilityLabel?: string;
90+
/**
91+
* testID for the close icon button.
92+
*/
93+
closeIconTestID?: string;
9094
/**
9195
* Function to execute on press.
9296
*/
@@ -186,6 +190,7 @@ const Chip = ({
186190
'aria-label': ariaLabel,
187191
role = 'button',
188192
closeIconAccessibilityLabel = 'Close',
193+
closeIconTestID,
189194
onPress,
190195
onLongPress,
191196
onPressOut,
@@ -281,7 +286,6 @@ const Chip = ({
281286
elevation={elevation}
282287
transitionDuration={elevationTransitionDuration}
283288
{...rest}
284-
testID={testID ? `${testID}-container` : undefined}
285289
theme={theme}
286290
>
287291
<TouchableRipple
@@ -386,6 +390,7 @@ const Chip = ({
386390
disabled={disabled}
387391
role="button"
388392
aria-label={closeIconAccessibilityLabel}
393+
testID={closeIconTestID}
389394
>
390395
<View style={[styles.icon, styles.closeIcon, styles.md3CloseIcon]}>
391396
{closeIcon ? (

‎src/components/CrossFadeIcon.tsx‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ const CrossFadeIcon = ({
9898

9999
return (
100100
<View
101+
testID={testID}
101102
style={[
102103
styles.content,
103104
{
@@ -107,17 +108,11 @@ const CrossFadeIcon = ({
107108
]}
108109
>
109110
{hasPreviousIcon ? (
110-
<Animated.View
111-
style={[styles.icon, previousIconStyle]}
112-
testID={testID ? `${testID}-previous` : undefined}
113-
>
111+
<Animated.View style={[styles.icon, previousIconStyle]}>
114112
<Icon source={previousIcon} size={size} color={color} theme={theme} />
115113
</Animated.View>
116114
) : null}
117-
<Animated.View
118-
style={[styles.icon, currentIconStyle]}
119-
testID={testID ? `${testID}-current` : undefined}
120-
>
115+
<Animated.View style={[styles.icon, currentIconStyle]}>
121116
<Icon source={currentIcon} size={size} color={color} theme={theme} />
122117
</Animated.View>
123118
</View>

0 commit comments

Comments
 (0)